#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 */
typedef scalar_t__ uint32_t;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
uint32_t time_elapsed_us(struct timeval tv) {
  struct timeval tv_now;
  uint32_t dur;
  gettimeofday(&tv_now, NULL);
  dur = ((tv_now.tv_sec - tv.tv_sec) * 1000000) + (tv_now.tv_usec - tv.tv_usec);
  return dur;
}


x86 assembly (clang, -O0):
<time_elapsed_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x10(%rbp)
mov    %rsi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x20(%rbp),%eax
sub    -0x10(%rbp),%eax
imul   $0xf4240,%eax,%eax
cltq
mov    -0x18(%rbp),%rcx
sub    -0x8(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_elapsed_us>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq 0x8(%rsp),%rax
movslq %ebp,%rcx
sub    %rcx,%rax
imul   $0xf4240,%rax,%rcx
mov    0x10(%rsp),%rax
sub    %rbx,%rax
add    %rcx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i64 @time_elapsed_us(i32 %0, i64 %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca i64
%6 = bitcast %struct.timeval* %3 to { i32, i64 }*
%7 = getelementptr inbounds { i32, i64 }, { i32, i64 }* %6, i32 0, i32 0
store i32 %0, i32* %7
%8 = getelementptr inbounds { i32, i64 }, { i32, i64 }* %6, i32 0, i32 1
store i64 %1, i64* %8
%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 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%13 = load i32, i32* %12
%14 = sub nsw i32 %11, %13
%15 = mul nsw i32 %14, 1000000
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%20 = load i64, i64* %19
%21 = sub nsw i64 %18, %20
%22 = add nsw i64 %16, %21
store i64 %22, i64* %5
%23 = load i64, i64* %5
ret i64 %23
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i64 @time_elapsed_us(i32 %0, i64 %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = sub nsw i32 %7, %0
%9 = mul nsw i32 %8, 1000000
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%12 = load i64, i64* %11
%13 = sub i64 %12, %1
%14 = add nsw i64 %13, %10
ret i64 %14
}