#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 timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};
typedef scalar_t__ int64_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int megahal_timeout(struct timespec start) {
  struct timespec now;
  int64_t timeout = 1000000000;
  int ret;
  ret = clock_gettime(CLOCK_MONOTONIC, &now);
  if (ret)
    return 1;
  timeout -=
      (now.tv_sec - start.tv_sec) * 1000000000 + (now.tv_nsec - start.tv_nsec);
  return timeout <= 0;
}


x86 assembly (clang, -O0):
<megahal_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x18(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x3b9aca00,-0x30(%rbp)
3b
mov    0x2eba(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1152 <megahal_timeout+0x42>
movl   $0x1,-0x4(%rbp)
jmp    1187 <megahal_timeout+0x77>
mov    -0x28(%rbp),%eax
sub    -0x18(%rbp),%eax
imul   $0x3b9aca00,%eax,%eax
movslq %eax,%rcx
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
add    %rax,%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<megahal_timeout>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ec6(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    1162 <megahal_timeout+0x52>
movslq 0x8(%rsp),%rax
movslq %ebp,%rcx
sub    %rcx,%rax
imul   $0x3b9aca00,%rax,%rax
mov    0x10(%rsp),%rcx
sub    %rbx,%rcx
add    %rax,%rcx
xor    %eax,%eax
cmp    $0x3b9aca00,%rcx
setge  %al
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
define i32 @megahal_timeout(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca i64
%7 = alloca i32
%8 = bitcast %struct.timespec* %4 to { i32, i64 }*
%9 = getelementptr inbounds { i32, i64 }, { i32, i64 }* %8, i32 0, i32 0
store i32 %0, i32* %9
%10 = getelementptr inbounds { i32, i64 }, { i32, i64 }* %8, i32 0, i32 1
store i64 %1, i64* %10
store i64 1000000000, i64* %6
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %5)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 1, i32* %3
br label %35
16: ; preds = %2
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = sub nsw i32 %18, %20
%22 = mul nsw i32 %21, 1000000000
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%25 = load i64, i64* %24
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%27 = load i64, i64* %26
%28 = sub nsw i64 %25, %27
%29 = add nsw i64 %23, %28
%30 = load i64, i64* %6
%31 = sub nsw i64 %30, %29
store i64 %31, i64* %6
%32 = load i64, i64* %6
%33 = icmp sle i64 %32, 0
%34 = zext i1 %33 to i32
store i32 %34, i32* %3
br label %35
35: ; preds = %16, %15
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
define i32 @megahal_timeout(i32 %0, i64 %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = load i32, i32* @CLOCK_MONOTONIC
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %20
8: ; preds = %2
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = sub nsw i32 %10, %0
%12 = mul nsw i32 %11, 1000000000
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%15 = load i64, i64* %14
%16 = sub i64 %15, %1
%17 = add nsw i64 %16, %13
%18 = icmp sgt i64 %17, 999999999
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %2, %8
%21 = phi i32 [ %19, %8 ], [ 1, %2 ]
ret i32 %21
}