#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*/);
long elapsed_time(void) {
  long r = 0;
  static struct timeval t0;
  struct timeval tv;
  gettimeofday(&tv, 0);
  if (!t0.tv_sec || !t0.tv_usec)
    t0 = tv;
  r = (tv.tv_sec - t0.tv_sec) * 1000;
  r += (tv.tv_usec - t0.tv_usec) / 1000;
  t0 = tv;
  return r;
}


x86 assembly (clang, -O0):
<elapsed_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmpl   $0x0,0x2efa(%rip)
je     1145 <elapsed_time+0x35>
cmpl   $0x0,0x2ef1(%rip)
jne    1150 <elapsed_time+0x40>
mov    -0x10(%rbp),%rax
mov    %rax,0x2edc(%rip)
mov    -0x10(%rbp),%eax
sub    0x2ed3(%rip),%eax
imul   $0x3e8,%eax,%eax
cltq
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
sub    0x2ec2(%rip),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cltq
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,0x2ea1(%rip)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elapsed_time>:
push   %rax
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x2f0f(%rip),%eax
test   %eax,%eax
je     113c <elapsed_time+0x2c>
mov    0x2f09(%rip),%edx
test   %edx,%edx
je     113c <elapsed_time+0x2c>
mov    (%rsp),%edi
mov    0x4(%rsp),%esi
mov    (%rsp),%rcx
jmp    114d <elapsed_time+0x3d>
mov    (%rsp),%rcx
mov    %rcx,%rdx
shr    $0x20,%rdx
mov    %edx,%esi
mov    %ecx,%eax
mov    %ecx,%edi
sub    %edx,%esi
sub    %eax,%edi
imul   $0x3e8,%edi,%eax
movslq %eax,%rdx
movslq %esi,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rax
add    %esi,%eax
cltq
add    %rdx,%rax
mov    %rcx,0x2eb3(%rip)
pop    %rcx
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@elapsed_time.t0 = internal unnamed_addr global %struct.timeval zeroinitializer
define i64 @elapsed_time() {
%1 = alloca i64
%2 = bitcast i64* %1 to %struct.timeval*
%3 = bitcast i64* %1 to i8*
%4 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%5 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @elapsed_time.t0, i64 0, i32 0)
%6 = icmp ne i32 %5, 0
%7 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @elapsed_time.t0, i64 0, i32 1)
%8 = icmp ne i32 %7, 0
%9 = select i1 %6, i1 %8, i1 false
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = bitcast i64* %1 to i32*
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%14 = load i32, i32* %13
%15 = load i64, i64* %1
br label %21
16: ; preds = %0
%17 = load i64, i64* %1
%18 = trunc i64 %17 to i32
%19 = lshr i64 %17, 32
%20 = trunc i64 %19 to i32
br label %21
21: ; preds = %10, %16
%22 = phi i64 [ %15, %10 ], [ %17, %16 ]
%23 = phi i32 [ %7, %10 ], [ %20, %16 ]
%24 = phi i32 [ %14, %10 ], [ %20, %16 ]
%25 = phi i32 [ %5, %10 ], [ %18, %16 ]
%26 = phi i32 [ %12, %10 ], [ %18, %16 ]
%27 = sub nsw i32 %26, %25
%28 = mul nsw i32 %27, 1000
%29 = sext i32 %28 to i64
%30 = sub nsw i32 %24, %23
%31 = sdiv i32 %30, 1000
%32 = sext i32 %31 to i64
%33 = add nsw i64 %32, %29
store i64 %22, i64* bitcast (%struct.timeval* @elapsed_time.t0 to i64*)
ret i64 %33
}