#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 */
scalar_t__ abs(int);
scalar_t__ floor(int /*<<< orphan*/);
int /*<<< orphan*/ log10(double);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *int_to_string(int value) {
  int digitcount;
  char *tmp_string;
  int increment = 2;
  if (value < 0) {
    increment += 1;
  }
  if (0 == value) {
    tmp_string = malloc(2 * sizeof(char));
    sprintf(tmp_string, "%u", 0);
  } else {
    digitcount = (int)floor(log10((double)abs(value))) + increment;
    tmp_string = malloc(digitcount * sizeof(char));
    sprintf(tmp_string, "%d", value);
  }
  return tmp_string;
}


x86 assembly (clang, -O0):
<int_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x2,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1175 <int_to_string+0x25>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x4(%rbp),%eax
jne    11a5 <int_to_string+0x55>
mov    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe67(%rip),%rsi
xor    %edx,%edx
call   1060 <sprintf@plt>
jmp    11ea <int_to_string+0x9a>
mov    -0x4(%rbp),%edi
call   1030 <abs@plt>
cvtsi2sd %rax,%xmm0
call   1050 <log10@plt>
mov    %eax,%edi
call   1070 <floor@plt>
add    -0x14(%rbp),%eax
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe1e(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<int_to_string>:
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
je     1194 <int_to_string+0x44>
mov    %edi,%ebx
mov    %edi,%r14d
shr    $0x1f,%r14d
call   1030 <abs@plt>
cvtsi2sd %rax,%xmm0
call   1050 <log10@plt>
mov    %eax,%edi
call   1070 <floor@plt>
lea    (%r14,%rax,1),%edi
add    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
lea    0xe76(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
jmp    11ad <int_to_string+0x5d>
mov    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
lea    0xe58(%rip),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%u\00"
@.str.1 = [3 x i8] c"%d\00"
define i8* @int_to_string(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
store i32 2, i32* %5
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* %5
%10 = add nsw i32 %9, 1
store i32 %10, i32* %5
br label %11
11: ; preds = %8, %1
%12 = load i32, i32* %2
%13 = icmp eq i32 0, %12
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = call i8* @malloc(i32 2)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 0)
br label %35
18: ; preds = %11
%19 = load i32, i32* %2
%20 = call i64 @abs(i32 %19)
%21 = sitofp i64 %20 to double
%22 = call i32 @log10(double %21)
%23 = call i64 @floor(i32 %22)
%24 = trunc i64 %23 to i32
%25 = load i32, i32* %5
%26 = add nsw i32 %24, %25
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 1
%30 = trunc i64 %29 to i32
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %4
%32 = load i8*, i8** %4
%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)
br label %35
35: ; preds = %18, %14
%36 = load i8*, i8** %4
ret i8* %36
}

LLVM IR (-O2):
@.str = [3 x i8] c"%u\00"
@.str.1 = [3 x i8] c"%d\00"
define noalias i8* @int_to_string(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
%5 = tail call i32 @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 0)
br label %17
6: ; preds = %1
%7 = icmp slt i32 %0, 0
%8 = select i1 %7, i32 3, i32 2
%9 = tail call i64 @abs(i32 %0)
%10 = sitofp i64 %9 to double
%11 = tail call i32 @log10(double %10)
%12 = tail call i64 @floor(i32 %11)
%13 = trunc i64 %12 to i32
%14 = add nsw i32 %8, %13
%15 = tail call i8* @malloc(i32 %14)
%16 = tail call i32 @sprintf(i8* dereferenceable(1) %15, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %17
17: ; preds = %6, %3
%18 = phi i8* [ %4, %3 ], [ %15, %6 ]
ret i8* %18
}