#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 *malloc(char);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *itoa(int n) {
  char *tmp;
  char i = 1;
  int x = n;
  while (x /= 10)
    i++;
  tmp = malloc(i + 1);
  sprintf(tmp, "%d", n);
  return tmp;
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movb   $0x1,-0x11(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1159 <itoa+0x39>
mov    -0x11(%rbp),%al
add    $0x1,%al
mov    %al,-0x11(%rbp)
jmp    1135 <itoa+0x15>
movsbl -0x11(%rbp),%eax
add    $0x1,%eax
movsbl %al,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe86(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0x9(%r14),%ecx
mov    $0x2,%al
cmp    $0x13,%ecx
jb     1164 <itoa+0x44>
mov    $0x2,%al
mov    %r14d,%ecx
nopw   0x0(%rax,%rax,1)
movslq %ecx,%rcx
imul   $0x66666667,%rcx,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x22,%rdx
lea    (%rdx,%rsi,1),%ecx
add    $0x1,%al
add    %esi,%edx
add    $0x9,%edx
cmp    $0x12,%edx
ja     1140 <itoa+0x20>
movsbl %al,%edi
call   1030 <malloc@plt>
mov    %rax,%rbx
lea    0xe8a(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   1040 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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