#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 skiboot_snprintf(char *, int, int, char const *, int);
int /*<<< orphan*/ snprintf(char *, int, char const *, int);
scalar_t__ strncmp(char *, char *, int);
void test_printf_x(const char *f) {
  char *buf, *buf2;
  int blen;
  int i, r, n = 0x1;
  for (i = 0; i < 8; i++) {
    blen = i + 2;
    buf = (char *)malloc(blen);
    buf2 = (char *)malloc(blen);
    r = skiboot_snprintf(buf, blen, blen, f, n);
    snprintf(buf2, blen, f, n);
    assert(i + 1 == r);
    assert(0 == strncmp(buf, buf2, blen));
    free(buf);
    free(buf2);
    n = n << 4;
  }
}


x86 assembly (clang, -O0):
<test_printf_x>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x8,-0x20(%rbp)
jge    1236 <test_printf_x+0xd6>
mov    -0x20(%rbp),%eax
add    $0x2,%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    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%r8d
call   1040 <skiboot_snprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%ecx
call   1060 <snprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cmp    -0x24(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
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    -0x28(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    117a <test_printf_x+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_printf_x>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1,%ebp
mov    $0x2,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebx,%edx
mov    %r14,%rcx
mov    %ebp,%r8d
call   1040 <skiboot_snprintf@plt>
mov    %eax,%r13d
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1060 <snprintf@plt>
lea    -0x1(%rbx),%eax
xor    %edi,%edi
cmp    %r13d,%eax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%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>
shl    $0x4,%ebp
add    $0x1,%ebx
cmp    $0xa,%ebx
jne    1180 <test_printf_x+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @test_printf_x(i8* %0) {
br label %2
2: ; preds = %1, %2
%3 = phi i32 [ 1, %1 ], [ %22, %2 ]
%4 = phi i32 [ 0, %1 ], [ %12, %2 ]
%5 = add nuw nsw i32 %4, 2
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i64 @malloc(i32 %5)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i32 @skiboot_snprintf(i8* %7, i32 %5, i32 %5, i8* %0, i32 %3)
%11 = tail call i32 @snprintf(i8* %9, i32 %5, i8* %0, i32 %3)
%12 = add nuw nsw i32 %4, 1
%13 = icmp eq i32 %12, %10
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i64 @strncmp(i8* %7, i8* %9, i32 %5)
%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* %7)
%21 = tail call i32 @free(i8* %9)
%22 = shl i32 %3, 4
%23 = icmp eq i32 %12, 8
br i1 %23, label %24, label %2
24: ; preds = %2
ret void
}