#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*/ _xfree(char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ xmalloc(int);
int elapsed_time(struct timeval *starttime) {
  struct timeval *newtime;
  int elapsed;
  newtime = (struct timeval *)xmalloc(sizeof(struct timeval));
  gettimeofday(newtime, NULL);
  elapsed = 0;
  if ((newtime->tv_usec - starttime->tv_usec) > 0) {
    elapsed += (newtime->tv_usec - starttime->tv_usec) / 1000;
  } else {
    elapsed += (1000000 + newtime->tv_usec - starttime->tv_usec) / 1000;
    newtime->tv_sec--;
  }
  if ((newtime->tv_sec - starttime->tv_sec) > 0) {
    elapsed += 1000 * (newtime->tv_sec - starttime->tv_sec);
  }
  if (elapsed < 1)
    elapsed = 1;
  do {
    char **_p = (char **)&(newtime);
    _xfree(*_p);
    *_p = 0;
  } while (0);
  return (elapsed);
}


x86 assembly (clang, -O0):
<elapsed_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x8,%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
cmp    $0x0,%eax
jle    1192 <elapsed_time+0x62>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11be <elapsed_time+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0xf4240,%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x8(%rbp),%rcx
sub    0x4(%rcx),%eax
cmp    $0x0,%eax
jle    11ef <elapsed_time+0xbf>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x8(%rbp),%rcx
sub    0x4(%rcx),%eax
imul   $0x3e8,%eax,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    1200 <elapsed_time+0xd0>
movl   $0x1,-0x14(%rbp)
jmp    1205 <elapsed_time+0xd5>
lea    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <_xfree@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elapsed_time>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x8,%edi
call   1040 <xmalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    (%rbx),%ecx
mov    (%r14),%eax
mov    %ecx,%edx
sub    %eax,%edx
jle    115e <elapsed_time+0x2e>
mov    0x4(%rbx),%eax
jmp    1171 <elapsed_time+0x41>
sub    %eax,%ecx
add    $0xf4240,%ecx
mov    0x4(%rbx),%eax
add    $0xffffffff,%eax
mov    %eax,0x4(%rbx)
mov    %ecx,%edx
movslq %edx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
sub    0x4(%r14),%eax
imul   $0x3e8,%eax,%edx
xor    %esi,%esi
test   %eax,%eax
cmovg  %edx,%esi
add    %ecx,%esi
test   %esi,%esi
mov    $0x1,%ebp
cmovg  %esi,%ebp
mov    %rbx,%rdi
call   1050 <_xfree@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @elapsed_time(%struct.timeval* %0) {
%2 = alloca %struct.timeval*
%3 = alloca %struct.timeval*
%4 = alloca i32
%5 = alloca i8**
store %struct.timeval* %0, %struct.timeval** %2
%6 = call i64 @xmalloc(i32 8)
%7 = inttoptr i64 %6 to %struct.timeval*
store %struct.timeval* %7, %struct.timeval** %3
%8 = load %struct.timeval*, %struct.timeval** %3
%9 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
store i32 0, i32* %4
%10 = load %struct.timeval*, %struct.timeval** %3
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load %struct.timeval*, %struct.timeval** %2
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = sub nsw i32 %12, %15
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %1
%19 = load %struct.timeval*, %struct.timeval** %3
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load %struct.timeval*, %struct.timeval** %2
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = sub nsw i32 %21, %24
%26 = sdiv i32 %25, 1000
%27 = load i32, i32* %4
%28 = add nsw i32 %27, %26
store i32 %28, i32* %4
br label %45
29: ; preds = %1
%30 = load %struct.timeval*, %struct.timeval** %3
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = add nsw i32 1000000, %32
%34 = load %struct.timeval*, %struct.timeval** %2
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = sub nsw i32 %33, %36
%38 = sdiv i32 %37, 1000
%39 = load i32, i32* %4
%40 = add nsw i32 %39, %38
store i32 %40, i32* %4
%41 = load %struct.timeval*, %struct.timeval** %3
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = add nsw i32 %43, -1
store i32 %44, i32* %42
br label %45
45: ; preds = %29, %18
%46 = load %struct.timeval*, %struct.timeval** %3
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load %struct.timeval*, %struct.timeval** %2
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = sub nsw i32 %48, %51
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %65
54: ; preds = %45
%55 = load %struct.timeval*, %struct.timeval** %3
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %55, i32 0, i32 1
%57 = load i32, i32* %56
%58 = load %struct.timeval*, %struct.timeval** %2
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %58, i32 0, i32 1
%60 = load i32, i32* %59
%61 = sub nsw i32 %57, %60
%62 = mul nsw i32 1000, %61
%63 = load i32, i32* %4
%64 = add nsw i32 %63, %62
store i32 %64, i32* %4
br label %65
65: ; preds = %54, %45
%66 = load i32, i32* %4
%67 = icmp slt i32 %66, 1
br i1 %67, label %68, label %69
68: ; preds = %65
store i32 1, i32* %4
br label %69
69: ; preds = %68, %65
br label %70
70: ; preds = %69
%71 = bitcast %struct.timeval** %3 to i8**
store i8** %71, i8*** %5
%72 = load i8**, i8*** %5
%73 = load i8*, i8** %72
%74 = call i32 @_xfree(i8* %73)
%75 = load i8**, i8*** %5
store i8* null, i8** %75
br label %76
76: ; preds = %70
%77 = load i32, i32* %4
ret i32 %77
}

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