#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_sec;
  scalar_t__ tv_usec;
};
typedef scalar_t__ __useconds_t;

/* Variables and functions */
int /*<<< orphan*/ timeval_adjust_usec(struct timeval *, struct timeval *);
__useconds_t timeval_diff(struct timeval *l, struct timeval *r,
                          struct timeval *diff) {
  static struct timeval tmp;
  timeval_adjust_usec(l, r);
  if (diff == NULL)
    diff = &tmp;
  diff->tv_sec = l->tv_sec - r->tv_sec;
  diff->tv_usec = l->tv_usec - r->tv_usec;
  return (diff->tv_sec * 1000000) + diff->tv_usec;
}


x86 assembly (clang, -O0):
<timeval_diff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <timeval_adjust_usec@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1147 <timeval_diff+0x37>
lea    0x2eed(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
sub    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
sub    0x8(%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
imul   $0xf4240,(%rax),%eax
cltq
mov    -0x18(%rbp),%rcx
add    0x8(%rcx),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_diff>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <timeval_adjust_usec@plt>
test   %r14,%r14
lea    0x2f03(%rip),%rax
cmovne %r14,%rax
movslq (%rbx),%rcx
movslq (%r15),%rdx
sub    %rdx,%rcx
mov    %ecx,(%rax)
mov    0x8(%rbx),%rdx
sub    0x8(%r15),%rdx
mov    %rdx,0x8(%rax)
imul   $0xf4240,%rcx,%rax
add    %rdx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@timeval_diff.tmp = internal unnamed_addr global %struct.timeval zeroinitializer
define i64 @timeval_diff(%struct.timeval* %0, %struct.timeval* %1, %struct.timeval* %2) {
%4 = tail call i32 @timeval_adjust_usec(%struct.timeval* %0, %struct.timeval* %1)
%5 = icmp eq %struct.timeval* %2, null
%6 = select i1 %5, %struct.timeval* @timeval_diff.tmp, %struct.timeval* %2
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = sub nsw i32 %8, %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%16 = load i64, i64* %15
%17 = sub nsw i64 %14, %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 %17, i64* %18
%19 = mul nsw i32 %11, 1000000
%20 = sext i32 %19 to i64
%21 = add nsw i64 %17, %20
ret i64 %21
}