#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__ calloc(int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *uint_to_str(int i) {
  int tmp = i;
  int dig = 0;
  char *ret;
  if (i < 0)
    return NULL;
  do {
    dig++;
    tmp = tmp / 10;
  } while (tmp > 0);
  if ((ret = (char *)calloc(dig + 1, sizeof(char))) == NULL) {
    return NULL;
  }
  sprintf(ret, "%d", i);
  return ret;
}


x86 assembly (clang, -O0):
<uint_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    114f <uint_to_str+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11bb <uint_to_str+0x9b>
jmp    1154 <uint_to_str+0x34>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     1154 <uint_to_str+0x34>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a0 <uint_to_str+0x80>
movq   $0x0,-0x8(%rbp)
jmp    11bb <uint_to_str+0x9b>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe52(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uint_to_str>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
js     1174 <uint_to_str+0x54>
mov    %edi,%ebp
mov    $0x1,%edi
mov    %ebp,%eax
movslq %eax,%rcx
imul   $0x66666667,%rcx,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rax
add    %edx,%eax
add    $0x1,%edi
cmp    $0x9,%ecx
jg     1130 <uint_to_str+0x10>
mov    $0x1,%esi
call   1030 <calloc@plt>
test   %rax,%rax
je     1174 <uint_to_str+0x54>
mov    %rax,%rbx
lea    0xe98(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1040 <sprintf@plt>
jmp    1176 <uint_to_str+0x56>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i8* @uint_to_str(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %16, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %7, %3 ], [ %0, %1 ]
%5 = phi i32 [ %6, %3 ], [ 0, %1 ]
%6 = add nuw nsw i32 %5, 1
%7 = sdiv i32 %4, 10
%8 = icmp sgt i32 %4, 9
br i1 %8, label %3, label %9
9: ; preds = %3
%10 = add nuw nsw i32 %5, 2
%11 = tail call i64 @calloc(i32 %10, i32 1)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = tail call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %16
16: ; preds = %9, %1, %14
%17 = phi i8* [ %12, %14 ], [ null, %1 ], [ null, %9 ]
ret i8* %17
}