#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 sigaction {
  void (*sa_handler)(int);
  int /*<<< orphan*/ sa_flags;
};
struct itimerval {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ITIMER_REAL;
int /*<<< orphan*/ SA_RESETHAND;
int /*<<< orphan*/ SIGALRM;
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
int setitimer(int /*<<< orphan*/, struct itimerval *, struct itimerval *);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             struct sigaction *);
int setup_timer(struct itimerval *timer, struct itimerval *old_timer,
                struct sigaction *old_sa, void (*timeout_handler)(int)) {
  struct sigaction sa;
  memset(&sa, 0, sizeof sa);
  sa.sa_handler = timeout_handler;
  sa.sa_flags = SA_RESETHAND;
  sigaction(SIGALRM, &sa, old_sa);
  return setitimer(ITIMER_REAL, timer, old_timer);
}


x86 assembly (clang, -O0):
<setup_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rsi
call   1030 <sigaction@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <setitimer@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_timer>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsp,%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %rbx,(%rsp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1030 <sigaction@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1050 <setitimer@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.itimerval = type { i32 }
%struct.sigaction = type { void (i32)*, i32 }
@SA_RESETHAND = global i32 0
@SIGALRM = global i32 0
@ITIMER_REAL = global i32 0
define i32 @setup_timer(%struct.itimerval* %0, %struct.itimerval* %1, %struct.sigaction* %2, void (i32)* %3) {
%5 = alloca %struct.itimerval*
%6 = alloca %struct.itimerval*
%7 = alloca %struct.sigaction*
%8 = alloca void (i32)*
%9 = alloca %struct.sigaction
store %struct.itimerval* %0, %struct.itimerval** %5
store %struct.itimerval* %1, %struct.itimerval** %6
store %struct.sigaction* %2, %struct.sigaction** %7
store void (i32)* %3, void (i32)** %8
%10 = call i32 @memset(%struct.sigaction* %9, i32 0, i32 16)
%11 = load void (i32)*, void (i32)** %8
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
store void (i32)* %11, void (i32)** %12
%13 = load i32, i32* @SA_RESETHAND
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @SIGALRM
%16 = load %struct.sigaction*, %struct.sigaction** %7
%17 = call i32 @sigaction(i32 %15, %struct.sigaction* %9, %struct.sigaction* %16)
%18 = load i32, i32* @ITIMER_REAL
%19 = load %struct.itimerval*, %struct.itimerval** %5
%20 = load %struct.itimerval*, %struct.itimerval** %6
%21 = call i32 @setitimer(i32 %18, %struct.itimerval* %19, %struct.itimerval* %20)
ret i32 %21
}

LLVM IR (-O2):
%struct.itimerval = type { i32 }
%struct.sigaction = type { void (i32)*, i32 }
@SA_RESETHAND = global i32 0
@SIGALRM = global i32 0
@ITIMER_REAL = global i32 0
define i32 @setup_timer(%struct.itimerval* nocapture %0, %struct.itimerval* nocapture %1, %struct.sigaction* %2, void (i32)* %3) {
%5 = alloca %struct.sigaction
%6 = bitcast %struct.sigaction* %5 to i8*
%7 = call i32 @memset(%struct.sigaction* %5, i32 0, i32 16)
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 0
store void (i32)* %3, void (i32)** %8
%9 = load i32, i32* @SA_RESETHAND
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @SIGALRM
%12 = call i32 @sigaction(i32 %11, %struct.sigaction* %5, %struct.sigaction* %2)
%13 = load i32, i32* @ITIMER_REAL
%14 = call i32 @setitimer(i32 %13, %struct.itimerval* %0, %struct.itimerval* %1)
ret i32 %14
}