#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*/ free(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
size_t strlen(char *);
char *IntegerToString(int n) {
  if (n / 10 == 0) {
    char buf[2];
    buf[0] = n + '0';
    buf[1] = 0;
    return strdup(buf);
  } else {
    char buf[20];
    char *prestr;
    memset(buf, 0, 20);
    prestr = IntegerToString(n / 10);
    strcpy(buf, prestr);
    free(prestr);
    buf[strlen(buf)] = ((n % 10) + '0');
    buf[strlen(buf)] = 0;
    return strdup(buf);
  }
}


x86 assembly (clang, -O0):
<IntegerToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%eax
jne    119e <IntegerToString+0x3e>
mov    -0xc(%rbp),%eax
add    $0x30,%eax
mov    %al,-0xe(%rbp)
movb   $0x0,-0xd(%rbp)
lea    -0xe(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1218 <IntegerToString+0xb8>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1060 <IntegerToString@plt>
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%al
mov    %al,-0x39(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x39(%rbp),%cl
mov    %cl,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<IntegerToString>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
lea    0x9(%r15),%eax
cmp    $0x12,%eax
ja     1189 <IntegerToString+0x29>
add    $0x30,%r15b
mov    %r15b,(%rsp)
movb   $0x0,0x1(%rsp)
mov    %rsp,%rdi
jmp    11f6 <IntegerToString+0x96>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
movslq %r15d,%rax
imul   $0x66666667,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x22,%rbx
add    %eax,%ebx
mov    %ebx,%edi
call   1060 <IntegerToString@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    %ebx,%ebx
lea    (%rbx,%rbx,4),%eax
sub    %eax,%r15d
add    $0x30,%r15b
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r15b,(%rsp,%rax,1)
mov    %r14,%rdi
call   1050 <strlen@plt>
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
call   1080 <strdup@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @IntegerToString(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [2 x i8]
%5 = alloca [20 x i8]
%6 = alloca i8*
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = sdiv i32 %7, 10
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i32, i32* %3
%12 = add nsw i32 %11, 48
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 %13, i8* %14
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 0, i8* %15
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %2
br label %41
18: ; preds = %1
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 20)
%21 = load i32, i32* %3
%22 = sdiv i32 %21, 10
%23 = call i8* @IntegerToString(i32 %22)
store i8* %23, i8** %6
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%25 = load i8*, i8** %6
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = load i8*, i8** %6
%28 = call i32 @free(i8* %27)
%29 = load i32, i32* %3
%30 = srem i32 %29, 10
%31 = add nsw i32 %30, 48
%32 = trunc i32 %31 to i8
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%34 = call i64 @strlen(i8* %33)
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 %34
store i8 %32, i8* %35
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%37 = call i64 @strlen(i8* %36)
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%40 = call i8* @strdup(i8* %39)
store i8* %40, i8** %2
br label %41
41: ; preds = %18, %10
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
define noalias i8* @IntegerToString(i32 %0) {
%2 = alloca [2 x i8]
%3 = alloca [20 x i8]
%4 = add i32 %0, 9
%5 = icmp ult i32 %4, 19
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%8 = trunc i32 %0 to i8
%9 = add nsw i8 %8, 48
store i8 %9, i8* %7
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
store i8 0, i8* %10
%11 = call i8* @strdup(i8* %7)
br label %27
12: ; preds = %1
%13 = sdiv i32 %0, 10
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i32 0, i32 20)
%16 = call i8* @IntegerToString(i32 %13)
%17 = call i32 @strcpy(i8* %14, i8* %16)
%18 = call i32 @free(i8* %16)
%19 = srem i32 %0, 10
%20 = trunc i32 %19 to i8
%21 = add nsw i8 %20, 48
%22 = call i64 @strlen(i8* %14)
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %22
store i8 %21, i8* %23
%24 = call i64 @strlen(i8* %14)
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i8* @strdup(i8* %14)
br label %27
27: ; preds = %12, %6
%28 = phi i8* [ %11, %6 ], [ %26, %12 ]
ret i8* %28
}