#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*/ exit(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
void time_string(char *out, int len, long ms) {
  if (len < 13) {
    printf("error: time_string buffer too small\n");
    exit(1);
  }
  if (ms > 10000) {
    snprintf(out, len, "%9ld s", ms / 1000);
  } else {
    snprintf(out, len, "%8ld ms", ms);
  }
}


x86 assembly (clang, -O0):
<time_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0xd,-0xc(%rbp)
jge    1163 <time_string+0x33>
lea    0xeac(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x2710,-0x18(%rbp)
jle    119a <time_string+0x6a>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
lea    0xe95(%rip),%rdx
call   1040 <snprintf@plt>
jmp    11b1 <time_string+0x81>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rcx
lea    0xe80(%rip),%rdx
call   1040 <snprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_string>:
cmp    $0xc,%esi
jle    1174 <time_string+0x44>
mov    %rdx,%rax
cmp    $0x2711,%rdx
jl     1165 <time_string+0x35>
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
mov    %rdx,%rcx
shr    $0x4,%rcx
lea    0xea0(%rip),%rdx
jmp    1040 <snprintf@plt>
lea    0xe9b(%rip),%rdx
mov    %rax,%rcx
jmp    1040 <snprintf@plt>
push   %rax
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"error: time_string buffer too small\0A\00"
@.str.1 = [7 x i8] c"%9ld s\00"
@.str.2 = [8 x i8] c"%8ld ms\00"
define void @time_string(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 13
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = load i64, i64* %6
%14 = icmp sgt i64 %13, 10000
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = load i32, i32* %5
%18 = load i64, i64* %6
%19 = sdiv i64 %18, 1000
%20 = call i32 @snprintf(i8* %16, i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %19)
br label %26
21: ; preds = %12
%22 = load i8*, i8** %4
%23 = load i32, i32* %5
%24 = load i64, i64* %6
%25 = call i32 @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 %24)
br label %26
26: ; preds = %21, %15
ret void
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"%9ld s\00"
@.str.2 = [8 x i8] c"%8ld ms\00"
@str = [36 x i8] c"error: time_string buffer too small\00"
define void @time_string(i8* %0, i32 %1, i64 %2) {
%4 = icmp slt i32 %1, 13
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %3
%9 = icmp sgt i64 %2, 10000
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = udiv i64 %2, 1000
%12 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %11)
br label %15
13: ; preds = %8
%14 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 %2)
br label %15
15: ; preds = %13, %10
ret void
}