#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*/ assert(int /*<<< orphan*/);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_err(int /*<<< orphan*/, char *);
void _tpoll_get_timeval(struct timeval *tvp, int ms) {
  assert(tvp != NULL);
  if (gettimeofday(tvp, NULL) < 0) {
    log_err(0, "Unable to get time of day");
  }
  if (ms > 0) {
    tvp->tv_sec += ms / 1000;
    tvp->tv_usec += (ms % 1000) * 1000;
    if (tvp->tv_usec >= 1000000) {
      tvp->tv_sec += tvp->tv_usec / 1000000;
      tvp->tv_usec %= 1000000;
    }
  }
  return;
}


x86 assembly (clang, -O0):
<_tpoll_get_timeval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jge    1176 <_tpoll_get_timeval+0x46>
xor    %edi,%edi
lea    0xe8f(%rip),%rsi
call   1050 <log_err@plt>
cmpl   $0x0,-0xc(%rbp)
jle    11f9 <_tpoll_get_timeval+0xc9>
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%ecx
mov    -0x8(%rbp),%rax
add    0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0xf4240,0x4(%rax)
jl     11f4 <_tpoll_get_timeval+0xc4>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x4(%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
mov    %edx,0x4(%rax)
jmp    11f9 <_tpoll_get_timeval+0xc9>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tpoll_get_timeval>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jns    1163 <_tpoll_get_timeval+0x33>
lea    0xea4(%rip),%rsi
xor    %edi,%edi
call   1050 <log_err@plt>
test   %ebp,%ebp
jle    11b0 <_tpoll_get_timeval+0x80>
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
add    (%rbx),%eax
mov    %eax,(%rbx)
sub    %ecx,%ebp
imul   $0x3e8,%ebp,%ecx
add    0x4(%rbx),%ecx
mov    %ecx,0x4(%rbx)
cmp    $0xf4240,%ecx
jl     11b0 <_tpoll_get_timeval+0x80>
mov    %ecx,%edx
imul   $0x431bde83,%rdx,%rdx
shr    $0x32,%rdx
add    %edx,%eax
mov    %eax,(%rbx)
imul   $0xf4240,%edx,%eax
sub    %eax,%ecx
mov    %ecx,0x4(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [26 x i8] c"Unable to get time of day\00"
define void @_tpoll_get_timeval(%struct.timeval* %0, i32 %1) {
%3 = alloca %struct.timeval*
%4 = alloca i32
store %struct.timeval* %0, %struct.timeval** %3
store i32 %1, i32* %4
%5 = load %struct.timeval*, %struct.timeval** %3
%6 = icmp ne %struct.timeval* %5, null
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load %struct.timeval*, %struct.timeval** %3
%10 = call i64 @gettimeofday(%struct.timeval* %9, i32* null)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @log_err(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %49
17: ; preds = %14
%18 = load i32, i32* %4
%19 = sdiv i32 %18, 1000
%20 = load %struct.timeval*, %struct.timeval** %3
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = add nsw i32 %22, %19
store i32 %23, i32* %21
%24 = load i32, i32* %4
%25 = srem i32 %24, 1000
%26 = mul nsw i32 %25, 1000
%27 = load %struct.timeval*, %struct.timeval** %3
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = add nsw i32 %29, %26
store i32 %30, i32* %28
%31 = load %struct.timeval*, %struct.timeval** %3
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = icmp sge i32 %33, 1000000
br i1 %34, label %35, label %48
35: ; preds = %17
%36 = load %struct.timeval*, %struct.timeval** %3
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = sdiv i32 %38, 1000000
%40 = load %struct.timeval*, %struct.timeval** %3
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = add nsw i32 %42, %39
store i32 %43, i32* %41
%44 = load %struct.timeval*, %struct.timeval** %3
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %44, i32 0, i32 1
%46 = load i32, i32* %45
%47 = srem i32 %46, 1000000
store i32 %47, i32* %45
br label %48
48: ; preds = %35, %17
br label %49
49: ; preds = %48, %14
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [26 x i8] c"Unable to get time of day\00"
define void @_tpoll_get_timeval(%struct.timeval* %0, i32 %1) {
%3 = icmp ne %struct.timeval* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = tail call i64 @gettimeofday(%struct.timeval* %0, i32* null)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @log_err(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = icmp sgt i32 %1, 0
br i1 %11, label %12, label %27
12: ; preds = %10
%13 = udiv i32 %1, 1000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%15 = load i32, i32* %14
%16 = add nsw i32 %15, %13
store i32 %16, i32* %14
%17 = urem i32 %1, 1000
%18 = mul nuw nsw i32 %17, 1000
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%20 = load i32, i32* %19
%21 = add nsw i32 %20, %18
store i32 %21, i32* %19
%22 = icmp sgt i32 %21, 999999
br i1 %22, label %23, label %27
23: ; preds = %12
%24 = udiv i32 %21, 1000000
%25 = add nsw i32 %24, %16
store i32 %25, i32* %14
%26 = urem i32 %21, 1000000
store i32 %26, i32* %19
br label %27
27: ; preds = %12, %23, %10
ret void
}