#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 */
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_err(int /*<<< orphan*/, char *);
int _tpoll_diff_timeval(struct timeval *tvp1, struct timeval *tvp0) {
  struct timeval tv;
  int ms;
  if (!tvp0 || !tvp1) {
    if (gettimeofday(&tv, NULL) < 0) {
      log_err(0, "Unable to get time of day");
    }
    if (!tvp0) {
      tvp0 = &tv;
    }
    if (!tvp1) {
      tvp1 = &tv;
    }
  }
  ms = ((tvp1->tv_sec - tvp0->tv_sec) * 1000) +
       ((tvp1->tv_usec - tvp0->tv_usec) / 1000);
  if ((tvp1->tv_sec >= tvp0->tv_sec) && (tvp1->tv_usec > tvp0->tv_usec)) {
    ms++;
  } else if ((tvp1->tv_sec <= tvp0->tv_sec) &&
             (tvp1->tv_usec < tvp0->tv_usec)) {
    ms--;
  }
  return (ms);
}


x86 assembly (clang, -O0):
<_tpoll_diff_timeval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <_tpoll_diff_timeval+0x26>
cmpq   $0x0,-0x8(%rbp)
jne    1196 <_tpoll_diff_timeval+0x76>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jge    116b <_tpoll_diff_timeval+0x4b>
xor    %edi,%edi
lea    0xe9a(%rip),%rsi
call   1040 <log_err@plt>
cmpq   $0x0,-0x10(%rbp)
jne    117e <_tpoll_diff_timeval+0x5e>
lea    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1191 <_tpoll_diff_timeval+0x71>
lea    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1196 <_tpoll_diff_timeval+0x76>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x10(%rbp),%rcx
sub    0x4(%rcx),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jl     11ff <_tpoll_diff_timeval+0xdf>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x10(%rbp),%rcx
cmp    0x4(%rcx),%eax
jle    11ff <_tpoll_diff_timeval+0xdf>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1233 <_tpoll_diff_timeval+0x113>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jg     122e <_tpoll_diff_timeval+0x10e>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x10(%rbp),%rcx
cmp    0x4(%rcx),%eax
jge    122e <_tpoll_diff_timeval+0x10e>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1233 <_tpoll_diff_timeval+0x113>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tpoll_diff_timeval>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
je     1139 <_tpoll_diff_timeval+0x19>
test   %r14,%r14
jne    1169 <_tpoll_diff_timeval+0x49>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jns    115b <_tpoll_diff_timeval+0x3b>
lea    0xeac(%rip),%rsi
xor    %edi,%edi
call   1040 <log_err@plt>
test   %r14,%r14
cmove  %r15,%r14
test   %rbx,%rbx
cmove  %r15,%rbx
mov    (%rbx),%esi
mov    0x4(%rbx),%ecx
mov    (%r14),%edi
mov    0x4(%r14),%edx
mov    %esi,%eax
sub    %edi,%eax
imul   $0x3e8,%eax,%r8d
mov    %ecx,%eax
sub    %edx,%eax
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rbx
shr    $0x3f,%rbx
sar    $0x26,%rax
add    %ebx,%eax
add    %r8d,%eax
cmp    %edi,%esi
jl     11aa <_tpoll_diff_timeval+0x8a>
cmp    %edx,%ecx
jle    11aa <_tpoll_diff_timeval+0x8a>
add    $0x1,%eax
jmp    11b8 <_tpoll_diff_timeval+0x98>
cmp    %edi,%esi
jg     11b8 <_tpoll_diff_timeval+0x98>
xor    %esi,%esi
cmp    %edx,%ecx
setl   %sil
sub    %esi,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [26 x i8] c"Unable to get time of day\00"
define i32 @_tpoll_diff_timeval(%struct.timeval* %0, %struct.timeval* %1) {
%3 = alloca %struct.timeval*
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval
%6 = alloca i32
store %struct.timeval* %0, %struct.timeval** %3
store %struct.timeval* %1, %struct.timeval** %4
%7 = load %struct.timeval*, %struct.timeval** %4
%8 = icmp ne %struct.timeval* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load %struct.timeval*, %struct.timeval** %3
%11 = icmp ne %struct.timeval* %10, null
br i1 %11, label %26, label %12
12: ; preds = %9, %2
%13 = call i64 @gettimeofday(%struct.timeval* %5, i32* null)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @log_err(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load %struct.timeval*, %struct.timeval** %4
%19 = icmp ne %struct.timeval* %18, null
br i1 %19, label %21, label %20
20: ; preds = %17
store %struct.timeval* %5, %struct.timeval** %4
br label %21
21: ; preds = %20, %17
%22 = load %struct.timeval*, %struct.timeval** %3
%23 = icmp ne %struct.timeval* %22, null
br i1 %23, label %25, label %24
24: ; preds = %21
store %struct.timeval* %5, %struct.timeval** %3
br label %25
25: ; preds = %24, %21
br label %26
26: ; preds = %25, %9
%27 = load %struct.timeval*, %struct.timeval** %3
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load %struct.timeval*, %struct.timeval** %4
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = sub nsw i32 %29, %32
%34 = mul nsw i32 %33, 1000
%35 = load %struct.timeval*, %struct.timeval** %3
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.timeval*, %struct.timeval** %4
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = sub nsw i32 %37, %40
%42 = sdiv i32 %41, 1000
%43 = add nsw i32 %34, %42
store i32 %43, i32* %6
%44 = load %struct.timeval*, %struct.timeval** %3
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load %struct.timeval*, %struct.timeval** %4
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = icmp sge i32 %46, %49
br i1 %50, label %51, label %62
51: ; preds = %26
%52 = load %struct.timeval*, %struct.timeval** %3
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %52, i32 0, i32 1
%54 = load i32, i32* %53
%55 = load %struct.timeval*, %struct.timeval** %4
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %55, i32 0, i32 1
%57 = load i32, i32* %56
%58 = icmp sgt i32 %54, %57
br i1 %58, label %59, label %62
59: ; preds = %51
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
store i32 %61, i32* %6
br label %82
62: ; preds = %51, %26
%63 = load %struct.timeval*, %struct.timeval** %3
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = load %struct.timeval*, %struct.timeval** %4
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = icmp sle i32 %65, %68
br i1 %69, label %70, label %81
70: ; preds = %62
%71 = load %struct.timeval*, %struct.timeval** %3
%72 = getelementptr inbounds %struct.timeval, %struct.timeval* %71, i32 0, i32 1
%73 = load i32, i32* %72
%74 = load %struct.timeval*, %struct.timeval** %4
%75 = getelementptr inbounds %struct.timeval, %struct.timeval* %74, i32 0, i32 1
%76 = load i32, i32* %75
%77 = icmp slt i32 %73, %76
br i1 %77, label %78, label %81
78: ; preds = %70
%79 = load i32, i32* %6
%80 = add nsw i32 %79, -1
store i32 %80, i32* %6
br label %81
81: ; preds = %78, %70, %62
br label %82
82: ; preds = %81, %59
%83 = load i32, i32* %6
ret i32 %83
}

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