#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 */

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
char *format_timediff(long long diff) {
  char *textual = malloc(20);
  if (!textual) {
    exit(ENOMEM);
  }
  if (diff < 0) {
    snprintf(textual, 20, "n/a");
  }
  if (diff < 60) {
    snprintf(textual, 20, "%llds", diff);
  } else if (diff < 60 * 60) {
    snprintf(textual, 20, "%lldm%llds", diff / 60, diff % 60);
  } else if (diff < 60 * 60 * 24) {
    snprintf(textual, 20, "%lldh%lldm", diff / (60 * 60),
             (diff % (60 * 60)) / 60);
  } else {
    snprintf(textual, 20, "%lldd%lldh", diff / (60 * 60 * 24),
             (diff % (60 * 60 * 24)) / (60 * 60));
  }
  return (textual);
}


x86 assembly (clang, -O0):
<format_timediff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x14,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <format_timediff+0x33>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jge    1185 <format_timediff+0x55>
mov    -0x10(%rbp),%rdi
mov    $0x14,%esi
lea    0xe82(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
cmpq   $0x3c,-0x8(%rbp)
jge    11b0 <format_timediff+0x80>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x14,%esi
lea    0xe65(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    12a5 <format_timediff+0x175>
cmpq   $0xe10,-0x8(%rbp)
jge    11fc <format_timediff+0xcc>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    $0x3c,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    $0x3c,%esi
cqto
idiv   %rsi
mov    %rdx,%r8
mov    $0x14,%esi
lea    0xe14(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    12a0 <format_timediff+0x170>
cmpq   $0x15180,-0x8(%rbp)
jge    1255 <format_timediff+0x125>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    $0xe10,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    $0xe10,%esi
cqto
idiv   %rsi
mov    %rdx,%rax
mov    $0x3c,%esi
cqto
idiv   %rsi
mov    %rax,%r8
mov    $0x14,%esi
lea    0xdc6(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    129b <format_timediff+0x16b>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    $0x15180,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    $0x15180,%esi
cqto
idiv   %rsi
mov    %rdx,%rax
mov    $0xe10,%esi
cqto
idiv   %rsi
mov    %rax,%r8
mov    $0x14,%esi
lea    0xd86(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    12a0 <format_timediff+0x170>
jmp    12a5 <format_timediff+0x175>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_timediff>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x14,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1237 <format_timediff+0x107>
mov    %rax,%r14
test   %rbx,%rbx
js     1182 <format_timediff+0x52>
cmp    $0x3b,%rbx
jbe    1198 <format_timediff+0x68>
cmp    $0xe0f,%rbx
ja     11b3 <format_timediff+0x83>
movzwl %bx,%eax
imul   $0x8889,%eax,%ecx
shr    $0x15,%ecx
imul   $0x3c,%ecx,%eax
sub    %eax,%ebx
movzwl %bx,%r8d
lea    0xe87(%rip),%rdx
jmp    121d <format_timediff+0xed>
lea    0xe77(%rip),%rdx
mov    %r14,%rdi
mov    $0x14,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    0xe6a(%rip),%rdx
mov    %r14,%rdi
mov    $0x14,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    122c <format_timediff+0xfc>
cmp    $0x1517f,%rbx
ja     11e7 <format_timediff+0xb7>
mov    %ebx,%eax
mov    $0x91a2b3c5,%ecx
imul   %rax,%rcx
shr    $0x2b,%rcx
imul   $0xe10,%ecx,%eax
sub    %eax,%ebx
imul   $0x8889,%ebx,%r8d
shr    $0x15,%r8d
lea    0xe2a(%rip),%rdx
jmp    121d <format_timediff+0xed>
movabs $0xc22e450672894ab7,%rcx
45 2e c2
mov    %rbx,%rax
mul    %rcx
mov    %rdx,%rcx
shr    $0x10,%rcx
imul   $0x15180,%rcx,%rax
sub    %rax,%rbx
mov    $0x91a2b3c5,%r8d
imul   %rbx,%r8
shr    $0x2b,%r8
lea    0xdfd(%rip),%rdx
mov    %r14,%rdi
mov    $0x14,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@ENOMEM = global i32 0
@.str = [4 x i8] c"n/a\00"
@.str.1 = [6 x i8] c"%llds\00"
@.str.2 = [11 x i8] c"%lldm%llds\00"
@.str.3 = [11 x i8] c"%lldh%lldm\00"
@.str.4 = [11 x i8] c"%lldd%lldh\00"
define i8* @format_timediff(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
store i64 %0, i64* %2
%4 = call i8* @malloc(i32 20)
store i8* %4, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* @ENOMEM
%9 = call i32 @exit(i32 %8)
unreachable
10: ; preds = %1
%11 = load i64, i64* %2
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i8*, i8** %3
%15 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %14, i32 20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %10
%17 = load i64, i64* %2
%18 = icmp slt i64 %17, 60
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = load i64, i64* %2
%22 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %20, i32 20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %21)
br label %54
23: ; preds = %16
%24 = load i64, i64* %2
%25 = icmp slt i64 %24, 3600
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = load i8*, i8** %3
%28 = load i64, i64* %2
%29 = sdiv i64 %28, 60
%30 = load i64, i64* %2
%31 = srem i64 %30, 60
%32 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %27, i32 20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %29, i64 %31)
br label %53
33: ; preds = %23
%34 = load i64, i64* %2
%35 = icmp slt i64 %34, 86400
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i8*, i8** %3
%38 = load i64, i64* %2
%39 = sdiv i64 %38, 3600
%40 = load i64, i64* %2
%41 = srem i64 %40, 3600
%42 = sdiv i64 %41, 60
%43 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %37, i32 20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i64 %39, i64 %42)
br label %52
44: ; preds = %33
%45 = load i8*, i8** %3
%46 = load i64, i64* %2
%47 = sdiv i64 %46, 86400
%48 = load i64, i64* %2
%49 = srem i64 %48, 86400
%50 = sdiv i64 %49, 3600
%51 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %45, i32 20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %47, i64 %50)
br label %52
52: ; preds = %44, %36
br label %53
53: ; preds = %52, %26
br label %54
54: ; preds = %53, %19
%55 = load i8*, i8** %3
ret i8* %55
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@.str = [4 x i8] c"n/a\00"
@.str.1 = [6 x i8] c"%llds\00"
@.str.2 = [11 x i8] c"%lldm%llds\00"
@.str.3 = [11 x i8] c"%lldh%lldm\00"
@.str.4 = [11 x i8] c"%lldd%lldh\00"
define i8* @format_timediff(i64 %0) {
%2 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @ENOMEM
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %1
%8 = icmp slt i64 %0, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %13
11: ; preds = %7
%12 = icmp ult i64 %0, 60
br i1 %12, label %13, label %15
13: ; preds = %9, %11
%14 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %0)
br label %42
15: ; preds = %11
%16 = icmp ult i64 %0, 3600
br i1 %16, label %17, label %24
17: ; preds = %15
%18 = trunc i64 %0 to i16
%19 = udiv i16 %18, 60
%20 = zext i16 %19 to i64
%21 = urem i16 %18, 60
%22 = zext i16 %21 to i64
%23 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %20, i64 %22)
br label %42
24: ; preds = %15
%25 = icmp ult i64 %0, 86400
br i1 %25, label %26, label %35
26: ; preds = %24
%27 = trunc i64 %0 to i32
%28 = udiv i32 %27, 3600
%29 = zext i32 %28 to i64
%30 = urem i32 %27, 3600
%31 = trunc i32 %30 to i16
%32 = udiv i16 %31, 60
%33 = zext i16 %32 to i64
%34 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i64 %29, i64 %33)
br label %42
35: ; preds = %24
%36 = udiv i64 %0, 86400
%37 = urem i64 %0, 86400
%38 = trunc i64 %37 to i32
%39 = udiv i32 %38, 3600
%40 = zext i32 %39 to i64
%41 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %2, i32 20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %36, i64 %40)
br label %42
42: ; preds = %17, %35, %26, %13
ret i8* %2
}