#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*/ snprintf(char *, int, char *, int, int, int, int);
char *time_diff(struct timeval *start, struct timeval *end, char *buf,
                int max) {
  struct timeval diff;
  diff.tv_usec = end->tv_usec - start->tv_usec;
  diff.tv_sec = end->tv_sec - start->tv_sec;
  if (diff.tv_usec < 0) {
    diff.tv_usec += 1000000;
    diff.tv_sec--;
  }
  buf[max - 1] = 0;
  snprintf(buf, max, "%.2dh%.2dm%.2d.%.2ds", diff.tv_sec / 3600,
           (diff.tv_sec / 60) % 60, diff.tv_sec % 60, diff.tv_usec / 10000);
  return buf;
}


x86 assembly (clang, -O0):
<time_diff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x8(%rbp),%rcx
sub    0x4(%rcx),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1165 <time_diff+0x55>
mov    -0x28(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x24(%rbp),%eax
mov    $0xe10,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
mov    $0x3c,%r8d
cltd
idiv   %r8d
mov    $0x3c,%r8d
cltd
idiv   %r8d
mov    %edx,%r8d
mov    -0x24(%rbp),%eax
mov    $0x3c,%r9d
cltd
idiv   %r9d
mov    %edx,%r9d
mov    -0x28(%rbp),%eax
mov    $0x2710,%r10d
cltd
idiv   %r10d
lea    0xe38(%rip),%rdx
mov    %eax,(%rsp)
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_diff>:
push   %rbx
sub    $0x10,%rsp
mov    (%rsi),%ebx
mov    0x4(%rsi),%esi
sub    (%rdi),%ebx
sub    0x4(%rdi),%esi
lea    0xf4240(%rbx),%eax
mov    %ebx,%edi
sar    $0x1f,%edi
add    %esi,%edi
test   %ebx,%ebx
cmovns %ebx,%eax
mov    %rdx,%rbx
movslq %ecx,%rsi
movb   $0x0,-0x1(%rsi,%rdx,1)
movslq %edi,%r9
imul   $0xffffffff91a2b3c5,%r9,%rcx
shr    $0x20,%rcx
add    %r9d,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0xb,%ecx
add    %edx,%ecx
imul   $0xffffffff88888889,%r9,%rdx
shr    $0x20,%rdx
add    %r9d,%edx
mov    %edx,%edi
shr    $0x1f,%edi
sar    $0x5,%edx
add    %edi,%edx
movslq %edx,%r8
imul   $0xffffffff88888889,%r8,%rdx
shr    $0x20,%rdx
add    %r8d,%edx
mov    %edx,%edi
shr    $0x1f,%edi
sar    $0x5,%edx
add    %edi,%edx
imul   $0x3c,%edx,%edx
imul   $0x3c,%r8d,%edi
sub    %edx,%r8d
sub    %edi,%r9d
cltq
imul   $0x68db8bad,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x2c,%rax
add    %edx,%eax
mov    %eax,(%rsp)
lea    0xe49(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [21 x i8] c"%.2dh%.2dm%.2d.%.2ds\00"
define i8* @time_diff(%struct.timeval* %0, %struct.timeval* %1, i8* %2, i32 %3) {
%5 = alloca %struct.timeval*
%6 = alloca %struct.timeval*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.timeval
store %struct.timeval* %0, %struct.timeval** %5
store %struct.timeval* %1, %struct.timeval** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%10 = load %struct.timeval*, %struct.timeval** %6
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load %struct.timeval*, %struct.timeval** %5
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = sub nsw i32 %12, %15
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %16, i32* %17
%18 = load %struct.timeval*, %struct.timeval** %6
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.timeval*, %struct.timeval** %5
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = sub nsw i32 %20, %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%27 = load i32, i32* %26
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %4
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1000000
store i32 %32, i32* %30
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%34 = load i32, i32* %33
%35 = add nsw i32 %34, -1
store i32 %35, i32* %33
br label %36
36: ; preds = %29, %4
%37 = load i8*, i8** %7
%38 = load i32, i32* %8
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %7
%43 = load i32, i32* %8
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%45 = load i32, i32* %44
%46 = sdiv i32 %45, 3600
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%48 = load i32, i32* %47
%49 = sdiv i32 %48, 60
%50 = srem i32 %49, 60
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%52 = load i32, i32* %51
%53 = srem i32 %52, 60
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%55 = load i32, i32* %54
%56 = sdiv i32 %55, 10000
%57 = call i32 @snprintf(i8* %42, i32 %43, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %46, i32 %50, i32 %53, i32 %56)
%58 = load i8*, i8** %7
ret i8* %58
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [21 x i8] c"%.2dh%.2dm%.2d.%.2ds\00"
define i8* @time_diff(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1, i8* returned %2, i32 %3) {
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, 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 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%13 = load i32, i32* %12
%14 = sub nsw i32 %11, %13
%15 = icmp slt i32 %9, 0
%16 = add nsw i32 %9, 1000000
%17 = ashr i32 %9, 31
%18 = add nsw i32 %14, %17
%19 = select i1 %15, i32 %16, i32 %9
%20 = add nsw i32 %3, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %2, i64 %21
store i8 0, i8* %22
%23 = sdiv i32 %18, 3600
%24 = sdiv i32 %18, 60
%25 = srem i32 %24, 60
%26 = srem i32 %18, 60
%27 = sdiv i32 %19, 10000
%28 = tail call i32 @snprintf(i8* %2, i32 %3, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %25, i32 %26, i32 %27)
ret i8* %2
}