#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 abs(int);
int intlen(int);
char *malloc(int);
char *itoa(int i) {
  char *buf = malloc(intlen(i) + (i < 0 ? 2 : 1));
  char *p = buf + intlen(i) + (i < 0 ? 2 : 1);
  *--p = '\0';
  long j = 1;
  do {
    *--p = '0' + (abs(i / j) % 10);
    j *= 10;
  } while (i / j);
  if (i < 0)
    *--p = '-';
  return buf;
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <intlen@plt>
mov    %eax,%edi
mov    -0x4(%rbp),%edx
mov    $0x1,%eax
mov    $0x2,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
add    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <intlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    -0x4(%rbp),%esi
mov    $0x1,%ecx
mov    $0x2,%edx
cmp    $0x0,%esi
cmovl  %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,-0x1(%rax)
movq   $0x1,-0x20(%rbp)
movslq -0x4(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    %eax,%edi
call   1040 <abs@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,-0x1(%rax)
imul   $0xa,-0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
movslq -0x4(%rbp),%rax
cqto
idivq  -0x20(%rbp)
cmp    $0x0,%rax
jne    11b7 <itoa+0x87>
cmpl   $0x0,-0x4(%rbp)
jge    1221 <itoa+0xf1>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x2d,-0x1(%rax)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%ebx
call   1030 <intlen@plt>
mov    %ebx,%ecx
shr    $0x1f,%ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
mov    %ecx,%r15d
add    $0x1,%r15d
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1030 <intlen@plt>
cltq
lea    (%r14,%rax,1),%rcx
movb   $0x0,-0x1(%r15,%rcx,1)
movslq %ebx,%rcx
add    %r15,%rax
lea    (%r14,%rax,1),%rsi
add    $0xfffffffffffffffe,%rsi
mov    $0x1,%edi
mov    $0xcccccccd,%r8d
jmp    1191 <itoa+0x71>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rcx,%rax
cqto
idiv   %rdi
add    $0xffffffffffffffff,%rsi
test   %rax,%rax
je     11ef <itoa+0xcf>
mov    %rcx,%rax
or     %rdi,%rax
shr    $0x20,%rax
je     11b0 <itoa+0x90>
mov    %rcx,%rax
cqto
idiv   %rdi
jmp    11b6 <itoa+0x96>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%eax
xor    %edx,%edx
div    %edi
mov    %eax,%edx
neg    %edx
cmovs  %eax,%edx
mov    %rdx,%rax
imul   %r8,%rax
shr    $0x23,%rax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%edx
or     $0x30,%dl
mov    %dl,(%rsi)
add    %rdi,%rdi
lea    (%rdi,%rdi,4),%rdi
mov    %rcx,%rax
or     %rdi,%rax
shr    $0x20,%rax
jne    1180 <itoa+0x60>
mov    %ebx,%eax
xor    %edx,%edx
div    %edi
jmp    1188 <itoa+0x68>
test   %ebx,%ebx
jns    11f6 <itoa+0xd6>
movb   $0x2d,(%rsi)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @itoa(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @intlen(i32 %6)
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
%10 = zext i1 %9 to i64
%11 = select i1 %9, i32 2, i32 1
%12 = add nsw i32 %7, %11
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = load i32, i32* %2
%16 = call i32 @intlen(i32 %15)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
%19 = load i32, i32* %2
%20 = icmp slt i32 %19, 0
%21 = zext i1 %20 to i64
%22 = select i1 %20, i32 2, i32 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %18, i64 %23
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %4
store i8 0, i8* %26
store i64 1, i64* %5
br label %27
27: ; preds = %41, %1
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = load i64, i64* %5
%31 = sdiv i64 %29, %30
%32 = trunc i64 %31 to i32
%33 = call i32 @abs(i32 %32)
%34 = srem i32 %33, 10
%35 = add nsw i32 48, %34
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i32 -1
store i8* %38, i8** %4
store i8 %36, i8* %38
%39 = load i64, i64* %5
%40 = mul nsw i64 %39, 10
store i64 %40, i64* %5
br label %41
41: ; preds = %27
%42 = load i32, i32* %2
%43 = sext i32 %42 to i64
%44 = load i64, i64* %5
%45 = sdiv i64 %43, %44
%46 = icmp ne i64 %45, 0
br i1 %46, label %27, label %47
47: ; preds = %41
%48 = load i32, i32* %2
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i8*, i8** %4
%52 = getelementptr inbounds i8, i8* %51, i32 -1
store i8* %52, i8** %4
store i8 45, i8* %52
br label %53
53: ; preds = %50, %47
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
define noalias i8* @itoa(i32 %0) {
%2 = tail call i32 @intlen(i32 %0)
%3 = icmp slt i32 %0, 0
%4 = select i1 %3, i32 2, i32 1
%5 = add nsw i32 %2, %4
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 @intlen(i32 %0)
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %6, i64 %8
%10 = zext i32 %4 to i64
%11 = getelementptr inbounds i8, i8* %9, i64 %10
%12 = getelementptr inbounds i8, i8* %11, i64 -1
store i8 0, i8* %12
%13 = sext i32 %0 to i64
br label %14
14: ; preds = %14, %1
%15 = phi i8* [ %12, %1 ], [ %23, %14 ]
%16 = phi i64 [ 1, %1 ], [ %24, %14 ]
%17 = sdiv i64 %13, %16
%18 = trunc i64 %17 to i32
%19 = tail call i32 @llvm.abs.i32(i32 %18, i1 true)
%20 = urem i32 %19, 10
%21 = trunc i32 %20 to i8
%22 = or i8 %21, 48
%23 = getelementptr inbounds i8, i8* %15, i64 -1
store i8 %22, i8* %23
%24 = mul nuw nsw i64 %16, 10
%25 = sdiv i64 %13, %24
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %14
27: ; preds = %14
br i1 %3, label %28, label %30
28: ; preds = %27
%29 = getelementptr inbounds i8, i8* %15, i64 -2
store i8 45, i8* %29
br label %30
30: ; preds = %28, %27
ret i8* %6
}