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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
long int us_elapsed(struct timeval *prev) {
  struct timeval aux;
  gettimeofday(&aux, NULL);
  long int result;
  if (aux.tv_usec >= prev->tv_usec) {
    result =
        aux.tv_usec - prev->tv_usec + (aux.tv_sec - prev->tv_sec) * 1000000;
  } else {
    result = 1000000 - prev->tv_usec + aux.tv_usec +
             (aux.tv_sec - 1 - prev->tv_sec) * 1000000;
  }
  *prev = aux;
  return result;
}


x86 assembly (clang, -O0):
<us_elapsed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jl     115e <us_elapsed+0x4e>
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rdx
sub    0x4(%rdx),%ecx
imul   $0xf4240,%ecx,%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x18(%rbp)
jmp    1187 <us_elapsed+0x77>
mov    -0x8(%rbp),%rcx
mov    $0xf4240,%eax
sub    (%rcx),%eax
add    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x8(%rbp),%rdx
sub    0x4(%rdx),%ecx
imul   $0xf4240,%ecx,%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<us_elapsed>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%eax
sub    (%rbx),%eax
jge    1144 <us_elapsed+0x34>
mov    0x4(%rbx),%ecx
not    %ecx
add    0xc(%rsp),%ecx
imul   $0xf4240,%ecx,%ecx
add    %ecx,%eax
add    $0xf4240,%eax
jmp    1153 <us_elapsed+0x43>
mov    0xc(%rsp),%ecx
sub    0x4(%rbx),%ecx
imul   $0xf4240,%ecx,%ecx
add    %ecx,%eax
cltq
mov    0x8(%rsp),%rcx
mov    %rcx,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i64 @us_elapsed(%struct.timeval* %0) {
%2 = alloca %struct.timeval*
%3 = alloca %struct.timeval
%4 = alloca i64
store %struct.timeval* %0, %struct.timeval** %2
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%7 = load i32, i32* %6
%8 = load %struct.timeval*, %struct.timeval** %2
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%10 = load i32, i32* %9
%11 = icmp sge i32 %7, %10
br i1 %11, label %12, label %28
12: ; preds = %1
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = load %struct.timeval*, %struct.timeval** %2
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = sub nsw i32 %14, %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.timeval*, %struct.timeval** %2
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = sub nsw i32 %20, %23
%25 = mul nsw i32 %24, 1000000
%26 = add nsw i32 %18, %25
%27 = sext i32 %26 to i64
store i64 %27, i64* %4
br label %46
28: ; preds = %1
%29 = load %struct.timeval*, %struct.timeval** %2
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = sub nsw i32 1000000, %31
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%34 = load i32, i32* %33
%35 = add nsw i32 %32, %34
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%37 = load i32, i32* %36
%38 = sub nsw i32 %37, 1
%39 = load %struct.timeval*, %struct.timeval** %2
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = sub nsw i32 %38, %41
%43 = mul nsw i32 %42, 1000000
%44 = add nsw i32 %35, %43
%45 = sext i32 %44 to i64
store i64 %45, i64* %4
br label %46
46: ; preds = %28, %12
%47 = load %struct.timeval*, %struct.timeval** %2
%48 = bitcast %struct.timeval* %47 to i8*
%49 = bitcast %struct.timeval* %3 to i8*
%50 = load i64, i64* %4
ret i64 %50
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i64 @us_elapsed(%struct.timeval* nocapture %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.timeval*
%4 = bitcast i64* %2 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = bitcast i64* %2 to i32*
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%9 = load i32, i32* %8
%10 = icmp slt i32 %7, %9
br i1 %10, label %20, label %11
11: ; preds = %1
%12 = sub i32 %7, %9
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%16 = load i32, i32* %15
%17 = sub nsw i32 %14, %16
%18 = mul nsw i32 %17, 1000000
%19 = add nsw i32 %12, %18
br label %31
20: ; preds = %1
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%24 = load i32, i32* %23
%25 = xor i32 %24, -1
%26 = add i32 %22, %25
%27 = mul nsw i32 %26, 1000000
%28 = add i32 %7, 1000000
%29 = sub i32 %28, %9
%30 = add i32 %29, %27
br label %31
31: ; preds = %20, %11
%32 = phi i32 [ %19, %11 ], [ %30, %20 ]
%33 = sext i32 %32 to i64
%34 = bitcast %struct.timeval* %0 to i64*
%35 = load i64, i64* %2
store i64 %35, i64* %34
ret i64 %33
}