#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 */
struct timeval {
  int dummy;
};
struct itimerval {
  struct timeval it_value;
};

/* Variables and functions */
int /*<<< orphan*/ ITIMER_REAL;
int getitimer(int /*<<< orphan*/, struct itimerval *);
int setitimer(int /*<<< orphan*/, struct itimerval *, int /*<<< orphan*/ *);
int wait_alarm(struct timeval *timeout) {
  struct itimerval itimeout;
  int i_retval;
  i_retval = 0;
  i_retval = getitimer(ITIMER_REAL, &itimeout);
  if (i_retval == 0) {
    itimeout.it_value = *timeout;
    i_retval = setitimer(ITIMER_REAL, &itimeout, NULL);
  }
  return i_retval;
}


x86 assembly (clang, -O0):
<wait_alarm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <getitimer@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1174 <wait_alarm+0x54>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <setitimer@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_alarm>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eb2(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1040 <getitimer@plt>
test   %eax,%eax
jne    114f <wait_alarm+0x2f>
mov    (%rbx),%eax
mov    %eax,(%rsp)
mov    (%r14),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <setitimer@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.itimerval = type { %struct.timeval }
@ITIMER_REAL = global i32 0
define i32 @wait_alarm(%struct.timeval* %0) {
%2 = alloca %struct.timeval*
%3 = alloca %struct.itimerval
%4 = alloca i32
store %struct.timeval* %0, %struct.timeval** %2
store i32 0, i32* %4
%5 = load i32, i32* @ITIMER_REAL
%6 = call i32 @getitimer(i32 %5, %struct.itimerval* %3)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %3, i32 0, i32 0
%11 = load %struct.timeval*, %struct.timeval** %2
%12 = bitcast %struct.timeval* %10 to i8*
%13 = bitcast %struct.timeval* %11 to i8*
%14 = load i32, i32* @ITIMER_REAL
%15 = call i32 @setitimer(i32 %14, %struct.itimerval* %3, i32* null)
store i32 %15, i32* %4
br label %16
16: ; preds = %9, %1
%17 = load i32, i32* %4
ret i32 %17
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.itimerval = type { %struct.timeval }
@ITIMER_REAL = global i32 0
define i32 @wait_alarm(%struct.timeval* nocapture %0) {
%2 = alloca %struct.itimerval
%3 = bitcast %struct.itimerval* %2 to i8*
%4 = load i32, i32* @ITIMER_REAL
%5 = call i32 @getitimer(i32 %4, %struct.itimerval* %2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = getelementptr %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%9 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 0, i32 0
%10 = load i32, i32* %8
store i32 %10, i32* %9
%11 = load i32, i32* @ITIMER_REAL
%12 = call i32 @setitimer(i32 %11, %struct.itimerval* %2, i32* null)
br label %13
13: ; preds = %7, %1
%14 = phi i32 [ %12, %7 ], [ %5, %1 ]
ret i32 %14
}