#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;
  int tv_usec;
};
typedef int /*<<< orphan*/ res;
typedef int int64_t;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
const char *calc_elapsed_time(struct timeval *bef, struct timeval *aft) {
  static char res[128];
  int64_t elapsed_tvsec = (aft->tv_sec - bef->tv_sec);
  int64_t elapsed_tvusec = (aft->tv_usec - bef->tv_usec);
  int64_t elapsed_usec = (elapsed_tvsec * 1000000) + elapsed_tvusec;
  int64_t sec = elapsed_usec / 1000000;
  int64_t usec = elapsed_usec % 1000000;
  snprintf(res, sizeof(res) - 1, "%3jd.%06jd", sec, usec);
  return res;
}


x86 assembly (clang, -O0):
<calc_elapsed_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x8(%rbp),%rcx
sub    0x4(%rcx),%eax
mov    %eax,-0x18(%rbp)
imul   $0xf4240,-0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x24(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
lea    0x2ec9(%rip),%rdi
mov    $0x7f,%esi
lea    0xe7d(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2eb1(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_elapsed_time>:
push   %rbx
mov    (%rsi),%eax
sub    (%rdi),%eax
mov    0x4(%rsi),%ecx
sub    0x4(%rdi),%ecx
imul   $0xf4240,%eax,%eax
add    %ecx,%eax
movslq %eax,%r8
imul   $0x431bde83,%r8,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
sar    $0x32,%rcx
add    %eax,%ecx
imul   $0xf4240,%ecx,%eax
sub    %eax,%r8d
lea    0x2ef6(%rip),%rbx
lea    0xeaf(%rip),%rdx
mov    %rbx,%rdi
mov    $0x7f,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@calc_elapsed_time.res = internal global [128 x i8] zeroinitializer
@.str = [11 x i8] c"%3jd.%06jd\00"
define i8* @calc_elapsed_time(%struct.timeval* %0, %struct.timeval* %1) {
%3 = alloca %struct.timeval*
%4 = alloca %struct.timeval*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.timeval* %0, %struct.timeval** %3
store %struct.timeval* %1, %struct.timeval** %4
%10 = load %struct.timeval*, %struct.timeval** %4
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load %struct.timeval*, %struct.timeval** %3
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = sub nsw i32 %12, %15
store i32 %16, i32* %5
%17 = load %struct.timeval*, %struct.timeval** %4
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load %struct.timeval*, %struct.timeval** %3
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = sub nsw i32 %19, %22
store i32 %23, i32* %6
%24 = load i32, i32* %5
%25 = mul nsw i32 %24, 1000000
%26 = load i32, i32* %6
%27 = add nsw i32 %25, %26
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = sdiv i32 %28, 1000000
store i32 %29, i32* %8
%30 = load i32, i32* %7
%31 = srem i32 %30, 1000000
store i32 %31, i32* %9
%32 = load i32, i32* %8
%33 = load i32, i32* %9
%34 = call i32 @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @calc_elapsed_time.res, i64 0, i64 0), i32 127, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %33)
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @calc_elapsed_time.res, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@calc_elapsed_time.res = internal global [128 x i8] zeroinitializer
@.str = [11 x i8] c"%3jd.%06jd\00"
define i8* @calc_elapsed_time(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1) {
%3 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%4 = load i32, i32* %3
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = sub nsw i32 %4, %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%11 = load i32, i32* %10
%12 = sub nsw i32 %9, %11
%13 = mul nsw i32 %7, 1000000
%14 = add nsw i32 %12, %13
%15 = sdiv i32 %14, 1000000
%16 = srem i32 %14, 1000000
%17 = tail call i32 @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @calc_elapsed_time.res, i64 0, i64 0), i32 127, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @calc_elapsed_time.res, i64 0, i64 0)
}