#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 */
char *malloc(int);
int sprintf(char *, char *, long);
char *human_time(long tm) {
  long remaining;
  long seconds = tm / 1000000000;
  remaining = tm % 1000000000;
  long millis = remaining / 1000000;
  remaining = remaining % 1000000;
  long micros = remaining / 1000;
  char *time_text = malloc(128 * sizeof(char));
  int n = 0;
  if (seconds > 0) {
    n = sprintf(time_text, " %lds", seconds);
  }
  if (millis > 0) {
    n += sprintf(time_text + n, " %ldms", millis);
  }
  if (micros > 0) {
    n += sprintf(time_text + n, " %ldus", micros);
  }
  return time_text;
}


x86 assembly (clang, -O0):
<human_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
mov    $0x80,%edi
call   1030 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    11bd <human_time+0x9d>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe4b(%rip),%rsi
call   1040 <sprintf@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    11e9 <human_time+0xc9>
mov    -0x30(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rdx
lea    0xe28(%rip),%rsi
call   1040 <sprintf@plt>
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    1215 <human_time+0xf5>
mov    -0x30(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   1040 <sprintf@plt>
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<human_time>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
movabs $0x112e0be826d694b3,%rcx
0b 2e 11
mov    %rdi,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
imul   $0x3b9aca00,%rdx,%rax
mov    %rdi,%r13
sub    %rax,%r13
mov    %r13d,%r12d
movslq %r13d,%rax
imul   $0x431bde83,%rax,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
sar    $0x32,%rbp
add    %eax,%ebp
imul   $0xf4240,%ebp,%eax
mov    %eax,0x4(%rsp)
mov    $0x80,%edi
call   1030 <malloc@plt>
mov    %rax,%r14
xor    %r15d,%r15d
cmp    $0x3b9aca00,%rbx
jl     11c0 <human_time+0xa0>
shr    $0x9,%rbx
movabs $0x44b82fa09b5a53,%rcx
mov    %rbx,%rax
mul    %rcx
shr    $0xb,%rdx
lea    0xe4b(%rip),%rsi
mov    %r14,%rdi
call   1040 <sprintf@plt>
mov    %eax,%r15d
sub    0x4(%rsp),%r12d
cmp    $0xf4240,%r13
jl     11e7 <human_time+0xc7>
movslq %ebp,%rdx
movslq %r15d,%r15
lea    (%r14,%r15,1),%rdi
lea    0xe27(%rip),%rsi
call   1040 <sprintf@plt>
add    %eax,%r15d
cmp    $0x3e8,%r12d
jl     121f <human_time+0xff>
movslq %r12d,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
movslq %eax,%rdx
movslq %r15d,%rax
mov    %r14,%rdi
add    %rax,%rdi
lea    0xdf3(%rip),%rsi
call   1040 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c" %lds\00"
@.str.1 = [7 x i8] c" %ldms\00"
@.str.2 = [7 x i8] c" %ldus\00"
define i8* @human_time(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i64 %0, i64* %2
%9 = load i64, i64* %2
%10 = sdiv i64 %9, 1000000000
store i64 %10, i64* %4
%11 = load i64, i64* %2
%12 = srem i64 %11, 1000000000
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = sdiv i64 %13, 1000000
store i64 %14, i64* %5
%15 = load i64, i64* %3
%16 = srem i64 %15, 1000000
store i64 %16, i64* %3
%17 = load i64, i64* %3
%18 = sdiv i64 %17, 1000
store i64 %18, i64* %6
%19 = call i8* @malloc(i32 128)
store i8* %19, i8** %7
store i32 0, i32* %8
%20 = load i64, i64* %4
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %1
%23 = load i8*, i8** %7
%24 = load i64, i64* %4
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %24)
store i32 %25, i32* %8
br label %26
26: ; preds = %22, %1
%27 = load i64, i64* %5
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i64, i64* %5
%35 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %34)
%36 = load i32, i32* %8
%37 = add nsw i32 %36, %35
store i32 %37, i32* %8
br label %38
38: ; preds = %29, %26
%39 = load i64, i64* %6
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %38
%42 = load i8*, i8** %7
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i64, i64* %6
%47 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 %46)
%48 = load i32, i32* %8
%49 = add nsw i32 %48, %47
store i32 %49, i32* %8
br label %50
50: ; preds = %41, %38
%51 = load i8*, i8** %7
ret i8* %51
}

LLVM IR (-O2):
@.str = [6 x i8] c" %lds\00"
@.str.1 = [7 x i8] c" %ldms\00"
@.str.2 = [7 x i8] c" %ldus\00"
define noalias i8* @human_time(i64 %0) {
%2 = srem i64 %0, 1000000000
%3 = trunc i64 %2 to i32
%4 = sdiv i32 %3, 1000000
%5 = sext i32 %4 to i64
%6 = srem i32 %3, 1000000
%7 = sdiv i32 %6, 1000
%8 = sext i32 %7 to i64
%9 = tail call dereferenceable_or_null(128) i8* @malloc(i32 128)
%10 = icmp sgt i64 %0, 999999999
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = udiv i64 %0, 1000000000
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %12)
br label %14
14: ; preds = %11, %1
%15 = phi i32 [ %13, %11 ], [ 0, %1 ]
%16 = icmp sgt i64 %2, 999999
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = sext i32 %15 to i64
%19 = getelementptr inbounds i8, i8* %9, i64 %18
%20 = tail call i32 @sprintf(i8* dereferenceable(1) %19, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %5)
%21 = add nsw i32 %20, %15
br label %22
22: ; preds = %17, %14
%23 = phi i32 [ %21, %17 ], [ %15, %14 ]
%24 = icmp sgt i32 %6, 999
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = sext i32 %23 to i64
%27 = getelementptr inbounds i8, i8* %9, i64 %26
%28 = tail call i32 @sprintf(i8* dereferenceable(1) %27, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 %8)
br label %29
29: ; preds = %25, %22
ret i8* %9
}