#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 */
typedef struct TYPE_6__ TYPE_3__;
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ timer_t;
struct TYPE_4__ {
  int /*<<< orphan*/ *sival_ptr;
};
struct sigevent {
  int sigev_signo;
  TYPE_1__ sigev_value;
  int /*<<< orphan*/ sigev_notify;
};
struct TYPE_6__ {
  int tv_sec;
  long tv_nsec;
};
struct TYPE_5__ {
  int tv_sec;
  long tv_nsec;
};
struct itimerspec {
  TYPE_3__ it_interval;
  TYPE_2__ it_value;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ SIGEV_SIGNAL;
int /*<<< orphan*/ printf(char *);
scalar_t__ timer_create(int /*<<< orphan*/, struct sigevent *,
                        int /*<<< orphan*/ *);
scalar_t__ timer_settime(int /*<<< orphan*/, int /*<<< orphan*/,
                         struct itimerspec *, struct itimerspec *);
timer_t SetTimer(int signo, int sec, int mode) {
  static struct sigevent sigev;
  static timer_t tid;
  static struct itimerspec itval;
  static struct itimerspec oitval;
  sigev.sigev_notify = SIGEV_SIGNAL;
  sigev.sigev_signo = signo;
  sigev.sigev_value.sival_ptr = &tid;
  if (timer_create(CLOCK_REALTIME, &sigev, &tid) == 0) {
    itval.it_value.tv_sec = sec / 1000;
    itval.it_value.tv_nsec = (long)(sec % 1000) * (1000000L);
    if (mode == 1) {
      itval.it_interval.tv_sec = itval.it_value.tv_sec;
      itval.it_interval.tv_nsec = itval.it_value.tv_nsec;
    } else {
      itval.it_interval.tv_sec = 0;
      itval.it_interval.tv_nsec = 0;
    }
    if (timer_settime(tid, 0, &itval, &oitval) != 0) {
      printf("time_settime error!");
    }
  } else {
    printf("timer_create error!");
    return (timer_t)-1;
  }
  return tid;
}


x86 assembly (clang, -O0):
<SetTimer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2f00(%rip)
mov    -0x8(%rbp),%eax
mov    %eax,0x2ee7(%rip)
lea    0x2ef8(%rip),%rax
mov    %rax,0x2ee1(%rip)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    0x2ec9(%rip),%rsi
lea    0x2eda(%rip),%rdx
call   1040 <timer_create@plt>
cmp    $0x0,%rax
jne    122e <SetTimer+0xfe>
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,0x2ed2(%rip)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
imul   $0xf4240,%rax,%rax
mov    %rax,0x2ebe(%rip)
cmpl   $0x1,-0x10(%rbp)
jne    11e3 <SetTimer+0xb3>
mov    0x2ea6(%rip),%eax
mov    %eax,0x2e90(%rip)
mov    0x2ea1(%rip),%rax
mov    %rax,0x2e8a(%rip)
jmp    11f8 <SetTimer+0xc8>
movl   $0x0,0x2e73(%rip)
movq   $0x0,0x2e70(%rip)
mov    0x2e5a(%rip),%edi
xor    %esi,%esi
lea    0x2e59(%rip),%rdx
lea    0x2e72(%rip),%rcx
call   1030 <timer_settime@plt>
cmp    $0x0,%rax
je     1229 <SetTimer+0xf9>
lea    0xddc(%rip),%rdi
call   1050 <printf@plt>
jmp    1246 <SetTimer+0x116>
lea    0xddf(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <SetTimer+0x11f>
mov    0x2e0c(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetTimer>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2f0a(%rip)
lea    0x2ef3(%rip),%rsi
mov    %edi,0x2eed(%rip)
lea    0x2efe(%rip),%rdx
mov    %rdx,0x2ee7(%rip)
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
call   1040 <timer_create@plt>
test   %rax,%rax
je     1187 <SetTimer+0x57>
lea    0xe99(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
jmp    1206 <SetTimer+0xd6>
movslq %ebp,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x2ecc(%rip)
imul   $0x3e8,%ecx,%edx
sub    %edx,%eax
imul   $0xf4240,%eax,%eax
cltq
mov    %rax,0x2ebd(%rip)
xor    %edx,%edx
cmp    $0x1,%ebx
cmovne %edx,%ecx
cmovne %rdx,%rax
lea    0x2e92(%rip),%rdx
mov    %ecx,0x2e8c(%rip)
mov    %rax,0x2e8d(%rip)
mov    0x2e77(%rip),%edi
lea    0x2e98(%rip),%rcx
xor    %esi,%esi
call   1030 <timer_settime@plt>
test   %rax,%rax
je     1200 <SetTimer+0xd0>
lea    0xe05(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e52(%rip),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigevent = type { i32, %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i32* }
%struct.itimerspec = type { %struct.TYPE_6__, %struct.TYPE_5__ }
%struct.TYPE_6__ = type { i32, i64 }
%struct.TYPE_5__ = type { i32, i64 }
@SetTimer.sigev = internal global %struct.sigevent zeroinitializer
@SetTimer.tid = internal global i32 0
@SetTimer.itval = internal global %struct.itimerspec zeroinitializer
@SetTimer.oitval = internal global %struct.itimerspec zeroinitializer
@SIGEV_SIGNAL = global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [20 x i8] c"time_settime error!\00"
@.str.1 = [20 x i8] c"timer_create error!\00"
define i32 @SetTimer(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i32, i32* @SIGEV_SIGNAL
store i32 %8, i32* getelementptr inbounds (%struct.sigevent, %struct.sigevent* @SetTimer.sigev, i32 0, i32 2)
%9 = load i32, i32* %5
store i32 %9, i32* getelementptr inbounds (%struct.sigevent, %struct.sigevent* @SetTimer.sigev, i32 0, i32 0)
store i32* @SetTimer.tid, i32** getelementptr inbounds (%struct.sigevent, %struct.sigevent* @SetTimer.sigev, i32 0, i32 1, i32 0)
%10 = load i32, i32* @CLOCK_REALTIME
%11 = call i64 @timer_create(i32 %10, %struct.sigevent* @SetTimer.sigev, i32* @SetTimer.tid)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %3
%14 = load i32, i32* %6
%15 = sdiv i32 %14, 1000
store i32 %15, i32* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i32 0, i32 1, i32 0)
%16 = load i32, i32* %6
%17 = srem i32 %16, 1000
%18 = sext i32 %17 to i64
%19 = mul nsw i64 %18, 1000000
store i64 %19, i64* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i32 0, i32 1, i32 1)
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = load i32, i32* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i32 0, i32 1, i32 0)
store i32 %23, i32* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i32 0, i32 0, i32 0)
%24 = load i64, i64* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i32 0, i32 1, i32 1)
store i64 %24, i64* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i32 0, i32 0, i32 1)
br label %26
25: ; preds = %13
store i32 0, i32* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i32 0, i32 0, i32 0)
store i64 0, i64* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i32 0, i32 0, i32 1)
br label %26
26: ; preds = %25, %22
%27 = load i32, i32* @SetTimer.tid
%28 = call i64 @timer_settime(i32 %27, i32 0, %struct.itimerspec* @SetTimer.itval, %struct.itimerspec* @SetTimer.oitval)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %35
33: ; preds = %3
%34 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %37
35: ; preds = %32
%36 = load i32, i32* @SetTimer.tid
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %33
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
%struct.sigevent = type { i32, %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i32* }
%struct.itimerspec = type { %struct.TYPE_6__, %struct.TYPE_5__ }
%struct.TYPE_6__ = type { i32, i64 }
%struct.TYPE_5__ = type { i32, i64 }
@SetTimer.sigev = internal global %struct.sigevent zeroinitializer
@SetTimer.tid = internal global i32 0
@SetTimer.itval = internal global %struct.itimerspec zeroinitializer
@SetTimer.oitval = internal global %struct.itimerspec zeroinitializer
@SIGEV_SIGNAL = global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [20 x i8] c"time_settime error!\00"
@.str.1 = [20 x i8] c"timer_create error!\00"
define i32 @SetTimer(i32 %0, i32 %1, i32 %2) {
%4 = load i32, i32* @SIGEV_SIGNAL
store i32 %4, i32* getelementptr inbounds (%struct.sigevent, %struct.sigevent* @SetTimer.sigev, i64 0, i32 2)
store i32 %0, i32* getelementptr inbounds (%struct.sigevent, %struct.sigevent* @SetTimer.sigev, i64 0, i32 0)
store i32* @SetTimer.tid, i32** getelementptr inbounds (%struct.sigevent, %struct.sigevent* @SetTimer.sigev, i64 0, i32 1, i32 0)
%5 = load i32, i32* @CLOCK_REALTIME
%6 = tail call i64 @timer_create(i32 %5, %struct.sigevent* @SetTimer.sigev, i32* @SetTimer.tid)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %21
8: ; preds = %3
%9 = sdiv i32 %1, 1000
store i32 %9, i32* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i64 0, i32 1, i32 0)
%10 = srem i32 %1, 1000
%11 = mul nsw i32 %10, 1000000
%12 = sext i32 %11 to i64
store i64 %12, i64* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i64 0, i32 1, i32 1)
%13 = icmp eq i32 %2, 1
%14 = select i1 %13, i32 %9, i32 0
%15 = select i1 %13, i64 %12, i64 0
store i32 %14, i32* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i64 0, i32 0, i32 0)
store i64 %15, i64* getelementptr inbounds (%struct.itimerspec, %struct.itimerspec* @SetTimer.itval, i64 0, i32 0, i32 1)
%16 = load i32, i32* @SetTimer.tid
%17 = tail call i64 @timer_settime(i32 %16, i32 0, %struct.itimerspec* @SetTimer.itval, %struct.itimerspec* @SetTimer.oitval)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %8
%20 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %23
21: ; preds = %3
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %25
23: ; preds = %8, %19
%24 = load i32, i32* @SetTimer.tid
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %24, %23 ], [ -1, %21 ]
ret i32 %26
}