#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*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *ll2string(long long v) {
  int i, len = 2;
  long long tmp;
  char *buf;
  tmp = v;
  while ((tmp /= 10) > 0)
    len++;
  buf = malloc(len);
  if (!buf) {
    fprintf(stderr, "Exited, as malloc failed at ll2string.\n");
    exit(1);
  }
  i = len - 2;
  while (v > 0) {
    buf[i--] = v % 10 + '0';
    v /= 10;
  }
  buf[len - 1] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<ll2string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x2,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jle    1175 <ll2string+0x45>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114b <ll2string+0x1b>
mov    -0x10(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ab <ll2string+0x7b>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jle    11fd <ll2string+0xcd>
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
add    $0x30,%rdx
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%esi
add    $0xffffffff,%esi
mov    %esi,-0xc(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
jmp    11b4 <ll2string+0x84>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ll2string>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x2,%r14d
movabs $0xcccccccccccccccd,%r15
cc cc cc
cmp    $0xa,%rdi
jl     1177 <ll2string+0x47>
mov    $0x2,%r14d
mov    %rbx,%rcx
nopw   0x0(%rax,%rax,1)
mov    %rcx,%rax
mul    %r15
shr    $0x3,%rdx
add    $0x1,%r14d
cmp    $0x63,%rcx
mov    %rdx,%rcx
ja     1160 <ll2string+0x30>
mov    %r14d,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11d9 <ll2string+0xa9>
mov    %rax,%rcx
test   %rbx,%rbx
jle    11c8 <ll2string+0x98>
lea    -0x2(%r14),%eax
cltq
mov    %rcx,%rsi
add    %rax,%rsi
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rax
mul    %r15
shr    $0x3,%rdx
lea    (%rdx,%rdx,1),%eax
lea    (%rax,%rax,4),%eax
mov    %ebx,%edi
sub    %eax,%edi
or     $0x30,%dil
mov    %dil,(%rsi)
add    $0xffffffffffffffff,%rsi
cmp    $0x9,%rbx
mov    %rdx,%rbx
ja     11a0 <ll2string+0x70>
movslq %r14d,%rax
movb   $0x0,-0x1(%rax,%rcx,1)
mov    %rcx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Exited, as malloc failed at ll2string.\0A\00"
define i8* @ll2string(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %2
store i32 2, i32* %4
%7 = load i64, i64* %2
store i64 %7, i64* %5
br label %8
8: ; preds = %12, %1
%9 = load i64, i64* %5
%10 = sdiv i64 %9, 10
store i64 %10, i64* %5
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
store i32 %14, i32* %4
br label %8
15: ; preds = %8
%16 = load i32, i32* %4
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = load i32, i32* %4
%26 = sub nsw i32 %25, 2
store i32 %26, i32* %3
br label %27
27: ; preds = %30, %24
%28 = load i64, i64* %2
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = load i64, i64* %2
%32 = srem i64 %31, 10
%33 = add nsw i64 %32, 48
%34 = trunc i64 %33 to i8
%35 = load i8*, i8** %6
%36 = load i32, i32* %3
%37 = add nsw i32 %36, -1
store i32 %37, i32* %3
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 %34, i8* %39
%40 = load i64, i64* %2
%41 = sdiv i64 %40, 10
store i64 %41, i64* %2
br label %27
42: ; preds = %27
%43 = load i8*, i8** %6
%44 = load i32, i32* %4
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %6
ret i8* %48
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Exited, as malloc failed at ll2string.\0A\00"
define noalias i8* @ll2string(i64 %0) {
%2 = icmp sgt i64 %0, 9
br i1 %2, label %3, label %9
3: ; preds = %1, %3
%4 = phi i64 [ %6, %3 ], [ %0, %1 ]
%5 = phi i32 [ %7, %3 ], [ 2, %1 ]
%6 = udiv i64 %4, 10
%7 = add nuw nsw i32 %5, 1
%8 = icmp ugt i64 %4, 99
br i1 %8, label %3, label %9
9: ; preds = %3, %1
%10 = phi i32 [ 2, %1 ], [ %7, %3 ]
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = icmp sgt i64 %0, 0
br i1 %18, label %19, label %32
19: ; preds = %17
%20 = add i32 %10, -2
%21 = sext i32 %20 to i64
br label %22
22: ; preds = %19, %22
%23 = phi i64 [ %21, %19 ], [ %28, %22 ]
%24 = phi i64 [ %0, %19 ], [ %30, %22 ]
%25 = urem i64 %24, 10
%26 = trunc i64 %25 to i8
%27 = or i8 %26, 48
%28 = add i64 %23, -1
%29 = getelementptr inbounds i8, i8* %11, i64 %23
store i8 %27, i8* %29
%30 = udiv i64 %24, 10
%31 = icmp ult i64 %24, 10
br i1 %31, label %32, label %22
32: ; preds = %22, %17
%33 = add nsw i32 %10, -1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %11, i64 %34
store i8 0, i8* %35
ret i8* %11
}