#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__ ceil(int /*<<< orphan*/);
int /*<<< orphan*/ log10(long);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
char *tl_int_to_str(long i) {
  int len = (int)(ceil(log10(i)) + 2);
  char *res = (char *)malloc(len * sizeof(char));
  snprintf(res, len * sizeof(char), "%ld", i);
  if (len - 2 != 0) {
    res[len - 2] = '\0';
  } else {
    res[len - 1] = '\0';
  }
  return res;
}


x86 assembly (clang, -O0):
<tl_int_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
add    $0x2,%rax
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
lea    0xe71(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
cmp    $0x0,%eax
je     11b9 <tl_int_to_str+0x79>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ca <tl_int_to_str+0x8a>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tl_int_to_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1060 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    %rax,%r15
lea    0x2(%rax),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xe93(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    %r15,%rax
shl    $0x20,%rax
movabs $0x100000000,%rcx
add    %rax,%rcx
test   %r15d,%r15d
cmovne %rax,%rcx
sar    $0x20,%rcx
movb   $0x0,(%rbx,%rcx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
define i8* @tl_int_to_str(i64 %0) {
%2 = tail call i32 @log10(i64 %0)
%3 = tail call i64 @ceil(i32 %2)
%4 = trunc i64 %3 to i32
%5 = add i32 %4, 2
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @snprintf(i8* %7, i32 %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %0)
%9 = icmp eq i32 %4, 0
%10 = shl i64 %3, 32
%11 = add i64 %10, 4294967296
%12 = select i1 %9, i64 %11, i64 %10
%13 = ashr exact i64 %12, 32
%14 = getelementptr inbounds i8, i8* %7, i64 %13
store i8 0, i8* %14
ret i8* %7
}