#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 */
unsigned int decodeInt32(unsigned char *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *getContent(int, int);
int /*<<< orphan*/ print_decode_line(unsigned char *, int, int, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
void decode_trunk_page(int pgno, int pagesize, int detail, int recursive) {
  int n, i;
  unsigned char *a;
  while (pgno > 0) {
    a = getContent((pgno - 1) * pagesize, pagesize);
    printf("Decode of freelist trunk page %d:\n", pgno);
    print_decode_line(a, 0, 4, "Next freelist trunk page");
    print_decode_line(a, 4, 4, "Number of entries on this page");
    if (detail) {
      n = (int)decodeInt32(&a[4]);
      for (i = 0; i < n; i++) {
        unsigned int x = decodeInt32(&a[8 + 4 * i]);
        char zIdx[10];
        sprintf(zIdx, "[%d]", i);
        printf("  %5s %7u", zIdx, x);
        if (i % 5 == 4)
          printf("\n");
      }
      if (i % 5 != 0)
        printf("\n");
    }
    if (!recursive) {
      pgno = 0;
    } else {
      pgno = (int)decodeInt32(&a[0]);
    }
    free(a);
  }
}


x86 assembly (clang, -O0):
<decode_trunk_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    12ce <decode_trunk_page+0x16e>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
imul   -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1070 <getContent@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
lea    0xe6c(%rip),%rcx
call   1050 <print_decode_line@plt>
mov    -0x20(%rbp),%rdi
mov    $0x4,%edx
lea    0xe70(%rip),%rcx
mov    %edx,%esi
call   1050 <print_decode_line@plt>
cmpl   $0x0,-0xc(%rbp)
je     129e <decode_trunk_page+0x13e>
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
call   1060 <decodeInt32@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1277 <decode_trunk_page+0x117>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%eax
shl    $0x2,%eax
add    $0x8,%eax
cltq
add    %rax,%rdi
call   1060 <decodeInt32@plt>
mov    %eax,-0x24(%rbp)
lea    -0x2e(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xe33(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x2e(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
cmp    $0x4,%edx
jne    1264 <decode_trunk_page+0x104>
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1269 <decode_trunk_page+0x109>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11f4 <decode_trunk_page+0x94>
mov    -0x18(%rbp),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1299 <decode_trunk_page+0x139>
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129e <decode_trunk_page+0x13e>
cmpl   $0x0,-0x10(%rbp)
jne    12b4 <decode_trunk_page+0x154>
movl   $0x0,-0x4(%rbp)
jmp    12c0 <decode_trunk_page+0x160>
mov    -0x20(%rbp),%rdi
call   1060 <decodeInt32@plt>
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1174 <decode_trunk_page+0x14>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_trunk_page>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
test   %edi,%edi
jle    1303 <decode_trunk_page+0x193>
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebp
mov    %edi,%ebx
lea    0xe69(%rip),%r12
lea    0xe85(%rip),%r13
mov    %ecx,0x10(%rsp)
mov    %edx,0xc(%rsp)
mov    %esi,0x8(%rsp)
nopw   0x0(%rax,%rax,1)
lea    -0x1(%rbx),%edi
imul   %ebp,%edi
mov    %ebp,%esi
call   1080 <getContent@plt>
mov    %r12,%rdi
mov    %ebx,%esi
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
mov    %r13,%rcx
call   1060 <print_decode_line@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x4,%edx
lea    0xe4a(%rip),%rcx
call   1060 <print_decode_line@plt>
test   %r15d,%r15d
je     12da <decode_trunk_page+0x16a>
lea    0x4(%rbx),%rdi
call   1070 <decodeInt32@plt>
test   %eax,%eax
jle    12da <decode_trunk_page+0x16a>
mov    %eax,0x14(%rsp)
mov    %eax,%eax
mov    %rax,0x20(%rsp)
mov    %rbx,0x18(%rsp)
add    $0x8,%rbx
mov    $0x4,%r14d
xor    %ebp,%ebp
jmp    1243 <decode_trunk_page+0xd3>
nop
add    $0x1,%rbp
add    $0x4,%rbx
add    $0xffffffff,%r14d
cmp    %rbp,0x20(%rsp)
je     12a0 <decode_trunk_page+0x130>
mov    %ebp,%eax
mov    $0xcccccccd,%ecx
imul   %rcx,%rax
shr    $0x22,%rax
lea    (%rax,%rax,4),%r12d
mov    %rbx,%rdi
call   1070 <decodeInt32@plt>
mov    %eax,%r15d
lea    0x2e(%rsp),%r13
mov    %r13,%rdi
lea    0xdeb(%rip),%rsi
mov    %ebp,%edx
call   1090 <sprintf@plt>
lea    0xde2(%rip),%rdi
mov    %r13,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    %r14d,%r12d
jne    1230 <decode_trunk_page+0xc0>
mov    $0xa,%edi
call   1040 <putchar@plt>
jmp    1230 <decode_trunk_page+0xc0>
nopl   0x0(%rax)
imul   $0xcccccccd,0x14(%rsp),%eax
cc
cmp    $0x33333334,%eax
mov    0x10(%rsp),%r14d
mov    0xc(%rsp),%r15d
mov    0x8(%rsp),%ebp
lea    0xd3e(%rip),%r12
lea    0xd5a(%rip),%r13
mov    0x18(%rsp),%rbx
jb     12da <decode_trunk_page+0x16a>
mov    $0xa,%edi
call   1040 <putchar@plt>
test   %r14d,%r14d
je     12fb <decode_trunk_page+0x18b>
mov    %rbx,%rdi
call   1070 <decodeInt32@plt>
mov    %rbx,%rdi
mov    %eax,%ebx
call   1030 <free@plt>
test   %ebx,%ebx
jg     11b0 <decode_trunk_page+0x40>
jmp    1303 <decode_trunk_page+0x193>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Decode of freelist trunk page %d:\0A\00"
@.str.1 = [25 x i8] c"Next freelist trunk page\00"
@.str.2 = [31 x i8] c"Number of entries on this page\00"
@.str.3 = [5 x i8] c"[%d]\00"
@.str.4 = [10 x i8] c" %5s %7u\00"
@.str.5 = [2 x i8] c"\0A\00"
define void @decode_trunk_page(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca [10 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
br label %14
14: ; preds = %78, %4
%15 = load i32, i32* %5
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %81
17: ; preds = %14
%18 = load i32, i32* %5
%19 = sub nsw i32 %18, 1
%20 = load i32, i32* %6
%21 = mul nsw i32 %19, %20
%22 = load i32, i32* %6
%23 = call i8* @getContent(i32 %21, i32 %22)
store i8* %23, i8** %11
%24 = load i32, i32* %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = load i8*, i8** %11
%27 = call i32 @print_decode_line(i8* %26, i32 0, i32 4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%28 = load i8*, i8** %11
%29 = call i32 @print_decode_line(i8* %28, i32 4, i32 4, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %70
32: ; preds = %17
%33 = load i8*, i8** %11
%34 = getelementptr inbounds i8, i8* %33, i64 4
%35 = call i32 @decodeInt32(i8* %34)
store i32 %35, i32* %9
store i32 0, i32* %10
br label %36
36: ; preds = %60, %32
%37 = load i32, i32* %10
%38 = load i32, i32* %9
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %63
40: ; preds = %36
%41 = load i8*, i8** %11
%42 = load i32, i32* %10
%43 = mul nsw i32 4, %42
%44 = add nsw i32 8, %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
%47 = call i32 @decodeInt32(i8* %46)
store i32 %47, i32* %12
%48 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 0
%49 = load i32, i32* %10
%50 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 0
%52 = load i32, i32* %12
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %51, i32 %52)
%54 = load i32, i32* %10
%55 = srem i32 %54, 5
%56 = icmp eq i32 %55, 4
br i1 %56, label %57, label %59
57: ; preds = %40
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %57, %40
br label %60
60: ; preds = %59
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
br label %36
63: ; preds = %36
%64 = load i32, i32* %10
%65 = srem i32 %64, 5
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %63
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67, %63
br label %70
70: ; preds = %69, %17
%71 = load i32, i32* %8
%72 = icmp ne i32 %71, 0
br i1 %72, label %74, label %73
73: ; preds = %70
store i32 0, i32* %5
br label %78
74: ; preds = %70
%75 = load i8*, i8** %11
%76 = getelementptr inbounds i8, i8* %75, i64 0
%77 = call i32 @decodeInt32(i8* %76)
store i32 %77, i32* %5
br label %78
78: ; preds = %74, %73
%79 = load i8*, i8** %11
%80 = call i32 @free(i8* %79)
br label %14
81: ; preds = %14
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"Decode of freelist trunk page %d:\0A\00"
@.str.1 = [25 x i8] c"Next freelist trunk page\00"
@.str.2 = [31 x i8] c"Number of entries on this page\00"
@.str.3 = [5 x i8] c"[%d]\00"
@.str.4 = [10 x i8] c" %5s %7u\00"
define void @decode_trunk_page(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [10 x i8]
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %53
7: ; preds = %4
%8 = icmp eq i32 %2, 0
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%10 = icmp eq i32 %3, 0
br label %11
11: ; preds = %7, %49
%12 = phi i32 [ %0, %7 ], [ %50, %49 ]
%13 = add nsw i32 %12, -1
%14 = mul nsw i32 %13, %1
%15 = call i8* @getContent(i32 %14, i32 %1)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %12)
%17 = call i32 @print_decode_line(i8* %15, i32 0, i32 4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @print_decode_line(i8* %15, i32 4, i32 4, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br i1 %8, label %46, label %19
19: ; preds = %11
%20 = getelementptr inbounds i8, i8* %15, i64 4
%21 = call i32 @decodeInt32(i8* %20)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %46
23: ; preds = %19
%24 = zext i32 %21 to i64
br label %25
25: ; preds = %23, %38
%26 = phi i64 [ 0, %23 ], [ %39, %38 ]
%27 = shl nsw i64 %26, 2
%28 = add nuw nsw i64 %27, 8
%29 = getelementptr inbounds i8, i8* %15, i64 %28
%30 = call i32 @decodeInt32(i8* %29)
%31 = trunc i64 %26 to i32
%32 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %9, i32 %30)
%34 = urem i32 %31, 5
%35 = icmp eq i32 %34, 4
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @putchar(i32 10)
br label %38
38: ; preds = %36, %25
%39 = add nuw nsw i64 %26, 1
%40 = icmp eq i64 %39, %24
br i1 %40, label %41, label %25
41: ; preds = %38
%42 = urem i32 %21, 5
%43 = icmp eq i32 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = call i32 @putchar(i32 10)
br label %46
46: ; preds = %19, %41, %44, %11
br i1 %10, label %47, label %49
47: ; preds = %46
%48 = call i32 @free(i8* %15)
br label %53
49: ; preds = %46
%50 = call i32 @decodeInt32(i8* %15)
%51 = call i32 @free(i8* %15)
%52 = icmp sgt i32 %50, 0
br i1 %52, label %11, label %53
53: ; preds = %49, %47, %4
ret void
}