#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 {
  long tv_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
unsigned long microseconds_since(struct timeval *tv) {
  int ret;
  struct timeval now;
  struct timeval diff;
  unsigned long retval;
  ret = gettimeofday(&now, NULL);
  if (ret == -1) {
    return 0L;
  }
  diff.tv_sec = now.tv_sec - tv->tv_sec;
  diff.tv_usec = now.tv_usec - tv->tv_usec;
  retval = diff.tv_sec * 1000000L;
  retval += diff.tv_usec;
  return retval;
}


x86 assembly (clang, -O0):
<microseconds_since>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1143 <microseconds_since+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1182 <microseconds_since+0x72>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x30(%rbp)
imul   $0xf4240,-0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<microseconds_since>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     1148 <microseconds_since+0x38>
mov    0x8(%rsp),%rax
mov    0x10(%rsp),%rcx
sub    (%r14),%rax
sub    0x8(%r14),%rcx
imul   $0xf4240,%rax,%rbx
add    %rcx,%rbx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i64 @microseconds_since(%struct.timeval* nocapture %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 %4, -1
br i1 %5, label %19, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%10 = load i64, i64* %9
%11 = sub nsw i64 %8, %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%13 = load i64, i64* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%15 = load i64, i64* %14
%16 = sub i64 %13, %15
%17 = mul nsw i64 %11, 1000000
%18 = add i64 %16, %17
br label %19
19: ; preds = %1, %6
%20 = phi i64 [ %18, %6 ], [ 0, %1 ]
ret i64 %20
}