#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 */
typedef int uint;

/* Variables and functions */
int abs(int const);
int /*<<< orphan*/ ceil(int /*<<< orphan*/);
int /*<<< orphan*/ log10(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int const);
char *intToString(int const n) {
  int nSameDigits = (n == 0) ? 1 : (abs(n) < 0 ? abs(n + 1) : abs(n));
  uint numDigits = (uint)ceil(log10(nSameDigits + 0.001));
  char *nAsStr = (char *)malloc((numDigits + 1 + 1) * sizeof(char));
  sprintf(nAsStr, "%i", n);
  return nAsStr;
}


x86 assembly (clang, -O0):
<intToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1172 <intToString+0x22>
mov    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a7 <intToString+0x57>
mov    -0x4(%rbp),%edi
call   1030 <abs@plt>
cmp    $0x0,%eax
jge    1196 <intToString+0x46>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1030 <abs@plt>
mov    %eax,-0x20(%rbp)
jmp    11a1 <intToString+0x51>
mov    -0x4(%rbp),%edi
call   1030 <abs@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8(%rbp)
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  0xe46(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1060 <log10@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe0f(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intToString>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
test   %edi,%edi
je     1162 <intToString+0x22>
mov    %ebp,%eax
neg    %eax
cmovs  %ebp,%eax
cvtsi2sd %eax,%xmm0
addsd  0xea4(%rip),%xmm0
cvttsd2si %xmm0,%edi
jmp    1167 <intToString+0x27>
mov    $0x1,%edi
call   1050 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
lea    0x2(%rax),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
lea    0xe83(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1060 <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* @intToString(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
br label %22
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i32 @abs(i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
%16 = call i32 @abs(i32 %15)
br label %20
17: ; preds = %9
%18 = load i32, i32* %2
%19 = call i32 @abs(i32 %18)
br label %20
20: ; preds = %17, %13
%21 = phi i32 [ %16, %13 ], [ %19, %17 ]
br label %22
22: ; preds = %20, %8
%23 = phi i32 [ 1, %8 ], [ %21, %20 ]
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = sitofp i32 %24 to double
%26 = fadd double %25, 1.000000e-03
%27 = fptosi double %26 to i32
%28 = call i32 @log10(i32 %27)
%29 = call i32 @ceil(i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
%32 = add nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = mul i64 %33, 1
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %5
%38 = load i8*, i8** %5
%39 = load i32, i32* %2
%40 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = load i8*, i8** %5
ret i8* %41
}

LLVM IR (-O2):
@.str = [3 x i8] c"%i\00"
define i8* @intToString(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %8, label %3
3: ; preds = %1
%4 = tail call i32 @llvm.abs.i32(i32 %0, i1 true)
%5 = sitofp i32 %4 to double
%6 = fadd double %5, 1.000000e-03
%7 = fptosi double %6 to i32
br label %8
8: ; preds = %3, %1
%9 = phi i32 [ 1, %1 ], [ %7, %3 ]
%10 = tail call i32 @log10(i32 %9)
%11 = tail call i32 @ceil(i32 %10)
%12 = add nsw i32 %11, 2
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
ret i8* %14
}