#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__ lmn_malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, long);
char *int_to_str(long n) {
  char *s;
  int keta = 0;
  if (n == 0)
    keta = 1;
  else {
    int m = n;
    keta = 0;
    if (m < 0) {
      m = -m, keta = 1;
    }
    while (m > 0) {
      m /= 10;
      keta++;
    }
  }
  s = ((char *)lmn_malloc(sizeof(char) * (keta + 1)));
  sprintf(s, "%ld", n);
  return s;
}


x86 assembly (clang, -O0):
<int_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    114a <int_to_str+0x2a>
movl   $0x1,-0x14(%rbp)
jmp    11a1 <int_to_str+0x81>
mov    -0x8(%rbp),%rax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1171 <int_to_str+0x51>
xor    %eax,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
jmp    1176 <int_to_str+0x56>
cmpl   $0x0,-0x18(%rbp)
jle    119c <int_to_str+0x7c>
mov    -0x18(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1176 <int_to_str+0x56>
jmp    11a1 <int_to_str+0x81>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <lmn_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<int_to_str>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdi,%rdi
je     1166 <int_to_str+0x46>
test   %r14d,%r14d
je     116d <int_to_str+0x4d>
mov    %r14d,%edi
shr    $0x1f,%edi
mov    %r14d,%eax
neg    %eax
cmovs  %r14d,%eax
add    $0x1,%edi
mov    $0xcccccccd,%ecx
nopl   0x0(%rax,%rax,1)
mov    %eax,%edx
imul   %rcx,%rdx
shr    $0x23,%rdx
add    $0x1,%edi
cmp    $0x9,%eax
mov    %edx,%eax
ja     1150 <int_to_str+0x30>
jmp    1172 <int_to_str+0x52>
mov    $0x2,%edi
jmp    1172 <int_to_str+0x52>
mov    $0x1,%edi
call   1030 <lmn_malloc@plt>
mov    %rax,%rbx
lea    0xe7f(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <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* @int_to_str(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i64 %0, i64* %2
store i32 0, i32* %4
%6 = load i64, i64* %2
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 1, i32* %4
br label %27
9: ; preds = %1
%10 = load i64, i64* %2
%11 = trunc i64 %10 to i32
store i32 %11, i32* %5
store i32 0, i32* %4
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* %5
%16 = sub nsw i32 0, %15
store i32 %16, i32* %5
store i32 1, i32* %4
br label %17
17: ; preds = %14, %9
br label %18
18: ; preds = %21, %17
%19 = load i32, i32* %5
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* %5
%23 = sdiv i32 %22, 10
store i32 %23, i32* %5
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %18
26: ; preds = %18
br label %27
27: ; preds = %26, %8
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = mul i64 1, %30
%32 = trunc i64 %31 to i32
%33 = call i64 @lmn_malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %3
%35 = load i8*, i8** %3
%36 = load i64, i64* %2
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %36)
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
define i8* @int_to_str(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %17, label %3
3: ; preds = %1
%4 = trunc i64 %0 to i32
%5 = icmp eq i32 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = lshr i32 %4, 31
%8 = tail call i32 @llvm.abs.i32(i32 %4, i1 true)
br label %9
9: ; preds = %6, %9
%10 = phi i32 [ %12, %9 ], [ %8, %6 ]
%11 = phi i32 [ %13, %9 ], [ %7, %6 ]
%12 = udiv i32 %10, 10
%13 = add nuw nsw i32 %11, 1
%14 = icmp ult i32 %10, 10
br i1 %14, label %15, label %9
15: ; preds = %9
%16 = add nuw i32 %11, 2
br label %17
17: ; preds = %15, %3, %1
%18 = phi i32 [ 2, %1 ], [ 1, %3 ], [ %16, %15 ]
%19 = tail call i64 @lmn_malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i32 @sprintf(i8* dereferenceable(1) %20, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %0)
ret i8* %20
}