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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// #![allow(
// dead_code,
// mutable_transmutes,
// non_camel_case_types,
// non_snake_case,
// non_upper_case_globals,
// unused_assignments,
// unused_mut
// )]
// #![register_tool(c2rust)]
// #![feature(extern_types, label_break_value, register_tool)]
// extern "C" {
// pub type thread_pool;
// #[no_mangle]
// fn __assert_fail(
// __assertion: *const libc::c_char,
// __file: *const libc::c_char,
// __line: libc::c_uint,
// __function: *const libc::c_char,
// ) -> !;
// #[no_mangle]
// fn pthread_cond_wait(__cond: *mut pthread_cond_t, __mutex: *mut pthread_mutex_t)
// -> libc::c_int;
// #[no_mangle]
// fn pthread_cond_signal(__cond: *mut pthread_cond_t) -> libc::c_int;
// #[no_mangle]
// fn pthread_cond_destroy(__cond: *mut pthread_cond_t) -> libc::c_int;
// #[no_mangle]
// fn pthread_cond_init(
// __cond: *mut pthread_cond_t,
// __cond_attr: *const pthread_condattr_t,
// ) -> libc::c_int;
// #[no_mangle]
// fn pthread_mutex_init(
// __mutex: *mut pthread_mutex_t,
// __mutexattr: *const pthread_mutexattr_t,
// ) -> libc::c_int;
// #[no_mangle]
// fn pthread_mutex_destroy(__mutex: *mut pthread_mutex_t) -> libc::c_int;
// #[no_mangle]
// fn pthread_mutex_lock(__mutex: *mut pthread_mutex_t) -> libc::c_int;
// #[no_mangle]
// fn pthread_mutex_unlock(__mutex: *mut pthread_mutex_t) -> libc::c_int;
// #[no_mangle]
// fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
// #[no_mangle]
// fn free(_: *mut libc::c_void);
// #[no_mangle]
// fn malloc(_: libc::c_ulong) -> *mut libc::c_void;
// #[no_mangle]
// fn thread_pool_init(num_worker_threads: size_t) -> *mut thread_pool_t;
// #[no_mangle]
// fn thread_pool_add_work(
// pool_0: *mut thread_pool_t,
// function: work_function_t,
// aux: *mut libc::c_void,
// );
// #[no_mangle]
// fn thread_pool_finish(pool_0: *mut thread_pool_t);
// }
// pub type size_t = libc::c_ulong;
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub struct __pthread_internal_list {
// pub __prev: *mut __pthread_internal_list,
// pub __next: *mut __pthread_internal_list,
// }
// pub type __pthread_list_t = __pthread_internal_list;
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub struct __pthread_mutex_s {
// pub __lock: libc::c_int,
// pub __count: libc::c_uint,
// pub __owner: libc::c_int,
// pub __nusers: libc::c_uint,
// pub __kind: libc::c_int,
// pub __spins: libc::c_short,
// pub __elision: libc::c_short,
// pub __list: __pthread_list_t,
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub struct __pthread_cond_s {
// pub c2rust_unnamed: C2RustUnnamed_1,
// pub c2rust_unnamed_0: C2RustUnnamed,
// pub __g_refs: [libc::c_uint; 2],
// pub __g_size: [libc::c_uint; 2],
// pub __g1_orig_size: libc::c_uint,
// pub __wrefs: libc::c_uint,
// pub __g_signals: [libc::c_uint; 2],
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub union C2RustUnnamed {
// pub __g1_start: libc::c_ulonglong,
// pub __g1_start32: C2RustUnnamed_0,
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub struct C2RustUnnamed_0 {
// pub __low: libc::c_uint,
// pub __high: libc::c_uint,
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub union C2RustUnnamed_1 {
// pub __wseq: libc::c_ulonglong,
// pub __wseq32: C2RustUnnamed_2,
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub struct C2RustUnnamed_2 {
// pub __low: libc::c_uint,
// pub __high: libc::c_uint,
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub union pthread_mutexattr_t {
// pub __size: [libc::c_char; 4],
// pub __align: libc::c_int,
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub union pthread_condattr_t {
// pub __size: [libc::c_char; 4],
// pub __align: libc::c_int,
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub union pthread_mutex_t {
// pub __data: __pthread_mutex_s,
// pub __size: [libc::c_char; 40],
// pub __align: libc::c_long,
// }
// #[derive(Copy, Clone)]
// #[repr(C)]
// pub union pthread_cond_t {
// pub __data: __pthread_cond_s,
// pub __size: [libc::c_char; 48],
// pub __align: libc::c_longlong,
// }
// pub type thread_pool_t = thread_pool;
// pub type work_function_t = Option<unsafe extern "C" fn(_: *mut libc::c_void) -> ()>;
// #[no_mangle]
// pub static mut NUM_THREADS: size_t = 16 as libc::c_int as size_t;
// #[no_mangle]
// pub static mut MAX_DEPTH: size_t = 5 as libc::c_int as size_t;
// #[no_mangle]
// pub static mut BRANCH_FACTOR: size_t = 5 as libc::c_int as size_t;
// #[no_mangle]
// pub static mut pool: *mut thread_pool_t = 0 as *const thread_pool_t as *mut thread_pool_t;
// #[no_mangle]
// pub static mut remaining_work: size_t = 0;
// #[no_mangle]
// pub static mut remaining_work_mutex: pthread_mutex_t = pthread_mutex_t {
// __data: __pthread_mutex_s {
// __lock: 0,
// __count: 0,
// __owner: 0,
// __nusers: 0,
// __kind: 0,
// __spins: 0,
// __elision: 0,
// __list: __pthread_list_t {
// __prev: 0 as *const __pthread_internal_list as *mut __pthread_internal_list,
// __next: 0 as *const __pthread_internal_list as *mut __pthread_internal_list,
// },
// },
// };
// #[no_mangle]
// pub static mut done_cond: pthread_cond_t = pthread_cond_t {
// __data: __pthread_cond_s {
// c2rust_unnamed: C2RustUnnamed_1 { __wseq: 0 },
// c2rust_unnamed_0: C2RustUnnamed { __g1_start: 0 },
// __g_refs: [0; 2],
// __g_size: [0; 2],
// __g1_orig_size: 0,
// __wrefs: 0,
// __g_signals: [0; 2],
// },
// };
// #[no_mangle]
// pub unsafe extern "C" fn add_work(mut depth: size_t) {
// let mut aux: *mut size_t =
// malloc(::std::mem::size_of::<size_t>() as libc::c_ulong) as *mut size_t;
// if !aux.is_null() {
// } else {
// __assert_fail(
// b"aux != NULL\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 21 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 22], &[libc::c_char; 22]>(
// b"void add_work(size_t)\x00",
// ))
// .as_ptr(),
// );
// }
// *aux = depth;
// thread_pool_add_work(
// pool,
// Some(recursive_work as unsafe extern "C" fn(_: *mut libc::c_void) -> ()),
// aux as *mut libc::c_void,
// );
// let mut result: libc::c_int = pthread_mutex_lock(&mut remaining_work_mutex);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 25 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 22], &[libc::c_char; 22]>(
// b"void add_work(size_t)\x00",
// ))
// .as_ptr(),
// );
// }
// remaining_work = remaining_work.wrapping_add(1);
// result = pthread_mutex_unlock(&mut remaining_work_mutex);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 28 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 22], &[libc::c_char; 22]>(
// b"void add_work(size_t)\x00",
// ))
// .as_ptr(),
// );
// };
// }
// #[no_mangle]
// pub unsafe extern "C" fn recursive_work(mut _depth: *mut libc::c_void) {
// let mut depth: size_t = *(_depth as *mut size_t);
// free(_depth);
// if depth == MAX_DEPTH {
// printf(b"Recursion depth hit!\n\x00" as *const u8 as *const libc::c_char);
// } else {
// let mut i: size_t = 0 as libc::c_int as size_t;
// while i < BRANCH_FACTOR {
// add_work(depth.wrapping_add(1 as libc::c_int as libc::c_ulong));
// i = i.wrapping_add(1)
// }
// }
// let mut result: libc::c_int = pthread_mutex_lock(&mut remaining_work_mutex);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 45 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 28], &[libc::c_char; 28]>(
// b"void recursive_work(void *)\x00",
// ))
// .as_ptr(),
// );
// }
// remaining_work = remaining_work.wrapping_sub(1);
// if remaining_work == 0 as libc::c_int as libc::c_ulong {
// pthread_cond_signal(&mut done_cond);
// }
// result = pthread_mutex_unlock(&mut remaining_work_mutex);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 50 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 28], &[libc::c_char; 28]>(
// b"void recursive_work(void *)\x00",
// ))
// .as_ptr(),
// );
// };
// }
// unsafe fn main_0() -> libc::c_int {
// pool = thread_pool_init(NUM_THREADS);
// remaining_work = 0 as libc::c_int as size_t;
// let mut result: libc::c_int =
// pthread_mutex_init(&mut remaining_work_mutex, 0 as *const pthread_mutexattr_t);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 57 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 11], &[libc::c_char; 11]>(b"int main()\x00")).as_ptr(),
// );
// }
// result = pthread_cond_init(&mut done_cond, 0 as *const pthread_condattr_t);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 59 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 11], &[libc::c_char; 11]>(b"int main()\x00")).as_ptr(),
// );
// }
// add_work(0 as libc::c_int as size_t);
// result = pthread_mutex_lock(&mut remaining_work_mutex);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 63 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 11], &[libc::c_char; 11]>(b"int main()\x00")).as_ptr(),
// );
// }
// while remaining_work > 0 as libc::c_int as libc::c_ulong {
// result = pthread_cond_wait(&mut done_cond, &mut remaining_work_mutex);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 66 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 11], &[libc::c_char; 11]>(b"int main()\x00"))
// .as_ptr(),
// );
// }
// }
// result = pthread_mutex_unlock(&mut remaining_work_mutex);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 69 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 11], &[libc::c_char; 11]>(b"int main()\x00")).as_ptr(),
// );
// }
// thread_pool_finish(pool);
// result = pthread_mutex_destroy(&mut remaining_work_mutex);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 73 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 11], &[libc::c_char; 11]>(b"int main()\x00")).as_ptr(),
// );
// }
// result = pthread_cond_destroy(&mut done_cond);
// if result == 0 as libc::c_int {
// } else {
// __assert_fail(
// b"result == 0\x00" as *const u8 as *const libc::c_char,
// b"tests/recursive_add_work.c\x00" as *const u8 as *const libc::c_char,
// 75 as libc::c_int as libc::c_uint,
// (*::std::mem::transmute::<&[u8; 11], &[libc::c_char; 11]>(b"int main()\x00")).as_ptr(),
// );
// }
// return 0;
// }
// pub fn main() {
// unsafe { ::std::process::exit(main_0() as i32) }
// }