#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 *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *itoa(int i) {
  char *a;
  int b = i;
  int c = ""[0];
  if (c != 1)
    c = 1;
  a = malloc(1);
  while (b > 9) {
    if (a != NULL)
      free(a);
    b /= 10;
    c += 1;
    a = malloc(c + 1);
  }
  sprintf(a, "%d", i);
  return a;
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
movsbl 0xeba(%rip),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     115c <itoa+0x2c>
movl   $0x1,-0x18(%rbp)
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x9,-0x14(%rbp)
jle    11b3 <itoa+0x83>
cmpq   $0x0,-0x10(%rbp)
je     1188 <itoa+0x58>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
jmp    116a <itoa+0x3a>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe3f(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
cmp    $0xa,%r14d
jl     11a0 <itoa+0x70>
mov    $0x3,%ebp
mov    $0xcccccccd,%r15d
mov    %r14d,%r13d
jmp    1191 <itoa+0x61>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%ebx
imul   %r15,%rbx
shr    $0x23,%rbx
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
add    $0x1,%ebp
cmp    $0x63,%r13d
mov    %ebx,%r13d
jbe    11a0 <itoa+0x70>
test   %r12,%r12
je     1170 <itoa+0x40>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1170 <itoa+0x40>
lea    0xe59(%rip),%rsi
mov    %r12,%rdi
mov    %r14d,%edx
call   1050 <sprintf@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [3 x i8] c"%d\00"
define i8* @itoa(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
store i32 %6, i32* %4
%7 = load i8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0)
%8 = sext i8 %7 to i32
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 1, i32* %5
br label %12
12: ; preds = %11, %1
%13 = call i8* @malloc(i32 1)
store i8* %13, i8** %3
br label %14
14: ; preds = %23, %12
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 9
br i1 %16, label %17, label %31
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i8*, i8** %3
%22 = call i32 @free(i8* %21)
br label %23
23: ; preds = %20, %17
%24 = load i32, i32* %4
%25 = sdiv i32 %24, 10
store i32 %25, i32* %4
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %3
br label %14
31: ; preds = %14
%32 = load i8*, i8** %3
%33 = load i32, i32* %2
%34 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
define i8* @itoa(i32 %0) {
%2 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%3 = icmp sgt i32 %0, 9
br i1 %3, label %4, label %17
4: ; preds = %1, %11
%5 = phi i32 [ %13, %11 ], [ 1, %1 ]
%6 = phi i32 [ %12, %11 ], [ %0, %1 ]
%7 = phi i8* [ %15, %11 ], [ %2, %1 ]
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 @free(i8* %7)
br label %11
11: ; preds = %9, %4
%12 = udiv i32 %6, 10
%13 = add nuw nsw i32 %5, 1
%14 = add nuw nsw i32 %5, 2
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp ugt i32 %6, 99
br i1 %16, label %4, label %17
17: ; preds = %11, %1
%18 = phi i8* [ %2, %1 ], [ %15, %11 ]
%19 = tail call i32 @sprintf(i8* dereferenceable(1) %18, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
ret i8* %18
}