#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 /*<<< orphan*/ snprintf(char *, int, char *, long long);
char *milliseconds_to_string(long long ms) {
  char *str = malloc(256);
  if (!str)
    return NULL;
  long long div = 1;
  char *fmt;
  if (ms < (long long)1000)
    fmt = "%lldms";
  else if (ms < 60 * (long long)1000) {
    fmt = "%llds";
    div = (long long)1000;
  } else if (ms < 60 * 60 * (long long)1000) {
    fmt = "%lldm";
    div = 60 * (long long)1000;
  } else if (ms < 24 * 60 * 60 * (long long)1000) {
    fmt = "%lldh";
    div = 60 * 60 * (long long)1000;
  } else if (ms < 7 * 24 * 60 * 60 * (long long)1000) {
    fmt = "%lldd";
    div = 24 * 60 * 60 * (long long)1000;
  } else if (ms < 52 * 7 * 24 * 60 * 60 * (long long)1000) {
    fmt = "%lldw";
    div = 7 * 24 * 60 * 60 * (long long)1000;
  } else {
    fmt = "%lldy";
    div = 52 * 7 * 24 * 60 * 60 * (long long)1000;
  }
  snprintf(str, 256, fmt, ms / div);
  return str;
}


x86 assembly (clang, -O0):
<milliseconds_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x100,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1152 <milliseconds_to_string+0x32>
movq   $0x0,-0x8(%rbp)
jmp    129d <milliseconds_to_string+0x17d>
movq   $0x1,-0x20(%rbp)
cmpq   $0x3e8,-0x10(%rbp)
jge    1178 <milliseconds_to_string+0x58>
lea    0xe91(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    126e <milliseconds_to_string+0x14e>
cmpq   $0xea60,-0x10(%rbp)
jge    119e <milliseconds_to_string+0x7e>
lea    0xe7a(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x3e8,-0x20(%rbp)
jmp    1269 <milliseconds_to_string+0x149>
cmpq   $0x36ee80,-0x10(%rbp)
jge    11c4 <milliseconds_to_string+0xa4>
lea    0xe5a(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0xea60,-0x20(%rbp)
jmp    1264 <milliseconds_to_string+0x144>
cmpq   $0x5265c00,-0x10(%rbp)
05
jge    11ea <milliseconds_to_string+0xca>
lea    0xe3a(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x36ee80,-0x20(%rbp)
jmp    125f <milliseconds_to_string+0x13f>
cmpq   $0x240c8400,-0x10(%rbp)
24
jge    1210 <milliseconds_to_string+0xf0>
lea    0xe1a(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x5265c00,-0x20(%rbp)
05
jmp    125a <milliseconds_to_string+0x13a>
movabs $0x7528ad000,%rax
cmp    %rax,-0x10(%rbp)
jge    123c <milliseconds_to_string+0x11c>
lea    0xdf4(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x240c8400,-0x20(%rbp)
24
jmp    1255 <milliseconds_to_string+0x135>
lea    0xde2(%rip),%rax
mov    %rax,-0x28(%rbp)
movabs $0x7528ad000,%rax
mov    %rax,-0x20(%rbp)
jmp    125a <milliseconds_to_string+0x13a>
jmp    125f <milliseconds_to_string+0x13f>
jmp    1264 <milliseconds_to_string+0x144>
jmp    1269 <milliseconds_to_string+0x149>
jmp    126e <milliseconds_to_string+0x14e>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
mov    $0x100,%esi
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<milliseconds_to_string>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x100,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1214 <milliseconds_to_string+0xf4>
cmp    $0x3e8,%rbx
jge    1157 <milliseconds_to_string+0x37>
lea    0xeb3(%rip),%rcx
mov    $0x1,%esi
jmp    11e5 <milliseconds_to_string+0xc5>
cmp    $0xea60,%rbx
jae    116e <milliseconds_to_string+0x4e>
lea    0xea0(%rip),%rcx
mov    $0x3e8,%esi
jmp    11e5 <milliseconds_to_string+0xc5>
cmp    $0x36ee80,%rbx
jae    1185 <milliseconds_to_string+0x65>
lea    0xe8f(%rip),%rcx
mov    $0xea60,%esi
jmp    11e5 <milliseconds_to_string+0xc5>
cmp    $0x5265c00,%rbx
jae    119c <milliseconds_to_string+0x7c>
lea    0xe7e(%rip),%rcx
mov    $0x36ee80,%esi
jmp    11e5 <milliseconds_to_string+0xc5>
cmp    $0x240c8400,%rbx
jae    11b3 <milliseconds_to_string+0x93>
lea    0xe6d(%rip),%rcx
mov    $0x5265c00,%esi
jmp    11e5 <milliseconds_to_string+0xc5>
mov    %rbx,%rax
shr    $0xc,%rax
cmp    $0x7528ad,%rax
mov    $0x240c8400,%eax
movabs $0x7528ad000,%rsi
cmovb  %rax,%rsi
lea    0xe45(%rip),%rax
lea    0xe44(%rip),%rcx
cmovb  %rax,%rcx
mov    %rbx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     11fb <milliseconds_to_string+0xdb>
mov    %rbx,%rax
cqto
idiv   %rsi
jmp    1201 <milliseconds_to_string+0xe1>
mov    %ebx,%eax
xor    %edx,%edx
div    %esi
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rcx,%rdx
mov    %rax,%rcx
call   1030 <snprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%lldms\00"
@.str.1 = [6 x i8] c"%llds\00"
@.str.2 = [6 x i8] c"%lldm\00"
@.str.3 = [6 x i8] c"%lldh\00"
@.str.4 = [6 x i8] c"%lldd\00"
@.str.5 = [6 x i8] c"%lldw\00"
@.str.6 = [6 x i8] c"%lldy\00"
define i8* @milliseconds_to_string(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %3
%7 = call i8* @malloc(i32 256)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %49
11: ; preds = %1
store i64 1, i64* %5
%12 = load i64, i64* %3
%13 = icmp slt i64 %12, 1000
br i1 %13, label %14, label %15
14: ; preds = %11
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %6
br label %41
15: ; preds = %11
%16 = load i64, i64* %3
%17 = icmp slt i64 %16, 60000
br i1 %17, label %18, label %19
18: ; preds = %15
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %6
store i64 1000, i64* %5
br label %40
19: ; preds = %15
%20 = load i64, i64* %3
%21 = icmp slt i64 %20, 3600000
br i1 %21, label %22, label %23
22: ; preds = %19
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %6
store i64 60000, i64* %5
br label %39
23: ; preds = %19
%24 = load i64, i64* %3
%25 = icmp slt i64 %24, 86400000
br i1 %25, label %26, label %27
26: ; preds = %23
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %6
store i64 3600000, i64* %5
br label %38
27: ; preds = %23
%28 = load i64, i64* %3
%29 = icmp slt i64 %28, 604800000
br i1 %29, label %30, label %31
30: ; preds = %27
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8** %6
store i64 86400000, i64* %5
br label %37
31: ; preds = %27
%32 = load i64, i64* %3
%33 = icmp slt i64 %32, 31449600000
br i1 %33, label %34, label %35
34: ; preds = %31
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8** %6
store i64 604800000, i64* %5
br label %36
35: ; preds = %31
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8** %6
store i64 31449600000, i64* %5
br label %36
36: ; preds = %35, %34
br label %37
37: ; preds = %36, %30
br label %38
38: ; preds = %37, %26
br label %39
39: ; preds = %38, %22
br label %40
40: ; preds = %39, %18
br label %41
41: ; preds = %40, %14
%42 = load i8*, i8** %4
%43 = load i8*, i8** %6
%44 = load i64, i64* %3
%45 = load i64, i64* %5
%46 = sdiv i64 %44, %45
%47 = call i32 @snprintf(i8* %42, i32 256, i8* %43, i64 %46)
%48 = load i8*, i8** %4
store i8* %48, i8** %2
br label %49
49: ; preds = %41, %10
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@.str = [7 x i8] c"%lldms\00"
@.str.1 = [6 x i8] c"%llds\00"
@.str.2 = [6 x i8] c"%lldm\00"
@.str.3 = [6 x i8] c"%lldh\00"
@.str.4 = [6 x i8] c"%lldd\00"
@.str.5 = [6 x i8] c"%lldw\00"
@.str.6 = [6 x i8] c"%lldy\00"
define i8* @milliseconds_to_string(i64 %0) {
%2 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%3 = icmp eq i8* %2, null
br i1 %3, label %23, label %4
4: ; preds = %1
%5 = icmp slt i64 %0, 1000
br i1 %5, label %18, label %6
6: ; preds = %4
%7 = icmp ult i64 %0, 60000
br i1 %7, label %18, label %8
8: ; preds = %6
%9 = icmp ult i64 %0, 3600000
br i1 %9, label %18, label %10
10: ; preds = %8
%11 = icmp ult i64 %0, 86400000
br i1 %11, label %18, label %12
12: ; preds = %10
%13 = icmp ult i64 %0, 604800000
br i1 %13, label %18, label %14
14: ; preds = %12
%15 = icmp ult i64 %0, 31449600000
%16 = select i1 %15, i64 604800000, i64 31449600000
%17 = select i1 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0)
br label %18
18: ; preds = %14, %12, %10, %8, %6, %4
%19 = phi i64 [ 1, %4 ], [ 1000, %6 ], [ 60000, %8 ], [ 3600000, %10 ], [ 86400000, %12 ], [ %16, %14 ]
%20 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %10 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), %12 ], [ %17, %14 ]
%21 = sdiv i64 %0, %19
%22 = tail call i32 @snprintf(i8* %2, i32 256, i8* %20, i64 %21)
br label %23
23: ; preds = %1, %18
ret i8* %2
}