#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 /*<<< orphan*/ ceil(int /*<<< orphan*/);
int labs(long const);
int /*<<< orphan*/ log10(long);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, long const);
char *longToString(long const n) {
  long nSameDigits = (n == 0) ? 1 : (labs(n) < 0 ? labs(n + 1) : labs(n));
  uint numDigits = (uint)ceil(log10(nSameDigits + 0.001));
  char *nAsStr = (char *)malloc((numDigits + 1 + 1) * sizeof(char));
  sprintf(nAsStr, "%ld", n);
  return nAsStr;
}


x86 assembly (clang, -O0):
<longToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1174 <longToString+0x24>
mov    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ad <longToString+0x5d>
mov    -0x8(%rbp),%rdi
call   1030 <labs@plt>
cmp    $0x0,%eax
jge    119b <longToString+0x4b>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1030 <labs@plt>
mov    %eax,-0x28(%rbp)
jmp    11a7 <longToString+0x57>
mov    -0x8(%rbp),%rdi
call   1030 <labs@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cltq
mov    %rax,-0x10(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  0xe3c(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rdi
call   1060 <log10@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<longToString>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdi,%rdi
je     1183 <longToString+0x33>
mov    %r14,%rdi
call   1030 <labs@plt>
shr    $0x1f,%eax
lea    (%rax,%r14,1),%rdi
call   1030 <labs@plt>
cvtsi2sd %eax,%xmm0
addsd  0xe84(%rip),%xmm0
cvttsd2si %xmm0,%rdi
jmp    1188 <longToString+0x38>
mov    $0x1,%edi
call   1060 <log10@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
lea    0x2(%rax),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xe62(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1070 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%ld\00"
define i8* @longToString(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
br label %22
9: ; preds = %1
%10 = load i64, i64* %2
%11 = call i32 @labs(i64 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i64, i64* %2
%15 = add nsw i64 %14, 1
%16 = call i32 @labs(i64 %15)
br label %20
17: ; preds = %9
%18 = load i64, i64* %2
%19 = call i32 @labs(i64 %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 ]
%24 = sext i32 %23 to i64
store i64 %24, i64* %3
%25 = load i64, i64* %3
%26 = sitofp i64 %25 to double
%27 = fadd double %26, 1.000000e-03
%28 = fptosi double %27 to i64
%29 = call i32 @log10(i64 %28)
%30 = call i32 @ceil(i32 %29)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = mul i64 %34, 1
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %5
%39 = load i8*, i8** %5
%40 = load i64, i64* %2
%41 = call i32 @sprintf(i8* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %40)
%42 = load i8*, i8** %5
ret i8* %42
}

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