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

/* Variables and functions */
int gettimeofday(struct timeval *, int /*<<< orphan*/);
int timeout_to_timespec(int timeout, struct timespec *ts) {
  int sc = timeout / 1000;
  int ns = (timeout % 1000) * 1000 * 1000;
  int ret;
  if (timeout < 0)
    return 1;
  struct timeval now;
  ret = gettimeofday(&now, 0);
  ts->tv_sec = now.tv_sec + sc;
  ts->tv_nsec = now.tv_usec * 1000 + ns;
  ts->tv_sec += ts->tv_nsec / 1000000000L;
  ts->tv_nsec %= 1000000000L;
  return 0;
}


x86 assembly (clang, -O0):
<timeout_to_timespec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    115d <timeout_to_timespec+0x4d>
movl   $0x1,-0x4(%rbp)
jmp    11d2 <timeout_to_timespec+0xc2>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rcx
movslq -0x14(%rbp),%rax
add    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
imul   $0x3e8,-0x30(%rbp),%ecx
add    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movslq (%rax),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    -0x38(%rbp),%rax
mov    %edx,%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeout_to_timespec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
js     1187 <timeout_to_timespec+0x77>
mov    %rsi,%r14
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rbx
shr    $0x26,%rbx
imul   $0x3e8,%ebx,%eax
sub    %eax,%edi
imul   $0xf4240,%edi,%ebp
lea    0x8(%rsp),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
call   1030 <gettimeofday@plt>
add    0x10(%rsp),%rbx
imul   $0x3e8,0x8(%rsp),%eax
add    %ebp,%eax
cltq
imul   $0x44b82fa1,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x3c,%rcx
add    %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rbx
mov    %rbx,0x8(%r14)
imul   $0x3b9aca00,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,(%r14)
jmp    118d <timeout_to_timespec+0x7d>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
%struct.timeval = type { i32, i64 }
define i32 @timeout_to_timespec(i32 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
store i32 %0, i32* %4
store %struct.timespec* %1, %struct.timespec** %5
%10 = load i32, i32* %4
%11 = sdiv i32 %10, 1000
store i32 %11, i32* %6
%12 = load i32, i32* %4
%13 = srem i32 %12, 1000
%14 = mul nsw i32 %13, 1000
%15 = mul nsw i32 %14, 1000
store i32 %15, i32* %7
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 1, i32* %3
br label %50
19: ; preds = %2
%20 = call i32 @gettimeofday(%struct.timeval* %9, i32 0)
store i32 %20, i32* %8
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%22 = load i64, i64* %21
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = add nsw i64 %22, %24
%26 = load %struct.timespec*, %struct.timespec** %5
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %26, i32 0, i32 1
store i64 %25, i64* %27
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%29 = load i32, i32* %28
%30 = mul nsw i32 %29, 1000
%31 = load i32, i32* %7
%32 = add nsw i32 %30, %31
%33 = load %struct.timespec*, %struct.timespec** %5
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load %struct.timespec*, %struct.timespec** %5
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = sext i32 %37 to i64
%39 = sdiv i64 %38, 1000000000
%40 = load %struct.timespec*, %struct.timespec** %5
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %40, i32 0, i32 1
%42 = load i64, i64* %41
%43 = add nsw i64 %42, %39
store i64 %43, i64* %41
%44 = load %struct.timespec*, %struct.timespec** %5
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = sext i32 %46 to i64
%48 = srem i64 %47, 1000000000
%49 = trunc i64 %48 to i32
store i32 %49, i32* %45
store i32 0, i32* %3
br label %50
50: ; preds = %19, %18
%51 = load i32, i32* %3
ret i32 %51
}

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