#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 *calloc(int, unsigned long long);
int /*<<< orphan*/ snprintf(char *, unsigned long long, char *, long long);
char *my_itoa(long long input) {
  unsigned long long len = 1;
  long long bufInput = (input < 0) ? -input : input;
  if (input < 0)
    ++len;
  while (bufInput) {
    ++len;
    bufInput /= 10;
  }
  char *bufStr = calloc(sizeof(char), ++len);
  snprintf(bufStr, len, "%lld", input);
  return bufStr;
}


x86 assembly (clang, -O0):
<my_itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x1,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    114e <my_itoa+0x2e>
xor    %eax,%eax
sub    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1156 <my_itoa+0x36>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1175 <my_itoa+0x55>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    117a <my_itoa+0x5a>
cmpq   $0x0,-0x18(%rbp)
je     11a8 <my_itoa+0x88>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
jmp    117a <my_itoa+0x5a>
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    %rsi,-0x10(%rbp)
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe2b(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_itoa>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    %rdi,%rcx
neg    %rcx
cmovs  %rdi,%rcx
mov    %rdi,%rbx
shr    $0x3f,%rbx
add    $0x1,%rbx
test   %rcx,%rcx
je     1175 <my_itoa+0x55>
movabs $0x6666666666666667,%rsi
66 66 66
nopl   0x0(%rax)
add    $0x1,%rbx
mov    %rcx,%rax
imul   %rsi
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
add    %rax,%rdx
add    $0x9,%rcx
cmp    $0x12,%rcx
mov    %rdx,%rcx
ja     1150 <my_itoa+0x30>
add    $0x1,%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
call   1040 <calloc@plt>
mov    %rax,%r15
lea    0xe70(%rip),%rdx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
define i8* @my_itoa(i64 %0) {
%2 = icmp slt i64 %0, 0
%3 = sub nsw i64 0, %0
%4 = select i1 %2, i64 %3, i64 %0
%5 = select i1 %2, i64 2, i64 1
%6 = icmp eq i64 %4, 0
br i1 %6, label %14, label %7
7: ; preds = %1, %7
%8 = phi i64 [ %10, %7 ], [ %5, %1 ]
%9 = phi i64 [ %11, %7 ], [ %4, %1 ]
%10 = add i64 %8, 1
%11 = sdiv i64 %9, 10
%12 = add i64 %9, 9
%13 = icmp ult i64 %12, 19
br i1 %13, label %14, label %7
14: ; preds = %7, %1
%15 = phi i64 [ %5, %1 ], [ %10, %7 ]
%16 = add i64 %15, 1
%17 = tail call i8* @calloc(i32 1, i64 %16)
%18 = tail call i32 @snprintf(i8* %17, i64 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %0)
ret i8* %17
}