1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 | #![feature(asm, braced_empty_structs, core_slice_ext, lang_items, libc, no_std, start)]
#![no_std]
extern crate libc;
use core::slice::from_raw_parts_mut;
use libc::{ETIME, atoi, calloc, c_char, c_void, fflush, fgets, fputs, free, rand, srand, strlen};
use libc::types::common::c95::FILE;
extern "C" {
static stdin: *mut FILE;
static stdout: *mut FILE;
}
static ENTREZ: [i8; 10] = [69, 110, 116, 114, 101, 122, 58, 32, 10, 0];
static PLUS: [i8; 14] = [67, 39, 101, 115, 116, 32, 112, 108, 117, 115, 32, 33, 10, 0];
static MOINS: [i8; 15] = [67, 39, 101, 115, 116, 32, 109, 111, 105, 110, 115, 32, 33, 10, 0];
static mut input_number: i32 = 0;
fn read() -> *const c_char {
const MAX_SIZE: usize = 10;
unsafe {
// Useless usage of the C standard library.
let mut buffer = calloc(1, MAX_SIZE as u64) as *mut i8;
fgets(buffer, MAX_SIZE as i32, stdin)
}
}
fn print(string: *const c_char) {
unsafe {
fputs(string, stdout);
let mut slice: &mut [u8] = from_raw_parts_mut(string as *mut u8, strlen(string) as usize);
let mut needs_flushing = false;
// Useless use of mutable iterator.
for character in slice.iter_mut() {
if(*character as char == '\0') {
needs_flushing = true;
}
}
if(needs_flushing) {
fflush(stdout);
}
}
}
struct String {
value: *mut i8,
}
impl String {
fn new(chars: *const i8) -> String {
String {
value: chars as *mut i8,
}
}
// Moves the value instead of borrowing it.
fn as_ptr(self) -> *const i8 {
self.value
}
fn delete(self) {
unsafe {
free(self.value as *mut c_void);
}
}
fn to_int(self) -> i32 {
unsafe {
atoi(self.value) as i32
}
}
}
impl Clone for String {
fn clone(&self) -> String {
String {
// Only copy the pointer, does not copy the referenced values.
value: self.value
}
}
}
// Use of an empty struct for no reason.
struct Random {}
// Random number generator.
impl Random {
fn new() -> Random {
unsafe {
// Use of a fake time.
srand(ETIME as u32);
}
Random {}
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn random_int(self, max: i32) -> i32 {
unsafe {
let mut number = rand();
let mut result: i32;
// Use of inline assembly because the modulo operator gives a compile time error.
asm!("
mov eax, $0
cdq
idiv ecx
mov eax, edx
"
: "={eax}"(result)
: "0"(number) "{ecx}"(max)
: "{edx}"
: "intel");
result
}
}
}
// Use of start instead of main.
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
let mut random = Random::new();
let mut random_number = random.random_int(100);
// Useless use of parentheses.
while(random_number != unsafe { input_number }) {
// Need to create a new String each time we print because String methods moves the value.
let mut string = String::new(ENTREZ.as_ptr());
print(string.as_ptr());
let mut line = String::new(read());
unsafe {
input_number = line.clone().to_int();
}
if(random_number < unsafe { input_number }) {
let mut string = String::new(MOINS.as_ptr());
print(string.as_ptr());
}
else if(random_number > unsafe { input_number }) {
let mut string = String::new(PLUS.as_ptr());
print(string.as_ptr());
}
line.delete();
}
0
}
#[lang = "eh_personality"] extern fn eh_personality() {}
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} }
|