#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 */
double LLONG_MAX;
int /*<<< orphan*/ assert(int);
scalar_t__ llround(double);
double pow(int, unsigned int const);
void ulltoa(char *str, unsigned long long val, const unsigned int len) {
  assert(str != NULL);
  assert(len > 0);
  double res = pow(10, len - 1);
  assert(res > 0);
  assert(res <= LLONG_MAX);
  unsigned long long div = (unsigned long long)llround(res);
  for (unsigned long long digit = 0; div > 0; div /= 10) {
    digit = val / div;
    *(str++) = (char)('0' + digit);
    val -= digit * div;
  }
}


x86 assembly (clang, -O0):
<ulltoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0x14(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
mov    $0xa,%edi
call   1050 <pow@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movsd  -0x20(%rbp),%xmm1
mov    0x2e48(%rip),%rax
movsd  (%rax),%xmm0
ucomisd %xmm1,%xmm0
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movsd  -0x20(%rbp),%xmm0
call   1030 <llround@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    122c <ulltoa+0xfc>
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x28(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x30,%rax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x30(%rbp),%rcx
imul   -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x28(%rbp)
jmp    11cb <ulltoa+0x9b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ulltoa>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
add    $0xffffffff,%ebp
setb   %dil
call   1040 <assert@plt>
mov    $0xa,%edi
mov    %ebp,%esi
call   1050 <pow@plt>
movsd  %xmm0,0x8(%rsp)
xorpd  %xmm1,%xmm1
xor    %edi,%edi
ucomisd %xmm1,%xmm0
seta   %dil
call   1040 <assert@plt>
mov    0x2e60(%rip),%rax
movsd  (%rax),%xmm0
xor    %edi,%edi
ucomisd 0x8(%rsp),%xmm0
setae  %dil
call   1040 <assert@plt>
movsd  0x8(%rsp),%xmm0
call   1030 <llround@plt>
test   %rax,%rax
je     11fe <ulltoa+0xce>
mov    %rax,%rcx
movabs $0xcccccccccccccccd,%rsi
cc cc cc
jmp    11e6 <ulltoa+0xb6>
nopl   0x0(%rax)
mov    %r14,%rax
xor    %edx,%edx
div    %rcx
mov    %rdx,%r14
add    $0x30,%al
mov    %al,(%rbx)
add    $0x1,%rbx
mov    %rcx,%rax
mul    %rsi
shr    $0x3,%rdx
cmp    $0xa,%rcx
mov    %rdx,%rcx
jb     11fe <ulltoa+0xce>
mov    %r14,%rax
or     %rcx,%rax
shr    $0x20,%rax
jne    11c0 <ulltoa+0x90>
mov    %r14d,%eax
xor    %edx,%edx
div    %ecx
mov    %edx,%r14d
jmp    11cb <ulltoa+0x9b>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@LLONG_MAX = global double 0.000000e+00
define void @ulltoa(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca double
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %6
%15 = icmp ugt i32 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32, i32* %6
%19 = sub i32 %18, 1
%20 = call double @pow(i32 10, i32 %19)
store double %20, double* %7
%21 = load double, double* %7
%22 = fcmp ogt double %21, 0.000000e+00
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load double, double* %7
%26 = load double, double* @LLONG_MAX
%27 = fcmp ole double %25, %26
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load double, double* %7
%31 = call i64 @llround(double %30)
store i64 %31, i64* %8
store i64 0, i64* %9
br label %32
32: ; preds = %49, %3
%33 = load i64, i64* %8
%34 = icmp ugt i64 %33, 0
br i1 %34, label %35, label %52
35: ; preds = %32
%36 = load i64, i64* %5
%37 = load i64, i64* %8
%38 = udiv i64 %36, %37
store i64 %38, i64* %9
%39 = load i64, i64* %9
%40 = add i64 48, %39
%41 = trunc i64 %40 to i8
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %4
store i8 %41, i8* %42
%44 = load i64, i64* %9
%45 = load i64, i64* %8
%46 = mul i64 %44, %45
%47 = load i64, i64* %5
%48 = sub i64 %47, %46
store i64 %48, i64* %5
br label %49
49: ; preds = %35
%50 = load i64, i64* %8
%51 = udiv i64 %50, 10
store i64 %51, i64* %8
br label %32
52: ; preds = %32
ret void
}

LLVM IR (-O2):
@LLONG_MAX = global double 0.000000e+00
define void @ulltoa(i8* writeonly %0, i64 %1, i32 %2) {
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i32 %2, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = add i32 %2, -1
%11 = tail call double @pow(i32 10, i32 %10)
%12 = fcmp ogt double %11, 0.000000e+00
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = load double, double* @LLONG_MAX
%16 = fcmp ole double %11, %15
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = tail call i64 @llround(double %11)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %22, %3
ret void
22: ; preds = %3, %22
%23 = phi i64 [ %32, %22 ], [ %19, %3 ]
%24 = phi i8* [ %29, %22 ], [ %0, %3 ]
%25 = phi i64 [ %31, %22 ], [ %1, %3 ]
%26 = udiv i64 %25, %23
%27 = trunc i64 %26 to i8
%28 = add i8 %27, 48
%29 = getelementptr inbounds i8, i8* %24, i64 1
store i8 %28, i8* %24
%30 = mul nuw i64 %26, %23
%31 = urem i64 %25, %23
%32 = udiv i64 %23, 10
%33 = icmp ult i64 %23, 10
br i1 %33, label %21, label %22
}