#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 */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ skiboot_snprintf(char *, int, int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strncmp(char *, char *, int);
void test_printf_0u(int n) {
  char *buf, *buf2;
  int blen;
  unsigned int i;
  for (i = 1; i < 10; i++) {
    blen = i + 1;
    if (n < 0)
      blen++;
    buf = (char *)malloc(blen);
    buf2 = (char *)malloc(blen);
    skiboot_snprintf(buf, blen, blen, "%08u", n);
    snprintf(buf2, blen, "%08u", n);
    n = n * 10;
    assert(0 == strncmp(buf, buf2, blen));
    free(buf);
    free(buf2);
  }
}


x86 assembly (clang, -O0):
<test_printf_0u>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0xa,-0x20(%rbp)
jae    122c <test_printf_0u+0xcc>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1198 <test_printf_0u+0x38>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x4(%rbp),%r8d
lea    0xe3b(%rip),%rcx
call   1040 <skiboot_snprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x4(%rbp),%ecx
lea    0xe25(%rip),%rdx
call   1060 <snprintf@plt>
imul   $0xa,-0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1172 <test_printf_0u+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_printf_0u>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x3,%r13d
lea    0xe86(%rip),%r14
nopw   0x0(%rax,%rax,1)
lea    -0x1(%r13),%ebp
test   %ebx,%ebx
cmovs  %r13d,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebp,%edx
mov    %r14,%rcx
mov    %ebx,%r8d
call   1040 <skiboot_snprintf@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
call   1060 <snprintf@plt>
add    %ebx,%ebx
lea    (%rbx,%rbx,4),%ebx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1050 <strncmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x1,%r13d
cmp    $0xc,%r13d
jne    1180 <test_printf_0u+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%08u\00"
define void @test_printf_0u(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %6
br label %7
7: ; preds = %47, %1
%8 = load i32, i32* %6
%9 = icmp ult i32 %8, 10
br i1 %9, label %10, label %50
10: ; preds = %7
%11 = load i32, i32* %6
%12 = add i32 %11, 1
store i32 %12, i32* %5
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* %5
%17 = add nsw i32 %16, 1
store i32 %17, i32* %5
br label %18
18: ; preds = %15, %10
%19 = load i32, i32* %5
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %3
%22 = load i32, i32* %5
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %4
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = load i32, i32* %5
%28 = load i32, i32* %2
%29 = call i32 @skiboot_snprintf(i8* %25, i32 %26, i32 %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = load i32, i32* %2
%33 = call i32 @snprintf(i8* %30, i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %2
%35 = mul nsw i32 %34, 10
store i32 %35, i32* %2
%36 = load i8*, i8** %3
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = call i64 @strncmp(i8* %36, i8* %37, i32 %38)
%40 = icmp eq i64 0, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i8*, i8** %3
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %4
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %18
%48 = load i32, i32* %6
%49 = add i32 %48, 1
store i32 %49, i32* %6
br label %7
50: ; preds = %7
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%08u\00"
define void @test_printf_0u(i32 %0) {
br label %2
2: ; preds = %1, %2
%3 = phi i32 [ 1, %1 ], [ %5, %2 ]
%4 = phi i32 [ %0, %1 ], [ %15, %2 ]
%5 = add nuw nsw i32 %3, 1
%6 = icmp slt i32 %4, 0
%7 = add nuw nsw i32 %3, 2
%8 = select i1 %6, i32 %7, i32 %5
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i64 @malloc(i32 %8)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @skiboot_snprintf(i8* %10, i32 %8, i32 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %4)
%14 = tail call i32 @snprintf(i8* %12, i32 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %4)
%15 = mul nsw i32 %4, 10
%16 = tail call i64 @strncmp(i8* %10, i8* %12, i32 %8)
%17 = icmp eq i64 %16, 0
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i32 @free(i8* %10)
%21 = tail call i32 @free(i8* %12)
%22 = icmp eq i32 %5, 10
br i1 %22, label %23, label %2
23: ; preds = %2
ret void
}