#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;
  long tv_usec;
};
struct rusage {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ RUSAGE_SELF;
int /*<<< orphan*/ get_cputime_from_rusage(struct rusage *, struct timeval *);
int /*<<< orphan*/ getrusage(int /*<<< orphan*/, struct rusage *);
void stop_timer(struct timeval *tv_with_recorded_start_time,
                struct timeval *tv_with_accumulated_time) {
  struct rusage end_rusage;
  struct timeval end_tv;
  long delta_sec, delta_usec;
  getrusage(RUSAGE_SELF, &end_rusage);
  get_cputime_from_rusage(&end_rusage, &end_tv);
  delta_sec = end_tv.tv_sec - tv_with_recorded_start_time->tv_sec;
  delta_usec = end_tv.tv_usec - tv_with_recorded_start_time->tv_usec;
  if (delta_usec < 0) {
    delta_usec += (1000000);
    delta_sec--;
  }
  tv_with_accumulated_time->tv_sec += delta_sec;
  tv_with_accumulated_time->tv_usec += delta_usec;
  if (tv_with_accumulated_time->tv_usec >= (1000000)) {
    tv_with_accumulated_time->tv_usec -= (1000000);
    tv_with_accumulated_time->tv_sec++;
  }
}


x86 assembly (clang, -O0):
<stop_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <getrusage@plt>
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <get_cputime_from_rusage@plt>
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1193 <stop_timer+0x73>
mov    -0x38(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0xf4240,0x8(%rax)
jl     11e4 <stop_timer+0xc4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
sub    $0xf4240,%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stop_timer>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2eaa(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%r15
mov    %r15,%rsi
call   1040 <getrusage@plt>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1030 <get_cputime_from_rusage@plt>
mov    0x10(%rsp),%rax
sub    (%r14),%rax
mov    0x18(%rsp),%rdx
sub    0x8(%r14),%rdx
lea    0xf4240(%rdx),%rcx
mov    %rdx,%rsi
sar    $0x3f,%rsi
test   %rdx,%rdx
cmovns %rdx,%rcx
add    (%rbx),%rax
add    %rsi,%rax
mov    %rax,(%rbx)
add    0x8(%rbx),%rcx
mov    %rcx,0x8(%rbx)
cmp    $0xf4240,%rcx
jl     11a4 <stop_timer+0x84>
add    $0xfffffffffff0bdc0,%rcx
mov    %rcx,0x8(%rbx)
add    $0x1,%rax
mov    %rax,(%rbx)
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.rusage = type { i32 }
@RUSAGE_SELF = global i32 0
define void @stop_timer(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1) {
%3 = alloca %struct.rusage
%4 = alloca %struct.timeval
%5 = bitcast %struct.rusage* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = load i32, i32* @RUSAGE_SELF
%8 = call i32 @getrusage(i32 %7, %struct.rusage* %3)
%9 = call i32 @get_cputime_from_rusage(%struct.rusage* %3, %struct.timeval* %4)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%11 = load i64, i64* %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%13 = load i64, i64* %12
%14 = sub i64 %11, %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%16 = load i64, i64* %15
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%18 = load i64, i64* %17
%19 = sub nsw i64 %16, %18
%20 = icmp slt i64 %19, 0
%21 = add nsw i64 %19, 1000000
%22 = ashr i64 %19, 63
%23 = select i1 %20, i64 %21, i64 %19
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%25 = load i64, i64* %24
%26 = add i64 %14, %25
%27 = add i64 %26, %22
store i64 %27, i64* %24
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%29 = load i64, i64* %28
%30 = add nsw i64 %29, %23
store i64 %30, i64* %28
%31 = icmp sgt i64 %30, 999999
br i1 %31, label %32, label %35
32: ; preds = %2
%33 = add nsw i64 %30, -1000000
store i64 %33, i64* %28
%34 = add nsw i64 %27, 1
store i64 %34, i64* %24
br label %35
35: ; preds = %32, %2
ret void
}