#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*/ *);
unsigned long time_delta(struct timeval *t) {
  struct timeval now;
  if (t == NULL)
    return 0;
  gettimeofday(&now, NULL);
  return (now.tv_sec - t->tv_sec) * 1000 + (now.tv_usec - t->tv_usec) / 1000;
}


x86 assembly (clang, -O0):
<time_delta>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1134 <time_delta+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1172 <time_delta+0x62>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
sub    0x4(%rcx),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_delta>:
test   %rdi,%rdi
je     115e <time_delta+0x4e>
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
sub    (%rbx),%eax
sub    0x4(%rbx),%ecx
imul   $0x3e8,%eax,%eax
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
movslq %ecx,%rax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i64 @time_delta(%struct.timeval* %0) {
%2 = alloca i64
%3 = alloca %struct.timeval*
%4 = alloca %struct.timeval
store %struct.timeval* %0, %struct.timeval** %3
%5 = load %struct.timeval*, %struct.timeval** %3
%6 = icmp eq %struct.timeval* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
store i64 0, i64* %2
br label %26
8: ; preds = %1
%9 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = load %struct.timeval*, %struct.timeval** %3
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = sub nsw i32 %11, %14
%16 = mul nsw i32 %15, 1000
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load %struct.timeval*, %struct.timeval** %3
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = sub nsw i32 %18, %21
%23 = sdiv i32 %22, 1000
%24 = add nsw i32 %16, %23
%25 = sext i32 %24 to i64
store i64 %25, i64* %2
br label %26
26: ; preds = %8, %7
%27 = load i64, i64* %2
ret i64 %27
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i64 @time_delta(%struct.timeval* %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = icmp eq %struct.timeval* %0, null
br i1 %4, label %21, label %5
5: ; preds = %1
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%10 = load i32, i32* %9
%11 = sub nsw i32 %8, %10
%12 = mul nsw i32 %11, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%16 = load i32, i32* %15
%17 = sub nsw i32 %14, %16
%18 = sdiv i32 %17, 1000
%19 = add nsw i32 %18, %12
%20 = sext i32 %19 to i64
br label %21
21: ; preds = %1, %5
%22 = phi i64 [ %20, %5 ], [ 0, %1 ]
ret i64 %22
}