#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 *, unsigned char);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
scalar_t__ strncmp(char *, char *, int);
void test_printf_c(void) {
  char *buf = (char *)malloc(2);
  char buf2[2];
  unsigned char i = 0xff;
  int r;
  while (i) {
    r = skiboot_snprintf(buf, 2, 2, "%c", i);
    snprintf(buf2, 2, "%c", i);
    assert(r == 1);
    assert(0 == strncmp(buf, buf2, 2));
    i--;
  }
  free(buf);
}


x86 assembly (clang, -O0):
<test_printf_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x8(%rbp)
movb   $0xff,-0xb(%rbp)
cmpb   $0x0,-0xb(%rbp)
je     1201 <test_printf_c+0xa1>
mov    -0x8(%rbp),%rdi
mov    $0x2,%edx
lea    0xe6c(%rip),%rcx
mov    %edx,%esi
movzbl -0xb(%rbp),%r8d
call   1040 <skiboot_snprintf@plt>
mov    %eax,-0x10(%rbp)
lea    -0xa(%rbp),%rdi
mov    $0x2,%esi
lea    0xe4d(%rip),%rdx
movzbl -0xb(%rbp),%ecx
call   1060 <snprintf@plt>
cmpl   $0x1,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
lea    -0xa(%rbp),%rsi
mov    $0x2,%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    -0xb(%rbp),%al
add    $0xff,%al
mov    %al,-0xb(%rbp)
jmp    117a <test_printf_c+0x1a>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_printf_c>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    $0xff,%bpl
lea    0xe7d(%rip),%r14
lea    0xe(%rsp),%r15
nopl   0x0(%rax,%rax,1)
movzbl %bpl,%ebp
mov    %r12,%rdi
mov    $0x2,%esi
mov    $0x2,%edx
mov    %r14,%rcx
mov    %ebp,%r8d
call   1040 <skiboot_snprintf@plt>
mov    %eax,%ebx
mov    %r15,%rdi
mov    $0x2,%esi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1060 <snprintf@plt>
xor    %edi,%edi
cmp    $0x1,%ebx
sete   %dil
call   1070 <assert@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
add    $0xff,%bpl
jne    1190 <test_printf_c+0x30>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%c\00"
define void @test_printf_c() {
%1 = alloca i8*
%2 = alloca [2 x i8]
%3 = alloca i8
%4 = alloca i32
%5 = call i64 @malloc(i32 2)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %1
store i8 -1, i8* %3
br label %7
7: ; preds = %10, %0
%8 = load i8, i8* %3
%9 = icmp ne i8 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %7
%11 = load i8*, i8** %1
%12 = load i8, i8* %3
%13 = call i32 @skiboot_snprintf(i8* %11, i32 2, i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 zeroext %12)
store i32 %13, i32* %4
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%15 = load i8, i8* %3
%16 = call i32 @snprintf(i8* %14, i32 2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 zeroext %15)
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 1
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i8*, i8** %1
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%23 = call i64 @strncmp(i8* %21, i8* %22, i32 2)
%24 = icmp eq i64 0, %23
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i8, i8* %3
%28 = add i8 %27, -1
store i8 %28, i8* %3
br label %7
29: ; preds = %7
%30 = load i8*, i8** %1
%31 = call i32 @free(i8* %30)
ret void
}

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