#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*/ printf(char *, long);
void get_timer(int which) {
  long lapsed_usec;
  static long utimer = 0;
  static long timer = 0;
  struct timeval tp;
  gettimeofday(&tp, (struct timezone *)0);
  if (which == 1) {
    lapsed_usec = ((tp.tv_sec - timer) * 1000000) + tp.tv_usec - utimer;
    printf("elapsed time is %ld\n", lapsed_usec);
  } else {
    utimer = tp.tv_usec;
    timer = tp.tv_sec;
  }
}


x86 assembly (clang, -O0):
<get_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmpl   $0x1,-0x4(%rbp)
jne    1178 <get_timer+0x58>
mov    -0x20(%rbp),%rax
sub    0x2ef3(%rip),%rax
imul   $0xf4240,%rax,%rax
add    -0x18(%rbp),%rax
sub    0x2ed9(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe92(%rip),%rdi
call   1030 <printf@plt>
jmp    118e <get_timer+0x6e>
mov    -0x18(%rbp),%rax
mov    %rax,0x2eb5(%rip)
mov    -0x20(%rbp),%rax
mov    %rax,0x2eb2(%rip)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_timer>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
cmp    $0x1,%ebx
jne    1165 <get_timer+0x45>
mov    (%rsp),%rax
sub    0x2eff(%rip),%rax
imul   $0xf4240,%rax,%rsi
add    0x8(%rsp),%rsi
sub    0x2ee4(%rip),%rsi
lea    0xea5(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <printf@plt>
mov    (%rsp),%rax
mov    0x8(%rsp),%rcx
mov    %rcx,0x2ec3(%rip)
mov    %rax,0x2ec4(%rip)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
@get_timer.utimer = internal global i64 0
@get_timer.timer = internal global i64 0
@.str = [21 x i8] c"elapsed time is %ld\0A\00"
define void @get_timer(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.timeval
store i32 %0, i32* %2
%5 = call i32 @gettimeofday(%struct.timeval* %4, %struct.timezone* null)
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %21
8: ; preds = %1
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @get_timer.timer
%12 = sub nsw i64 %10, %11
%13 = mul nsw i64 %12, 1000000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%15 = load i64, i64* %14
%16 = add nsw i64 %13, %15
%17 = load i64, i64* @get_timer.utimer
%18 = sub nsw i64 %16, %17
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %19)
br label %26
21: ; preds = %1
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%23 = load i64, i64* %22
store i64 %23, i64* @get_timer.utimer
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%25 = load i64, i64* %24
store i64 %25, i64* @get_timer.timer
br label %26
26: ; preds = %21, %8
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
@get_timer.utimer = internal unnamed_addr global i64 0
@get_timer.timer = internal unnamed_addr global i64 0
@.str = [21 x i8] c"elapsed time is %ld\0A\00"
define void @get_timer(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = call i32 @gettimeofday(%struct.timeval* %2, %struct.timezone* null)
%5 = icmp eq i32 %0, 1
br i1 %5, label %6, label %18
6: ; preds = %1
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @get_timer.timer
%10 = sub nsw i64 %8, %9
%11 = mul nsw i64 %10, 1000000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%13 = load i64, i64* %12
%14 = add nsw i64 %11, %13
%15 = load i64, i64* @get_timer.utimer
%16 = sub i64 %14, %15
%17 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %16)
br label %23
18: ; preds = %1
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%20 = load i64, i64* %19
store i64 %20, i64* @get_timer.utimer
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%22 = load i64, i64* %21
store i64 %22, i64* @get_timer.timer
br label %23
23: ; preds = %18, %6
ret void
}