#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 {
  long tv_sec;
  unsigned int tv_usec;
};

/* Variables and functions */
int EINVAL;
int set_timeout(struct timeval *timeval, unsigned int msec) {
  if (msec == 0)
    return -EINVAL;
  timeval->tv_sec = 0;
  timeval->tv_usec = 0;
  if (msec < 1000) {
    timeval->tv_usec = msec * 1000;
    return 0;
  }
  timeval->tv_sec = (long)(msec / 1000);
  timeval->tv_usec = (long)((msec - (timeval->tv_sec * 1000)) * 1000);
  return 0;
}


x86 assembly (clang, -O0):
<set_timeout>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1128 <set_timeout+0x28>
xor    %eax,%eax
mov    0x2ec2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <set_timeout+0xa4>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movl   $0x0,0x8(%rax)
cmpl   $0x3e8,-0x14(%rbp)
jae    1165 <set_timeout+0x65>
imul   $0x3e8,-0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11a4 <set_timeout+0xa4>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%eax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
imul   $0x3e8,(%rcx),%rcx
sub    %rcx,%rax
imul   $0x3e8,%rax,%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_timeout>:
test   %esi,%esi
je     111b <set_timeout+0x1b>
movq   $0x0,(%rdi)
cmp    $0x3e7,%esi
ja     1127 <set_timeout+0x27>
imul   $0x3e8,%esi,%eax
jmp    1145 <set_timeout+0x45>
mov    0x2ebe(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
ret
mov    %esi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %rax,(%rdi)
imul   $0xfffffc18,%eax,%eax
add    %esi,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0x8(%rdi)
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
@EINVAL = global i32 0
define i32 @set_timeout(%struct.timeval* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval*
%5 = alloca i32
store %struct.timeval* %0, %struct.timeval** %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
store i32 %10, i32* %3
br label %40
11: ; preds = %2
%12 = load %struct.timeval*, %struct.timeval** %4
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i64 0, i64* %13
%14 = load %struct.timeval*, %struct.timeval** %4
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store i32 0, i32* %15
%16 = load i32, i32* %5
%17 = icmp ult i32 %16, 1000
br i1 %17, label %18, label %23
18: ; preds = %11
%19 = load i32, i32* %5
%20 = mul i32 %19, 1000
%21 = load %struct.timeval*, %struct.timeval** %4
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i32 0, i32 1
store i32 %20, i32* %22
store i32 0, i32* %3
br label %40
23: ; preds = %11
%24 = load i32, i32* %5
%25 = udiv i32 %24, 1000
%26 = zext i32 %25 to i64
%27 = load %struct.timeval*, %struct.timeval** %4
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %27, i32 0, i32 0
store i64 %26, i64* %28
%29 = load i32, i32* %5
%30 = zext i32 %29 to i64
%31 = load %struct.timeval*, %struct.timeval** %4
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = mul nsw i64 %33, 1000
%35 = sub nsw i64 %30, %34
%36 = mul nsw i64 %35, 1000
%37 = trunc i64 %36 to i32
%38 = load %struct.timeval*, %struct.timeval** %4
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %38, i32 0, i32 1
store i32 %37, i32* %39
store i32 0, i32* %3
br label %40
40: ; preds = %23, %18, %8
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
@EINVAL = global i32 0
define i32 @set_timeout(%struct.timeval* nocapture writeonly %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @EINVAL
%6 = sub nsw i32 0, %5
br label %19
7: ; preds = %2
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%10 = icmp ult i32 %1, 1000
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = mul nuw nsw i32 %1, 1000
store i32 %12, i32* %9
br label %19
13: ; preds = %7
%14 = udiv i32 %1, 1000
%15 = zext i32 %14 to i64
store i64 %15, i64* %8
%16 = mul i32 %14, -1000
%17 = add i32 %16, %1
%18 = mul i32 %17, 1000
store i32 %18, i32* %9
br label %19
19: ; preds = %13, %11, %4
%20 = phi i32 [ %6, %4 ], [ 0, %11 ], [ 0, %13 ]
ret i32 %20
}