#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;
typedef int /*<<< orphan*/ time_t;
struct TYPE_4__ {
  int /*<<< orphan*/ *sival_ptr;
};
struct sigevent {
  int sigev_signo;
  TYPE_1__ sigev_value;
  int /*<<< orphan*/ sigev_notify;
};
struct TYPE_6__ {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};
struct TYPE_5__ {
  scalar_t__ tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
struct itimerspec {
  TYPE_3__ it_interval;
  TYPE_2__ it_value;
};
typedef int /*<<< orphan*/ sevp;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ Log(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ SIGEV_SIGNAL;
int /*<<< orphan*/ assert(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct sigevent *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ timer_create(int /*<<< orphan*/, struct sigevent *,
                        int /*<<< orphan*/ *);
scalar_t__ timer_settime(int /*<<< orphan*/, int /*<<< orphan*/,
                         struct itimerspec *, int /*<<< orphan*/ *);
int start_watchdog(time_t duration, int signal, timer_t *timerid) {
  struct sigevent sevp;
  struct itimerspec when;
  assert(timerid);
  memset(&sevp, 0, sizeof(sevp));
  sevp.sigev_notify = SIGEV_SIGNAL;
  sevp.sigev_signo = signal;
  sevp.sigev_value.sival_ptr = timerid;
  if (timer_create(CLOCK_REALTIME, &sevp, timerid) < 0) {
    Log(4, "Failed to create watchdog timer:%s", strerror(errno));
    return -1;
  }
  when.it_value.tv_sec = duration;
  when.it_value.tv_nsec = 0;
  when.it_interval.tv_sec = 0;
  when.it_interval.tv_nsec = 0;
  if (timer_settime(*timerid, 0, &when, NULL) < 0) {
    Log(4, "Failed to set watchdog timer:%s", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<start_watchdog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <assert@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rsi
call   1040 <timer_create@plt>
cmp    $0x0,%rax
jge    11f2 <start_watchdog+0x92>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    $0x4,%edi
lea    0xe1f(%rip),%rsi
call   1070 <Log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <start_watchdog+0x103>
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x50(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <timer_settime@plt>
cmp    $0x0,%rax
jge    125c <start_watchdog+0xfc>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    $0x4,%edi
lea    0xdd8(%rip),%rsi
call   1070 <Log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <start_watchdog+0x103>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_watchdog>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r15d
mov    %rdx,%rdi
call   1060 <assert@plt>
lea    0x8(%rsp),%r12
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <timer_create@plt>
test   %rax,%rax
js     11f6 <start_watchdog+0x96>
mov    %r15d,0x38(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movq   $0x0,0x30(%rsp)
mov    (%rbx),%edi
lea    0x20(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1030 <timer_settime@plt>
test   %rax,%rax
jns    1220 <start_watchdog+0xc0>
lea    0xe2f(%rip),%rbx
jmp    11fd <start_watchdog+0x9d>
lea    0xe03(%rip),%rbx
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    $0x4,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <Log@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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 { i64, i64 }
%struct.TYPE_5__ = type { i64, i32 }
@SIGEV_SIGNAL = global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [35 x i8] c"Failed to create watchdog timer:%s\00"
@errno = global i32 0
@.str.1 = [32 x i8] c"Failed to set watchdog timer:%s\00"
define i32 @start_watchdog(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.sigevent
%9 = alloca %struct.itimerspec
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i32*, i32** %7
%11 = call i32 @assert(i32* %10)
%12 = call i32 @memset(%struct.sigevent* %8, i32 0, i32 24)
%13 = load i32, i32* @SIGEV_SIGNAL
%14 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %8, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* %6
%16 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %8, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32*, i32** %7
%18 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %8, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 0
store i32* %17, i32** %19
%20 = load i32, i32* @CLOCK_REALTIME
%21 = load i32*, i32** %7
%22 = call i64 @timer_create(i32 %20, %struct.sigevent* %8, i32* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %3
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @Log(i32 4, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 -1, i32* %4
br label %47
28: ; preds = %3
%29 = load i32, i32* %5
%30 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %9, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %30, i32 0, i32 1
store i32 %29, i32* %31
%32 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %9, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %32, i32 0, i32 0
store i64 0, i64* %33
%34 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %9, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %34, i32 0, i32 1
store i64 0, i64* %35
%36 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %9, i32 0, i32 0
%37 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %36, i32 0, i32 0
store i64 0, i64* %37
%38 = load i32*, i32** %7
%39 = load i32, i32* %38
%40 = call i64 @timer_settime(i32 %39, i32 0, %struct.itimerspec* %9, i32* null)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %28
%43 = load i32, i32* @errno
%44 = call i32 @strerror(i32 %43)
%45 = call i32 @Log(i32 4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %44)
store i32 -1, i32* %4
br label %47
46: ; preds = %28
store i32 0, i32* %4
br label %47
47: ; preds = %46, %42, %24
%48 = load i32, i32* %4
ret i32 %48
}

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 { i64, i64 }
%struct.TYPE_5__ = type { i64, i32 }
@SIGEV_SIGNAL = global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [35 x i8] c"Failed to create watchdog timer:%s\00"
@errno = global i32 0
@.str.1 = [32 x i8] c"Failed to set watchdog timer:%s\00"
define i32 @start_watchdog(i32 %0, i32 %1, i32* %2) {
%4 = alloca %struct.sigevent
%5 = alloca %struct.itimerspec
%6 = bitcast %struct.sigevent* %4 to i8*
%7 = bitcast %struct.itimerspec* %5 to i8*
%8 = tail call i32 @assert(i32* %2)
%9 = call i32 @memset(%struct.sigevent* %4, i32 0, i32 24)
%10 = load i32, i32* @SIGEV_SIGNAL
%11 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i64 0, i32 2
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i64 0, i32 0
store i32 %1, i32* %12
%13 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i64 0, i32 1, i32 0
store i32* %2, i32** %13
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i64 @timer_create(i32 %14, %struct.sigevent* %4, i32* %2)
%16 = icmp slt i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %3
%18 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %5, i64 0, i32 1, i32 1
store i32 %0, i32* %18
%19 = bitcast %struct.itimerspec* %5 to i8*
%20 = load i32, i32* %2
%21 = call i64 @timer_settime(i32 %20, i32 0, %struct.itimerspec* %5, i32* null)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %17, %3
%24 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %17 ]
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @Log(i32 4, i8* %24, i32 %26)
br label %28
28: ; preds = %23, %17
%29 = phi i32 [ 0, %17 ], [ -1, %23 ]
ret i32 %29
}