#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 /*<<< orphan*/ abs(int);
char *calloc(int, int);
int floor(int /*<<< orphan*/);
int /*<<< orphan*/ log10(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *integer_to_string(int n) {
  int len = n == 0 ? 1 : floor(log10(abs(n))) + 1;
  if (n < 0)
    len++;
  char *buf = calloc(sizeof(char), len + 1);
  snprintf(buf, len + 1, "%i", n);
  return buf;
}


x86 assembly (clang, -O0):
<integer_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1172 <integer_to_string+0x22>
mov    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118e <integer_to_string+0x3e>
mov    -0x4(%rbp),%edi
call   1030 <abs@plt>
mov    %eax,%edi
call   1060 <log10@plt>
mov    %eax,%edi
call   1070 <floor@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11a7 <integer_to_string+0x57>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
add    $0x1,%esi
mov    -0x4(%rbp),%ecx
lea    0xe31(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integer_to_string>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
test   %edi,%edi
je     1165 <integer_to_string+0x25>
mov    %r14d,%edi
neg    %edi
cmovs  %r14d,%edi
call   1050 <log10@plt>
mov    %eax,%edi
call   1060 <floor@plt>
add    $0x1,%eax
jmp    116a <integer_to_string+0x2a>
mov    $0x1,%eax
mov    %r14d,%ecx
shr    $0x1f,%ecx
lea    (%rcx,%rax,1),%ebp
add    $0x1,%ebp
mov    $0x1,%edi
mov    %ebp,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
lea    0xe74(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14d,%ecx
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%i\00"
define i8* @integer_to_string(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
br label %14
8: ; preds = %1
%9 = load i32, i32* %2
%10 = call i32 @abs(i32 %9)
%11 = call i32 @log10(i32 %10)
%12 = call i32 @floor(i32 %11)
%13 = add nsw i32 %12, 1
br label %14
14: ; preds = %8, %7
%15 = phi i32 [ 1, %7 ], [ %13, %8 ]
store i32 %15, i32* %3
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
store i32 %20, i32* %3
br label %21
21: ; preds = %18, %14
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
%24 = call i8* @calloc(i32 1, i32 %23)
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
%28 = load i32, i32* %2
%29 = call i32 @snprintf(i8* %25, i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %4
ret i8* %30
}

LLVM IR (-O2):
@.str = [3 x i8] c"%i\00"
define i8* @integer_to_string(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 = tail call i32 @log10(i32 %4)
%6 = tail call i32 @floor(i32 %5)
%7 = add nsw i32 %6, 1
br label %8
8: ; preds = %1, %3
%9 = phi i32 [ %7, %3 ], [ 1, %1 ]
%10 = lshr i32 %0, 31
%11 = add nuw nsw i32 %10, 1
%12 = add i32 %11, %9
%13 = tail call i8* @calloc(i32 1, i32 %12)
%14 = tail call i32 @snprintf(i8* %13, i32 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
ret i8* %13
}