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


x86 assembly (clang, -O0):
<uintToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1162 <uintToString+0x22>
mov    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1168 <uintToString+0x28>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8(%rbp)
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  0xe85(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1050 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe51(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uintToString>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
test   %edi,%edi
mov    $0x1,%eax
cmovne %edi,%eax
cvtsi2sd %eax,%xmm0
addsd  0xea5(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
lea    0x1(%rax),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
lea    0xe8b(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%u\00"
define i8* @uintToString(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
br label %11
9: ; preds = %1
%10 = load i32, i32* %2
br label %11
11: ; preds = %9, %8
%12 = phi i32 [ 1, %8 ], [ %10, %9 ]
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = sitofp i32 %13 to double
%15 = fadd double %14, 1.000000e-03
%16 = fptosi double %15 to i32
%17 = call i32 @log10(i32 %16)
%18 = call i32 @ceil(i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 1
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = load i32, i32* %2
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = load i8*, i8** %5
ret i8* %29
}

LLVM IR (-O2):
@.str = [3 x i8] c"%u\00"
define i8* @uintToString(i32 %0) {
%2 = icmp eq i32 %0, 0
%3 = select i1 %2, i32 1, i32 %0
%4 = sitofp i32 %3 to double
%5 = fadd double %4, 1.000000e-03
%6 = fptosi double %5 to i32
%7 = tail call i32 @log10(i32 %6)
%8 = tail call i32 @ceil(i32 %7)
%9 = add nsw i32 %8, 1
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
ret i8* %11
}