#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 *calloc(int, unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
char *itoaa(unsigned int i) {
  unsigned int tmp = i;
  unsigned int digits = 0;
  while (tmp) {
    digits++;
    tmp /= 10;
  }
  char *result = calloc(sizeof(char), digits + 1);
  sprintf(result, "%i", i);
  return result;
}


x86 assembly (clang, -O0):
<itoaa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     115f <itoaa+0x3f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x8(%rbp)
jmp    1138 <itoaa+0x18>
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe7f(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoaa>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x1,%esi
test   %edi,%edi
je     1154 <itoaa+0x34>
mov    $0xcccccccd,%eax
mov    %ebp,%ecx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ecx,%edx
imul   %rax,%rdx
shr    $0x23,%rdx
add    $0x1,%esi
cmp    $0x9,%ecx
mov    %edx,%ecx
ja     1140 <itoaa+0x20>
mov    $0x1,%edi
call   1030 <calloc@plt>
mov    %rax,%rbx
lea    0xe98(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1040 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%i\00"
define i8* @itoaa(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %2
%6 = load i32, i32* %2
store i32 %6, i32* %3
store i32 0, i32* %4
br label %7
7: ; preds = %10, %1
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = load i32, i32* %4
%12 = add i32 %11, 1
store i32 %12, i32* %4
%13 = load i32, i32* %3
%14 = udiv i32 %13, 10
store i32 %14, i32* %3
br label %7
15: ; preds = %7
%16 = load i32, i32* %4
%17 = add i32 %16, 1
%18 = call i8* @calloc(i32 1, i32 %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = load i32, i32* %2
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i8*, i8** %5
ret i8* %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"%i\00"
define noalias i8* @itoaa(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %9, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %6, %3 ], [ %0, %1 ]
%5 = phi i32 [ %7, %3 ], [ 1, %1 ]
%6 = udiv i32 %4, 10
%7 = add i32 %5, 1
%8 = icmp ult i32 %4, 10
br i1 %8, label %9, label %3
9: ; preds = %3, %1
%10 = phi i32 [ 1, %1 ], [ %7, %3 ]
%11 = tail call i8* @calloc(i32 1, i32 %10)
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
ret i8* %11
}