#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_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
long timer(int reset) {
  static long start = 0;
  struct timeval tv;
  gettimeofday(&tv, NULL);
  if (!reset) {
    long stop = ((long)tv.tv_sec) * 1000 + tv.tv_usec / 1000;
    return (stop - start);
  }
  start = ((long)tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  return 0;
}


x86 assembly (clang, -O0):
<timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmpl   $0x0,-0xc(%rbp)
jne    116d <timer+0x5d>
imul   $0x3e8,-0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    0x2ecc(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <timer+0x8f>
imul   $0x3e8,-0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e99(%rip)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,0x8(%rsp),%rax
movslq (%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%rcx
test   %ebx,%ebx
je     1157 <timer+0x47>
add    %rcx,%rax
mov    %rax,0x2edd(%rip)
xor    %eax,%eax
jmp    1161 <timer+0x51>
sub    0x2ed2(%rip),%rax
add    %rcx,%rax
add    $0x10,%rsp
pop    %rbx
ret

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

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