#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, k;
  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    $0x40,%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,-0x28(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
lea    0xe6c(%rip),%rcx
call   1050 <print_decode_line@plt>
mov    -0x28(%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    -0x28(%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    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%eax
shl    $0x2,%eax
add    $0x8,%eax
cltq
add    %rax,%rdi
call   1060 <decodeInt32@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x36(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xe33(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x36(%rbp),%rsi
mov    -0x2c(%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    -0x28(%rbp),%rdi
call   1060 <decodeInt32@plt>
mov    %eax,-0x4(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1174 <decode_trunk_page+0x14>
add    $0x40,%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 i32
%12 = alloca i8*
%13 = alloca i32
%14 = 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 %15
15: ; preds = %79, %4
%16 = load i32, i32* %5
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %82
18: ; preds = %15
%19 = load i32, i32* %5
%20 = sub nsw i32 %19, 1
%21 = load i32, i32* %6
%22 = mul nsw i32 %20, %21
%23 = load i32, i32* %6
%24 = call i8* @getContent(i32 %22, i32 %23)
store i8* %24, i8** %12
%25 = load i32, i32* %5
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i8*, i8** %12
%28 = call i32 @print_decode_line(i8* %27, i32 0, i32 4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%29 = load i8*, i8** %12
%30 = call i32 @print_decode_line(i8* %29, i32 4, i32 4, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %71
33: ; preds = %18
%34 = load i8*, i8** %12
%35 = getelementptr inbounds i8, i8* %34, i64 4
%36 = call i32 @decodeInt32(i8* %35)
store i32 %36, i32* %9
store i32 0, i32* %10
br label %37
37: ; preds = %61, %33
%38 = load i32, i32* %10
%39 = load i32, i32* %9
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %64
41: ; preds = %37
%42 = load i8*, i8** %12
%43 = load i32, i32* %10
%44 = mul nsw i32 4, %43
%45 = add nsw i32 8, %44
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %42, i64 %46
%48 = call i32 @decodeInt32(i8* %47)
store i32 %48, i32* %13
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%50 = load i32, i32* %10
%51 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%53 = load i32, i32* %13
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %52, i32 %53)
%55 = load i32, i32* %10
%56 = srem i32 %55, 5
%57 = icmp eq i32 %56, 4
br i1 %57, label %58, label %60
58: ; preds = %41
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %60
60: ; preds = %58, %41
br label %61
61: ; preds = %60
%62 = load i32, i32* %10
%63 = add nsw i32 %62, 1
store i32 %63, i32* %10
br label %37
64: ; preds = %37
%65 = load i32, i32* %10
%66 = srem i32 %65, 5
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %70
70: ; preds = %68, %64
br label %71
71: ; preds = %70, %18
%72 = load i32, i32* %8
%73 = icmp ne i32 %72, 0
br i1 %73, label %75, label %74
74: ; preds = %71
store i32 0, i32* %5
br label %79
75: ; preds = %71
%76 = load i8*, i8** %12
%77 = getelementptr inbounds i8, i8* %76, i64 0
%78 = call i32 @decodeInt32(i8* %77)
store i32 %78, i32* %5
br label %79
79: ; preds = %75, %74
%80 = load i8*, i8** %12
%81 = call i32 @free(i8* %80)
br label %15
82: ; preds = %15
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
}