#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint8_t;
typedef unsigned int uint64_t;
struct timespec {
  int dummy;
};
struct nrf24l01 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int rf_command(struct nrf24l01 *, int, int /*<<< orphan*/ *, int /*<<< orphan*/,
               int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
unsigned int timespec_diff(struct timespec *, struct timespec *);
int /*<<< orphan*/ usleep(int);
int rf_wait_status(struct nrf24l01 *dev, uint8_t mask, unsigned int timeout) {
  int ret;
  struct timespec start, now;
  uint64_t millis = 0;
  clock_gettime(CLOCK_MONOTONIC, &start);
  uint8_t status = 0;
  while (millis < timeout) {
    ret = rf_command(dev, 0b11111111, NULL, 0, 0);
    if (ret < 0) {
      fprintf(stderr, "NRF24L01 "
                      "wait_status read status error\n");
      goto out;
    }
    status = ret & 0xff;
    if (status & mask) {
      ret = status;
      goto out;
    }
    usleep(100);
    clock_gettime(CLOCK_MONOTONIC, &now);
    millis = timespec_diff(&start, &now);
  }
  fprintf(stderr, "NRF24L01 "
                  "wait_status timeout\n");
  ret = -1;
out:
  return ret;
}


x86 assembly (clang, -O0):
<rf_wait_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    1223 <rf_wait_status+0xd3>
mov    -0x8(%rbp),%rdi
mov    $0xff,%esi
xor    %eax,%eax
mov    %eax,%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1040 <rf_command@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11cd <rf_wait_status+0x7d>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1050 <fprintf@plt>
jmp    123f <rf_wait_status+0xef>
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
and    -0xc(%rbp),%eax
cmp    $0x0,%eax
je     11f2 <rf_wait_status+0xa2>
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    123f <rf_wait_status+0xef>
mov    $0x64,%edi
call   1070 <usleep@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <timespec_diff@plt>
mov    %eax,-0x24(%rbp)
jmp    1182 <rf_wait_status+0x32>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rf_wait_status>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r12
mov    0x2e7b(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0xffffffff,%ebx
lea    0xea2(%rip),%rsi
test   %ebp,%ebp
je     11fb <rf_wait_status+0xab>
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    $0xff,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <rf_command@plt>
test   %eax,%eax
js     11f2 <rf_wait_status+0xa2>
movzbl %al,%eax
test   %r15d,%eax
jne    120b <rf_wait_status+0xbb>
mov    $0x64,%edi
call   1070 <usleep@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1060 <timespec_diff@plt>
cmp    %ebp,%eax
jb     11a0 <rf_wait_status+0x50>
mov    $0xffffffff,%ebx
lea    0xe38(%rip),%rsi
jmp    11fb <rf_wait_status+0xab>
lea    0xe07(%rip),%rsi
mov    %eax,%ebx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1050 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nrf24l01 = type { i32 }
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"NRF24L01 wait_status read status error\0A\00"
@.str.1 = [30 x i8] c"NRF24L01 wait_status timeout\0A\00"
define i32 @rf_wait_status(%struct.nrf24l01* %0, i32 %1, i32 %2) {
%4 = alloca %struct.nrf24l01*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
%9 = alloca %struct.timespec
%10 = alloca i32
%11 = alloca i32
store %struct.nrf24l01* %0, %struct.nrf24l01** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %10
%12 = load i32, i32* @CLOCK_MONOTONIC
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %8)
store i32 0, i32* %11
br label %14
14: ; preds = %35, %3
%15 = load i32, i32* %10
%16 = load i32, i32* %6
%17 = icmp ult i32 %15, %16
br i1 %17, label %18, label %40
18: ; preds = %14
%19 = load %struct.nrf24l01*, %struct.nrf24l01** %4
%20 = call i32 @rf_command(%struct.nrf24l01* %19, i32 255, i32* null, i32 0, i32 0)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %43
26: ; preds = %18
%27 = load i32, i32* %7
%28 = and i32 %27, 255
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = load i32, i32* %5
%31 = and i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = load i32, i32* %11
store i32 %34, i32* %7
br label %43
35: ; preds = %26
%36 = call i32 @usleep(i32 100)
%37 = load i32, i32* @CLOCK_MONOTONIC
%38 = call i32 @clock_gettime(i32 %37, %struct.timespec* %9)
%39 = call i32 @timespec_diff(%struct.timespec* %8, %struct.timespec* %9)
store i32 %39, i32* %10
br label %14
40: ; preds = %14
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %7
br label %43
43: ; preds = %40, %33, %23
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
%struct.nrf24l01 = type { i32 }
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"NRF24L01 wait_status read status error\0A\00"
@.str.1 = [30 x i8] c"NRF24L01 wait_status timeout\0A\00"
define i32 @rf_wait_status(%struct.nrf24l01* %0, i32 %1, i32 %2) {
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = bitcast %struct.timespec* %4 to i8*
%7 = bitcast %struct.timespec* %5 to i8*
%8 = load i32, i32* @CLOCK_MONOTONIC
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %4)
%10 = icmp eq i32 %2, 0
br i1 %10, label %24, label %11
11: ; preds = %3, %18
%12 = call i32 @rf_command(%struct.nrf24l01* %0, i32 255, i32* null, i32 0, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %11
%15 = and i32 %12, 255
%16 = and i32 %15, %1
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = call i32 @usleep(i32 100)
%20 = load i32, i32* @CLOCK_MONOTONIC
%21 = call i32 @clock_gettime(i32 %20, %struct.timespec* %5)
%22 = call i32 @timespec_diff(%struct.timespec* %4, %struct.timespec* %5)
%23 = icmp ult i32 %22, %2
br i1 %23, label %11, label %24
24: ; preds = %18, %11, %3
%25 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %18 ]
%26 = phi i32 [ -1, %3 ], [ %12, %11 ], [ -1, %18 ]
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* %25)
br label %29
29: ; preds = %14, %24
%30 = phi i32 [ %26, %24 ], [ %15, %14 ]
ret i32 %30
}