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


x86 assembly (clang, -O0):
<test_printf_d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    1267 <test_printf_d+0x107>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1198 <test_printf_d+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    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x4(%rbp),%ecx
lea    0xe22(%rip),%rdx
call   1060 <snprintf@plt>
imul   $0xa,-0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    120f <test_printf_d+0xaf>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cmp    -0x20(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
jmp    1222 <test_printf_d+0xc2>
mov    -0x24(%rbp),%eax
cmp    -0x20(%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    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1172 <test_printf_d+0x12>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_printf_d>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x3,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    -0x2(%r14),%eax
lea    -0x1(%r14),%r15d
test   %ebx,%ebx
mov    %r15d,%ebp
cmovs  %r14d,%ebp
cmovns %eax,%r15d
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %rax,(%rsp)
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %r13,%rdi
mov    %ebp,%esi
mov    %ebp,%edx
lea    0xe45(%rip),%rcx
mov    %ebx,%r8d
call   1040 <skiboot_snprintf@plt>
mov    %eax,%r13d
mov    %r12,%rdi
mov    %ebp,%esi
lea    0xe2e(%rip),%rdx
mov    %ebx,%ecx
call   1060 <snprintf@plt>
add    %ebx,%ebx
lea    (%rbx,%rbx,4),%ebx
xor    %edi,%edi
cmp    %r13d,%r15d
sete   %dil
call   1070 <assert@plt>
mov    (%rsp),%r15
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,%r14d
cmp    $0xc,%r14d
jne    1180 <test_printf_d+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define void @test_printf_d(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 ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %4)
%14 = tail call i32 @snprintf(i8* %12, i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %4)
%15 = mul nsw i32 %4, 10
%16 = select i1 %6, i32 %5, i32 %3
%17 = icmp eq i32 %16, %13
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i64 @strncmp(i8* %10, i8* %12, i32 %8)
%21 = icmp eq i64 %20, 0
%22 = zext i1 %21 to i32
%23 = tail call i32 @assert(i32 %22)
%24 = tail call i32 @free(i8* %10)
%25 = tail call i32 @free(i8* %12)
%26 = icmp eq i32 %5, 10
br i1 %26, label %27, label %2
27: ; preds = %2
ret void
}