#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 timezone {
  int dummy;
};
struct timeval {
  long tv_sec;
  long tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, struct timezone *);
int /*<<< orphan*/ vis5d_get_display_timer(int, long *, long *);
int /*<<< orphan*/ vis5d_reset_display_timer(int);
int timer(int index, int flag) {
  struct timeval tp;
  static long start_usec, start_sec, lapsed_usec;
  gettimeofday(&tp, (struct timezone *)0);
  if (flag) {
    vis5d_get_display_timer(index, &start_sec, &start_usec);
    lapsed_usec = ((tp.tv_sec - start_sec) * 1000000) + tp.tv_usec - start_usec;
    return lapsed_usec / 1000;
  } else {
    vis5d_reset_display_timer(index);
    return 0;
  }
}


x86 assembly (clang, -O0):
<timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmpl   $0x0,-0xc(%rbp)
je     11a8 <timer+0x78>
mov    -0x8(%rbp),%edi
lea    0x2ee9(%rip),%rsi
lea    0x2eda(%rip),%rdx
call   1050 <vis5d_get_display_timer@plt>
mov    -0x20(%rbp),%rax
sub    0x2ed2(%rip),%rax
imul   $0xf4240,%rax,%rax
add    -0x18(%rbp),%rax
sub    0x2eb8(%rip),%rax
mov    %rax,0x2ec1(%rip)
mov    0x2eba(%rip),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %eax,-0x4(%rbp)
jmp    11b7 <timer+0x87>
mov    -0x8(%rbp),%edi
call   1040 <vis5d_reset_display_timer@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
xor    %ebx,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %ebp,%ebp
je     11a0 <timer+0x70>
lea    0x2ef4(%rip),%rsi
lea    0x2ee5(%rip),%rdx
mov    %r14d,%edi
call   1050 <vis5d_get_display_timer@plt>
mov    (%rsp),%rax
sub    0x2eda(%rip),%rax
imul   $0xf4240,%rax,%rax
add    0x8(%rsp),%rax
sub    0x2ebf(%rip),%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
imul   %rcx
mov    %rdx,%rbx
mov    %rdx,%rax
shr    $0x3f,%rax
shr    $0x7,%rbx
add    %eax,%ebx
jmp    11a8 <timer+0x78>
mov    %r14d,%edi
call   1040 <vis5d_reset_display_timer@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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