#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 timeval {
  int tv_sec;
  int tv_usec;
};
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void set_timeout(struct timespec *ts, int usec) {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  ts->tv_sec = tv.tv_sec;
  int us = usec + tv.tv_usec;
  if (us >= 1000 * 1000) {
    ts->tv_nsec = (us % 1000 * 1000) * 1000;
    ts->tv_sec += 1;
  } else {
    ts->tv_nsec = us * 1000;
  }
}


x86 assembly (clang, -O0):
<set_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0xf4240,-0x1c(%rbp)
jl     1179 <set_timeout+0x69>
mov    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
imul   $0x3e8,%eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1187 <set_timeout+0x77>
imul   $0x3e8,-0x1c(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_timeout>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    (%rsp),%eax
mov    %eax,(%rbx)
add    0x4(%rsp),%ebp
cmp    $0xf4240,%ebp
jl     1155 <set_timeout+0x45>
mov    %ebp,%ecx
imul   $0x10624dd3,%rcx,%rcx
shr    $0x26,%rcx
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%ebp
imul   $0xf4240,%ebp,%ecx
add    $0x1,%eax
mov    %eax,(%rbx)
jmp    115b <set_timeout+0x4b>
imul   $0x3e8,%ebp,%ecx
mov    %ecx,0x4(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
define void @set_timeout(%struct.timespec* %0, i32 %1) {
%3 = alloca %struct.timespec*
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
store %struct.timespec* %0, %struct.timespec** %3
store i32 %1, i32* %4
%7 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load %struct.timespec*, %struct.timespec** %3
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = load i32, i32* %4
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%14 = load i32, i32* %13
%15 = add nsw i32 %12, %14
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp sge i32 %16, 1000000
br i1 %17, label %18, label %29
18: ; preds = %2
%19 = load i32, i32* %6
%20 = srem i32 %19, 1000
%21 = mul nsw i32 %20, 1000
%22 = mul nsw i32 %21, 1000
%23 = load %struct.timespec*, %struct.timespec** %3
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %23, i32 0, i32 1
store i32 %22, i32* %24
%25 = load %struct.timespec*, %struct.timespec** %3
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = add nsw i32 %27, 1
store i32 %28, i32* %26
br label %34
29: ; preds = %2
%30 = load i32, i32* %6
%31 = mul nsw i32 %30, 1000
%32 = load %struct.timespec*, %struct.timespec** %3
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %32, i32 0, i32 1
store i32 %31, i32* %33
br label %34
34: ; preds = %29, %18
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
define void @set_timeout(%struct.timespec* nocapture %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%10 = load i32, i32* %9
%11 = add nsw i32 %10, %1
%12 = icmp sgt i32 %11, 999999
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = urem i32 %11, 1000
%15 = mul nuw nsw i32 %14, 1000000
%16 = add nsw i32 %7, 1
store i32 %16, i32* %8
br label %19
17: ; preds = %2
%18 = mul nsw i32 %11, 1000
br label %19
19: ; preds = %17, %13
%20 = phi i32 [ %15, %13 ], [ %18, %17 ]
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
store i32 %20, i32* %21
ret void
}