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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#![allow(
    dead_code,
    mutable_transmutes,
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]
extern "C" {
    pub type _IO_wide_data;
    pub type _IO_codecvt;
    pub type _IO_marker;
    static mut stderr: *mut FILE;
    fn fprintf(_: *mut FILE, _: *const libc::c_char, _: ...) -> libc::c_int;
    fn perror(__s: *const libc::c_char);
    fn malloc(_: libc::c_ulong) -> *mut libc::c_void;
    fn free(_: *mut libc::c_void);
    fn rc_gpio_get_value(chip: libc::c_int, pin: libc::c_int) -> libc::c_int;
    fn rc_gpio_init_event(
        chip: libc::c_int,
        pin: libc::c_int,
        handle_flags: libc::c_int,
        event_flags: libc::c_int,
    ) -> libc::c_int;
    fn rc_gpio_poll(
        chip: libc::c_int,
        pin: libc::c_int,
        timeout_ms: libc::c_int,
        event_time_ns: *mut uint64_t,
    ) -> libc::c_int;
    fn rc_usleep(us: libc::c_uint);
    fn pthread_mutex_init(__mutex: *mut pthread_mutex_t, __mutexattr: *const pthread_mutexattr_t) -> libc::c_int;
    fn pthread_mutex_lock(__mutex: *mut pthread_mutex_t) -> libc::c_int;
    fn pthread_mutex_unlock(__mutex: *mut pthread_mutex_t) -> libc::c_int;
    fn rc_pthread_timed_join(
        thread: pthread_t,
        retval: *mut *mut libc::c_void,
        timeout_sec: libc::c_float,
    ) -> libc::c_int;
    fn rc_pthread_create(
        thread: *mut pthread_t,
        func: Option<unsafe extern "C" fn(*mut libc::c_void) -> *mut libc::c_void>,
        arg: *mut libc::c_void,
        policy: libc::c_int,
        priority: libc::c_int,
    ) -> libc::c_int;
    fn pthread_cond_wait(__cond: *mut pthread_cond_t, __mutex: *mut pthread_mutex_t) -> libc::c_int;
    fn pthread_cond_broadcast(__cond: *mut pthread_cond_t) -> libc::c_int;
    fn pthread_cond_init(__cond: *mut pthread_cond_t, __cond_attr: *const pthread_condattr_t) -> libc::c_int;
}
pub type size_t = libc::c_ulong;
pub type __uint64_t = libc::c_ulong;
pub type __off_t = libc::c_long;
pub type __off64_t = libc::c_long;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _IO_FILE {
    pub _flags: libc::c_int,
    pub _IO_read_ptr: *mut libc::c_char,
    pub _IO_read_end: *mut libc::c_char,
    pub _IO_read_base: *mut libc::c_char,
    pub _IO_write_base: *mut libc::c_char,
    pub _IO_write_ptr: *mut libc::c_char,
    pub _IO_write_end: *mut libc::c_char,
    pub _IO_buf_base: *mut libc::c_char,
    pub _IO_buf_end: *mut libc::c_char,
    pub _IO_save_base: *mut libc::c_char,
    pub _IO_backup_base: *mut libc::c_char,
    pub _IO_save_end: *mut libc::c_char,
    pub _markers: *mut _IO_marker,
    pub _chain: *mut _IO_FILE,
    pub _fileno: libc::c_int,
    pub _flags2: libc::c_int,
    pub _old_offset: __off_t,
    pub _cur_column: libc::c_ushort,
    pub _vtable_offset: libc::c_schar,
    pub _shortbuf: [libc::c_char; 1],
    pub _lock: *mut libc::c_void,
    pub _offset: __off64_t,
    pub _codecvt: *mut _IO_codecvt,
    pub _wide_data: *mut _IO_wide_data,
    pub _freeres_list: *mut _IO_FILE,
    pub _freeres_buf: *mut libc::c_void,
    pub __pad5: size_t,
    pub _mode: libc::c_int,
    pub _unused2: [libc::c_char; 20],
}
pub type _IO_lock_t = ();
pub type FILE = _IO_FILE;
#[derive(Copy, Clone)]
#[repr(C)]
pub union __atomic_wide_counter {
    pub __value64: libc::c_ulonglong,
    pub __value32: C2RustUnnamed,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed {
    pub __low: libc::c_uint,
    pub __high: libc::c_uint,
}
#[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 __wseq: __atomic_wide_counter,
    pub __g1_start: __atomic_wide_counter,
    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],
}
pub type pthread_t = libc::c_ulong;
#[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 uint64_t = __uint64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct btn_cfg_t {
    pub press_cb: Option<unsafe extern "C" fn() -> ()>,
    pub release_cb: Option<unsafe extern "C" fn() -> ()>,
    pub poll_thread: pthread_t,
    pub started: libc::c_char,
    pub pol: libc::c_char,
    pub press_mutex: pthread_mutex_t,
    pub press_condition: pthread_cond_t,
    pub release_mutex: pthread_mutex_t,
    pub release_condition: pthread_cond_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct thread_cfg_t {
    pub chip: libc::c_int,
    pub pin: libc::c_int,
    pub pol: libc::c_int,
    pub debounce: libc::c_int,
}
static mut cfg: [[*mut btn_cfg_t; 64]; 6] = [[0 as *const btn_cfg_t as *mut btn_cfg_t; 64]; 6];
static mut shutdown_flag: libc::c_int = 0 as libc::c_int;
unsafe extern "C" fn poll_thread_func(mut arg: *mut libc::c_void) -> *mut libc::c_void {
    let mut val: libc::c_int = 0;
    let mut event: libc::c_int = 0;
    let mut chip: libc::c_int = 0;
    let mut pin: libc::c_int = 0;
    let mut pol: libc::c_int = 0;
    let mut debounce: libc::c_int = 0;
    let mut press_expected_event: libc::c_int = 0;
    let mut release_expected_event: libc::c_int = 0;
    let mut press_thread: pthread_t = 0;
    let mut release_thread: pthread_t = 0;
    let mut thread_cfg: thread_cfg_t = *(arg as *mut thread_cfg_t);
    chip = thread_cfg.chip;
    pin = thread_cfg.pin;
    pol = thread_cfg.pol;
    debounce = thread_cfg.debounce;
    (*cfg[chip as usize][pin as usize]).started = 1 as libc::c_int as libc::c_char;
    if pol == 1 as libc::c_int {
        press_expected_event = 2 as libc::c_int;
        release_expected_event = 1 as libc::c_int;
    } else {
        press_expected_event = 1 as libc::c_int;
        release_expected_event = 2 as libc::c_int;
    }
    while shutdown_flag == 0 {
        event = rc_gpio_poll(chip, pin, 500 as libc::c_int, 0 as *mut uint64_t);
        if event == -(1 as libc::c_int) {
            fprintf(
                stderr,
                b"ERROR in rc_button handler thread\n\0" as *const u8 as *const libc::c_char,
            );
            return 0 as *mut libc::c_void;
        }
        if event == 0 as libc::c_int {
            continue;
        }
        if debounce != 0 {
            rc_usleep(debounce as libc::c_uint);
            val = rc_gpio_get_value(chip, pin);
            if val == -(1 as libc::c_int) {
                fprintf(
                    stderr,
                    b"ERROR in rc_button handler thread\n\0" as *const u8 as *const libc::c_char,
                );
                return 0 as *mut libc::c_void;
            }
            if event == 2 as libc::c_int && val != 0 as libc::c_int {
                continue;
            }
            if event == 1 as libc::c_int && val != 1 as libc::c_int {
                continue;
            }
        }
        if event == press_expected_event {
            if ((*cfg[chip as usize][pin as usize]).press_cb).is_some() {
                rc_pthread_create(
                    &mut press_thread,
                    ::core::mem::transmute::<
                        Option<unsafe extern "C" fn() -> ()>,
                        Option<unsafe extern "C" fn(*mut libc::c_void) -> *mut libc::c_void>,
                    >((*cfg[chip as usize][pin as usize]).press_cb),
                    0 as *mut libc::c_void,
                    0 as libc::c_int,
                    0 as libc::c_int,
                );
            }
            pthread_mutex_lock(
                &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                    .as_mut_ptr()
                    .offset(pin as isize))
                .press_mutex,
            );
            pthread_cond_broadcast(
                &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                    .as_mut_ptr()
                    .offset(pin as isize))
                .press_condition,
            );
            pthread_mutex_unlock(
                &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                    .as_mut_ptr()
                    .offset(pin as isize))
                .press_mutex,
            );
        } else if event == release_expected_event {
            if ((*cfg[chip as usize][pin as usize]).release_cb).is_some() {
                rc_pthread_create(
                    &mut release_thread,
                    ::core::mem::transmute::<
                        Option<unsafe extern "C" fn() -> ()>,
                        Option<unsafe extern "C" fn(*mut libc::c_void) -> *mut libc::c_void>,
                    >((*cfg[chip as usize][pin as usize]).release_cb),
                    0 as *mut libc::c_void,
                    0 as libc::c_int,
                    0 as libc::c_int,
                );
            }
            pthread_mutex_lock(
                &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                    .as_mut_ptr()
                    .offset(pin as isize))
                .release_mutex,
            );
            pthread_cond_broadcast(
                &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                    .as_mut_ptr()
                    .offset(pin as isize))
                .release_condition,
            );
            pthread_mutex_unlock(
                &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                    .as_mut_ptr()
                    .offset(pin as isize))
                .release_mutex,
            );
        }
    }
    return 0 as *mut libc::c_void;
}
#[no_mangle]
pub unsafe extern "C" fn rc_button_init(
    mut chip: libc::c_int,
    mut pin: libc::c_int,
    mut polarity: libc::c_char,
    mut debounce_us: libc::c_int,
) -> libc::c_int {
    let mut i: libc::c_int = 0;
    let mut ptr: *mut btn_cfg_t = 0 as *mut btn_cfg_t;
    let mut thread_cfg: thread_cfg_t = thread_cfg_t {
        chip: 0,
        pin: 0,
        pol: 0,
        debounce: 0,
    };
    if chip < 0 as libc::c_int || chip >= 6 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_init, chip out of bounds\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if pin < 0 as libc::c_int || pin >= 64 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_init, pin out of bounds\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if polarity as libc::c_int != 0 as libc::c_int && polarity as libc::c_int != 1 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_init\n\0" as *const u8 as *const libc::c_char,
        );
        fprintf(
            stderr,
            b"polarity must be RC_BTN_POLARITY_NORM_LOW or RC_BTN_POLARITY_NORM_HIGH\n\0" as *const u8
                as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if debounce_us < 0 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_init, debounce_us must be >=0\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if !ptr.is_null() {
        fprintf(
            stderr,
            b"ERROR in rc_button_init, button already initialized\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if rc_gpio_init_event(
        chip,
        pin,
        ((1 as libc::c_ulong) << 0 as libc::c_int) as libc::c_int,
        ((1 as libc::c_ulong) << 0 as libc::c_int | (1 as libc::c_ulong) << 1 as libc::c_int) as libc::c_int,
    ) == -(1 as libc::c_int)
    {
        fprintf(
            stderr,
            b"ERROR: in rc_button_init, failed to setup GPIO pin\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    ptr = malloc(::core::mem::size_of::<btn_cfg_t>() as libc::c_ulong) as *mut btn_cfg_t;
    if ptr.is_null() {
        perror(b"ERROR in rc_button_init\0" as *const u8 as *const libc::c_char);
        return -(1 as libc::c_int);
    }
    (*ptr).press_cb = None;
    (*ptr).release_cb = None;
    (*ptr).poll_thread = 0 as libc::c_int as pthread_t;
    (*ptr).started = 0 as libc::c_int as libc::c_char;
    (*ptr).pol = polarity;
    pthread_mutex_init(&mut (*ptr).press_mutex, 0 as *const pthread_mutexattr_t);
    pthread_cond_init(&mut (*ptr).press_condition, 0 as *const pthread_condattr_t);
    pthread_mutex_init(&mut (*ptr).release_mutex, 0 as *const pthread_mutexattr_t);
    pthread_cond_init(&mut (*ptr).release_condition, 0 as *const pthread_condattr_t);
    thread_cfg.chip = chip;
    thread_cfg.pin = pin;
    thread_cfg.pol = polarity as libc::c_int;
    thread_cfg.debounce = debounce_us;
    shutdown_flag = 0 as libc::c_int;
    cfg[chip as usize][pin as usize] = ptr;
    if rc_pthread_create(
        &mut (*ptr).poll_thread,
        Some(poll_thread_func as unsafe extern "C" fn(*mut libc::c_void) -> *mut libc::c_void),
        &mut thread_cfg as *mut thread_cfg_t as *mut libc::c_void,
        0 as libc::c_int,
        0 as libc::c_int,
    ) != 0
    {
        fprintf(
            stderr,
            b"ERROR in rc_button_init, failed to start press handler thread\n\0" as *const u8 as *const libc::c_char,
        );
        cfg[chip as usize][pin as usize] = 0 as *mut btn_cfg_t;
        return -(1 as libc::c_int);
    }
    i = 0 as libc::c_int;
    while (*ptr).started as libc::c_int == 0 as libc::c_int {
        i += 1;
        if i >= 100 as libc::c_int {
            fprintf(
                stderr,
                b"ERROR in rc_button_init, timeout waiting for thread to start\n\0" as *const u8 as *const libc::c_char,
            );
            cfg[chip as usize][pin as usize] = 0 as *mut btn_cfg_t;
            return -(1 as libc::c_int);
        }
        rc_usleep(1000 as libc::c_int as libc::c_uint);
    }
    return 0 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn rc_button_cleanup() {
    let mut i: libc::c_int = 0;
    let mut j: libc::c_int = 0;
    let mut ret: libc::c_int = 0;
    shutdown_flag = 1 as libc::c_int;
    i = 0 as libc::c_int;
    while i < 6 as libc::c_int {
        j = 0 as libc::c_int;
        while j < 64 as libc::c_int {
            if !(cfg[i as usize][j as usize]).is_null() {
                ret = rc_pthread_timed_join(
                    (*cfg[i as usize][j as usize]).poll_thread,
                    0 as *mut *mut libc::c_void,
                    3.0f64 as libc::c_float,
                );
                if ret == -(1 as libc::c_int) {
                    fprintf(
                        stderr,
                        b"WARNING in rc_button_cleanup, problem joining button handler thread for pin %d\n\0"
                            as *const u8 as *const libc::c_char,
                        i,
                    );
                } else if ret == 1 as libc::c_int {
                    fprintf(
                        stderr,
                        b"WARNING in rc_button_cleanup, thread exit timeout for pin %d\n\0" as *const u8
                            as *const libc::c_char,
                        i,
                    );
                    fprintf(
                        stderr,
                        b"most likely cause is your button press callback function is stuck and didn't return\n\0"
                            as *const u8 as *const libc::c_char,
                    );
                }
                free(cfg[i as usize][j as usize] as *mut libc::c_void);
                cfg[i as usize][j as usize] = 0 as *mut btn_cfg_t;
            }
            j += 1;
        }
        i += 1;
    }
}
#[no_mangle]
pub unsafe extern "C" fn rc_button_set_callbacks(
    mut chip: libc::c_int,
    mut pin: libc::c_int,
    mut press_func: Option<unsafe extern "C" fn() -> ()>,
    mut release_func: Option<unsafe extern "C" fn() -> ()>,
) -> libc::c_int {
    if chip < 0 as libc::c_int || chip >= 6 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_set_callbacks, chip out of bounds\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if pin < 0 as libc::c_int || pin >= 64 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_set_callbacks, pin out of bounds\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if (cfg[chip as usize][pin as usize]).is_null() {
        fprintf(
            stderr,
            b"ERROR in rc_button_set_callbacks, pin not initialized yet\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    (*cfg[chip as usize][pin as usize]).press_cb = press_func;
    (*cfg[chip as usize][pin as usize]).release_cb = release_func;
    return 0 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn rc_button_get_state(mut chip: libc::c_int, mut pin: libc::c_int) -> libc::c_int {
    let mut val: libc::c_int = 0;
    let mut ret: libc::c_int = 0;
    if chip < 0 as libc::c_int || chip >= 6 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_get_state, chip out of bounds\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if pin < 0 as libc::c_int || pin >= 64 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_get_state, pin out of bounds\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if (cfg[chip as usize][pin as usize]).is_null() {
        fprintf(
            stderr,
            b"ERROR in rc_button_get_state, pin not initialized yet\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    val = rc_gpio_get_value(chip, pin);
    if val == -(1 as libc::c_int) {
        fprintf(
            stderr,
            b"ERROR in rc_button_get_state\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if (*cfg[chip as usize][pin as usize]).pol as libc::c_int == 1 as libc::c_int {
        if val != 0 {
            ret = 0 as libc::c_int;
        } else {
            ret = 1 as libc::c_int;
        }
    } else if val != 0 {
        ret = 1 as libc::c_int;
    } else {
        ret = 0 as libc::c_int;
    }
    return ret;
}
#[no_mangle]
pub unsafe extern "C" fn rc_button_wait_for_event(
    mut chip: libc::c_int,
    mut pin: libc::c_int,
    mut press_or_release: libc::c_int,
) -> libc::c_int {
    if chip < 0 as libc::c_int || chip >= 6 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_wait_for_event, chip out of bounds\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if pin < 0 as libc::c_int || pin >= 64 as libc::c_int {
        fprintf(
            stderr,
            b"ERROR in rc_button_wait_for_event, pin out of bounds\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if (cfg[chip as usize][pin as usize]).is_null() {
        fprintf(
            stderr,
            b"ERROR in rc_button_wait_for_event, pin not initialized yet\n\0" as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    if press_or_release == 1 as libc::c_int {
        pthread_mutex_lock(
            &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                .as_mut_ptr()
                .offset(pin as isize))
            .press_mutex,
        );
        pthread_cond_wait(
            &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                .as_mut_ptr()
                .offset(pin as isize))
            .press_condition,
            &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                .as_mut_ptr()
                .offset(pin as isize))
            .press_mutex,
        );
        pthread_mutex_unlock(
            &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                .as_mut_ptr()
                .offset(pin as isize))
            .press_mutex,
        );
    } else if press_or_release == 0 as libc::c_int {
        pthread_mutex_lock(
            &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                .as_mut_ptr()
                .offset(pin as isize))
            .release_mutex,
        );
        pthread_cond_wait(
            &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                .as_mut_ptr()
                .offset(pin as isize))
            .release_condition,
            &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                .as_mut_ptr()
                .offset(pin as isize))
            .release_mutex,
        );
        pthread_mutex_unlock(
            &mut (**(*cfg.as_mut_ptr().offset(chip as isize))
                .as_mut_ptr()
                .offset(pin as isize))
            .release_mutex,
        );
    } else {
        fprintf(
            stderr,
            b"ERROR in rc_button_wait_for_event, argument should be RC_BTN_STATE_PRESSED or RC_BTN_STATE_RELEASED\n\0"
                as *const u8 as *const libc::c_char,
        );
        return -(1 as libc::c_int);
    }
    return 0 as libc::c_int;
}