#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  long tv_sec;
  long tv_nsec;
};
struct TYPE_3__ {
  long tv_sec;
  long tv_nsec;
};
struct itimerspec {
  TYPE_2__ it_interval;
  TYPE_1__ it_value;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct itimerspec *, int /*<<< orphan*/, int);
int timerfd_create(int /*<<< orphan*/, int /*<<< orphan*/);
int timerfd_settime(int, int /*<<< orphan*/, struct itimerspec *,
                    int /*<<< orphan*/ *);
int __sysapi_create_timer(long secs, long usec, int interval_timer) {
  struct itimerspec it_sp;
  int ret;
  int timer_fd;
  timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
  if (timer_fd < 0)
    return -1;
  memset(&it_sp, 0, sizeof(struct itimerspec));
  it_sp.it_value.tv_sec = secs;
  it_sp.it_value.tv_nsec = usec * 1000;
  if (interval_timer) {
    it_sp.it_interval.tv_sec = secs;
    it_sp.it_interval.tv_nsec = usec * 1000;
  }
  ret = timerfd_settime(timer_fd, 0, &it_sp, NULL);
  if (ret < 0) {
    close(timer_fd);
    return -1;
  }
  return timer_fd;
}


x86 assembly (clang, -O0):
<__sysapi_create_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <timerfd_create@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    117c <__sysapi_create_timer+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <__sysapi_create_timer+0xb7>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
imul   $0x3e8,-0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11be <__sysapi_create_timer+0x7e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
imul   $0x3e8,-0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%edi
xor    %esi,%esi
lea    -0x40(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <timerfd_settime@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11f1 <__sysapi_create_timer+0xb1>
mov    -0x48(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <__sysapi_create_timer+0xb7>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__sysapi_create_timer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <timerfd_create@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11bd <__sysapi_create_timer+0x7d>
mov    %eax,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    %rbx,0x10(%rsp)
imul   $0x3e8,%r12,%rax
mov    %rax,0x18(%rsp)
test   %r15d,%r15d
je     119f <__sysapi_create_timer+0x5f>
mov    %rbx,(%rsp)
mov    %rax,0x8(%rsp)
mov    %rsp,%rdx
mov    %ebp,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <timerfd_settime@plt>
test   %eax,%eax
js     11b6 <__sysapi_create_timer+0x76>
mov    %ebp,%r14d
jmp    11bd <__sysapi_create_timer+0x7d>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64, i64 }
%struct.TYPE_3__ = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
define i32 @__sysapi_create_timer(i64 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.itimerspec
%9 = alloca i32
%10 = alloca i32
store i64 %0, i64* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i32 @timerfd_create(i32 %11, i32 0)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %45
16: ; preds = %3
%17 = call i32 @memset(%struct.itimerspec* %8, i32 0, i32 32)
%18 = load i64, i64* %5
%19 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %8, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i32 0, i32 0
store i64 %18, i64* %20
%21 = load i64, i64* %6
%22 = mul nsw i64 %21, 1000
%23 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %8, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %23, i32 0, i32 1
store i64 %22, i64* %24
%25 = load i32, i32* %7
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %16
%28 = load i64, i64* %5
%29 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %8, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %29, i32 0, i32 0
store i64 %28, i64* %30
%31 = load i64, i64* %6
%32 = mul nsw i64 %31, 1000
%33 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %8, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %33, i32 0, i32 1
store i64 %32, i64* %34
br label %35
35: ; preds = %27, %16
%36 = load i32, i32* %10
%37 = call i32 @timerfd_settime(i32 %36, i32 0, %struct.itimerspec* %8, i32* null)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* %10
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %4
br label %45
43: ; preds = %35
%44 = load i32, i32* %10
store i32 %44, i32* %4
br label %45
45: ; preds = %43, %40, %15
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64, i64 }
%struct.TYPE_3__ = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
define i32 @__sysapi_create_timer(i64 %0, i64 %1, i32 %2) {
%4 = alloca %struct.itimerspec
%5 = bitcast %struct.itimerspec* %4 to i8*
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = tail call i32 @timerfd_create(i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %3
%10 = call i32 @memset(%struct.itimerspec* %4, i32 0, i32 32)
%11 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %4, i64 0, i32 1, i32 0
store i64 %0, i64* %11
%12 = mul nsw i64 %1, 1000
%13 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %4, i64 0, i32 1, i32 1
store i64 %12, i64* %13
%14 = icmp eq i32 %2, 0
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %4, i64 0, i32 0, i32 0
store i64 %0, i64* %16
%17 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %4, i64 0, i32 0, i32 1
store i64 %12, i64* %17
br label %18
18: ; preds = %15, %9
%19 = call i32 @timerfd_settime(i32 %7, i32 0, %struct.itimerspec* %4, i32* null)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @close(i32 %7)
br label %23
23: ; preds = %18, %3, %21
%24 = phi i32 [ -1, %21 ], [ -1, %3 ], [ %7, %18 ]
ret i32 %24
}