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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int timeelapsed(struct timeval *elapsed, const struct timeval *start) {
  struct timeval end;
  gettimeofday(&end, NULL);
  if (end.tv_usec < start->tv_usec) {
    end.tv_usec += 1000000;
    end.tv_sec--;
  }
  elapsed->tv_sec = end.tv_sec - start->tv_sec;
  elapsed->tv_usec = end.tv_usec - start->tv_usec;
  return 0;
}


x86 assembly (clang, -O0):
<timeelapsed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jge    1158 <timeelapsed+0x48>
mov    -0x20(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    0x8(%rax),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    (%rax),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeelapsed>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    (%rbx),%rax
mov    0x8(%rsp),%rcx
mov    0x10(%rsp),%rdx
lea    0xf4240(%rcx),%rsi
xor    %edi,%edi
cmp    %rax,%rcx
cmovge %rcx,%rsi
setl   %dil
sub    %rdi,%rdx
sub    0x8(%rbx),%rdx
mov    %rdx,0x8(%r14)
sub    %rax,%rsi
mov    %rsi,(%r14)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @timeelapsed(%struct.timeval* nocapture writeonly %0, %struct.timeval* nocapture %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 i64, i64* %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%9 = load i64, i64* %8
%10 = icmp slt i64 %7, %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%12 = load i64, i64* %11
%13 = add nsw i64 %7, 1000000
%14 = select i1 %10, i64 %13, i64 %7
%15 = sext i1 %10 to i64
%16 = add nsw i64 %12, %15
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%18 = load i64, i64* %17
%19 = sub nsw i64 %16, %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
store i64 %19, i64* %20
%21 = sub nsw i64 %14, %9
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
store i64 %21, i64* %22
ret i32 0
}