#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;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ timeradd(struct timeval *, struct timeval *,
                            struct timeval *);
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
int ap_utils_timeval_set(struct timeval *tv, int mode, int msec) {
  struct timeval tmp;
  if (msec < 1)
    return 0;
  switch (mode) {
  case 0:
    tmp.tv_sec = msec / 1000;
    tmp.tv_usec = 1000 * (msec % 1000);
    timeradd(tv, &tmp, tv);
    break;
  case 1:
    tmp.tv_sec = msec / 1000;
    tmp.tv_usec = 1000 * (msec % 1000);
    timersub(tv, &tmp, tv);
    break;
  case 2:
    gettimeofday(tv, NULL);
    tmp.tv_sec = msec / 1000;
    tmp.tv_usec = 1000 * (msec % 1000);
    timeradd(tv, &tmp, tv);
    break;
  case 3:
    tv->tv_sec = msec / 1000;
    tv->tv_usec = 1000 * (msec % 1000);
    break;
  default:
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<ap_utils_timeval_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jge    1158 <ap_utils_timeval_set+0x28>
movl   $0x0,-0x4(%rbp)
jmp    1275 <ap_utils_timeval_set+0x145>
mov    -0x14(%rbp),%eax
mov    %rax,-0x28(%rbp)
sub    $0x3,%rax
ja     1262 <ap_utils_timeval_set+0x132>
mov    -0x28(%rbp),%rax
lea    0xe8c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rsi
call   1030 <timeradd@plt>
jmp    126e <ap_utils_timeval_set+0x13e>
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rsi
call   1050 <timersub@plt>
jmp    126e <ap_utils_timeval_set+0x13e>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rsi
call   1030 <timeradd@plt>
jmp    126e <ap_utils_timeval_set+0x13e>
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
jmp    126e <ap_utils_timeval_set+0x13e>
movl   $0x0,-0x4(%rbp)
jmp    1275 <ap_utils_timeval_set+0x145>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ap_utils_timeval_set>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
test   %edx,%edx
jle    11f0 <ap_utils_timeval_set+0xc0>
cmp    $0x3,%esi
ja     11f0 <ap_utils_timeval_set+0xc0>
mov    %edx,%ebp
mov    %rdi,%rbx
mov    %esi,%eax
lea    0xeac(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%ebp
imul   $0x3e8,%ebp,%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %rbx,%rdx
call   1050 <timersub@plt>
jmp    11eb <ap_utils_timeval_set+0xbb>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%ebp
imul   $0x3e8,%ebp,%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %rbx,%rdx
call   1030 <timeradd@plt>
jmp    11eb <ap_utils_timeval_set+0xbb>
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,(%rbx)
imul   $0x3e8,%eax,%eax
sub    %eax,%ebp
imul   $0x3e8,%ebp,%eax
mov    %eax,0x4(%rbx)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @ap_utils_timeval_set(%struct.timeval* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
store %struct.timeval* %0, %struct.timeval** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 0, i32* %4
br label %61
12: ; preds = %3
%13 = load i32, i32* %6
switch i32 %13, label %59 [
i32 0, label %14
i32 1, label %25
i32 2, label %36
i32 3, label %49
]
14: ; preds = %12
%15 = load i32, i32* %7
%16 = sdiv i32 %15, 1000
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %7
%19 = srem i32 %18, 1000
%20 = mul nsw i32 1000, %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i32 %20, i32* %21
%22 = load %struct.timeval*, %struct.timeval** %5
%23 = load %struct.timeval*, %struct.timeval** %5
%24 = call i32 @timeradd(%struct.timeval* %22, %struct.timeval* %8, %struct.timeval* %23)
br label %60
25: ; preds = %12
%26 = load i32, i32* %7
%27 = sdiv i32 %26, 1000
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %7
%30 = srem i32 %29, 1000
%31 = mul nsw i32 1000, %30
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i32 %31, i32* %32
%33 = load %struct.timeval*, %struct.timeval** %5
%34 = load %struct.timeval*, %struct.timeval** %5
%35 = call i32 @timersub(%struct.timeval* %33, %struct.timeval* %8, %struct.timeval* %34)
br label %60
36: ; preds = %12
%37 = load %struct.timeval*, %struct.timeval** %5
%38 = call i32 @gettimeofday(%struct.timeval* %37, i32* null)
%39 = load i32, i32* %7
%40 = sdiv i32 %39, 1000
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %7
%43 = srem i32 %42, 1000
%44 = mul nsw i32 1000, %43
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i32 %44, i32* %45
%46 = load %struct.timeval*, %struct.timeval** %5
%47 = load %struct.timeval*, %struct.timeval** %5
%48 = call i32 @timeradd(%struct.timeval* %46, %struct.timeval* %8, %struct.timeval* %47)
br label %60
49: ; preds = %12
%50 = load i32, i32* %7
%51 = sdiv i32 %50, 1000
%52 = load %struct.timeval*, %struct.timeval** %5
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %52, i32 0, i32 0
store i32 %51, i32* %53
%54 = load i32, i32* %7
%55 = srem i32 %54, 1000
%56 = mul nsw i32 1000, %55
%57 = load %struct.timeval*, %struct.timeval** %5
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %57, i32 0, i32 1
store i32 %56, i32* %58
br label %60
59: ; preds = %12
store i32 0, i32* %4
br label %61
60: ; preds = %49, %36, %25, %14
store i32 1, i32* %4
br label %61
61: ; preds = %60, %59, %11
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @ap_utils_timeval_set(%struct.timeval* %0, i32 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %4 to i8*
%6 = icmp slt i32 %2, 1
br i1 %6, label %36, label %7
7: ; preds = %3
switch i32 %1, label %36 [
i32 0, label %8
i32 1, label %15
i32 2, label %22
i32 3, label %30
]
8: ; preds = %7
%9 = udiv i32 %2, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %9, i32* %10
%11 = urem i32 %2, 1000
%12 = mul nuw nsw i32 %11, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %12, i32* %13
%14 = call i32 @timeradd(%struct.timeval* %0, %struct.timeval* %4, %struct.timeval* %0)
br label %36
15: ; preds = %7
%16 = udiv i32 %2, 1000
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %16, i32* %17
%18 = urem i32 %2, 1000
%19 = mul nuw nsw i32 %18, 1000
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %19, i32* %20
%21 = call i32 @timersub(%struct.timeval* %0, %struct.timeval* %4, %struct.timeval* %0)
br label %36
22: ; preds = %7
%23 = tail call i32 @gettimeofday(%struct.timeval* %0, i32* null)
%24 = udiv i32 %2, 1000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %24, i32* %25
%26 = urem i32 %2, 1000
%27 = mul nuw nsw i32 %26, 1000
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %27, i32* %28
%29 = call i32 @timeradd(%struct.timeval* %0, %struct.timeval* %4, %struct.timeval* %0)
br label %36
30: ; preds = %7
%31 = udiv i32 %2, 1000
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
store i32 %31, i32* %32
%33 = urem i32 %2, 1000
%34 = mul nuw nsw i32 %33, 1000
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
store i32 %34, i32* %35
br label %36
36: ; preds = %8, %15, %22, %30, %7, %3
%37 = phi i32 [ 0, %3 ], [ 0, %7 ], [ 1, %30 ], [ 1, %22 ], [ 1, %15 ], [ 1, %8 ]
ret i32 %37
}