#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*/ fprintf(int /*<<< orphan*/, char *);
int get_block(int, int);
int get_nr_blocks(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
void print_blocks(int fd) {
  int nr_blocks, i;
  nr_blocks = get_nr_blocks(fd);
  if (nr_blocks < 0) {
    fprintf(stderr, "get_nr_blocks failed!\n");
    return;
  }
  if (nr_blocks == 0) {
    printf("no allocated blocks\n");
    return;
  } else if (nr_blocks == 1) {
    printf("1 block\n\n");
  } else
    printf("%d blocks\n\n", nr_blocks);
  for (i = 0; i < nr_blocks; i++) {
    int phys_block;
    phys_block = get_block(fd, i);
    if (phys_block < 0) {
      fprintf(stderr, "get_block failed!\n");
      return;
    }
    if (!phys_block) {
      continue;
    }
    printf("(%u, %u) ", i, phys_block);
  }
  putchar('\n');
}


x86 assembly (clang, -O0):
<print_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <get_nr_blocks@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118a <print_blocks+0x3a>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1050 <fprintf@plt>
jmp    125a <print_blocks+0x10a>
cmpl   $0x0,-0x8(%rbp)
jne    11a7 <print_blocks+0x57>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125a <print_blocks+0x10a>
cmpl   $0x1,-0x8(%rbp)
jne    11c4 <print_blocks+0x74>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d5 <print_blocks+0x85>
mov    -0x8(%rbp),%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11da <print_blocks+0x8a>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1250 <print_blocks+0x100>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <get_block@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    121f <print_blocks+0xcf>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
call   1050 <fprintf@plt>
jmp    125a <print_blocks+0x10a>
cmpl   $0x0,-0x10(%rbp)
jne    122e <print_blocks+0xde>
jmp    1242 <print_blocks+0xf2>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11e1 <print_blocks+0x91>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_blocks>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
call   1080 <get_nr_blocks@plt>
test   %eax,%eax
js     1194 <print_blocks+0x34>
mov    %eax,%ebp
cmp    $0x1,%eax
je     11a6 <print_blocks+0x46>
test   %ebp,%ebp
jne    11b4 <print_blocks+0x54>
lea    0xec4(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
jmp    121b <print_blocks+0xbb>
lea    0xe93(%rip),%rdi
call   1040 <puts@plt>
jmp    11c4 <print_blocks+0x64>
lea    0xe5c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe6b(%rip),%r14
xor    %ebx,%ebx
jmp    11d7 <print_blocks+0x77>
nop
add    $0x1,%ebx
cmp    %ebx,%ebp
je     11f7 <print_blocks+0x97>
mov    %r15d,%edi
mov    %ebx,%esi
call   1070 <get_block@plt>
test   %eax,%eax
js     120b <print_blocks+0xab>
je     11d0 <print_blocks+0x70>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d0 <print_blocks+0x70>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"get_nr_blocks failed!\0A\00"
@.str.1 = [21 x i8] c"no allocated blocks\0A\00"
@.str.2 = [10 x i8] c"1 block\0A\0A\00"
@.str.3 = [12 x i8] c"%d blocks\0A\0A\00"
@.str.4 = [19 x i8] c"get_block failed!\0A\00"
@.str.5 = [10 x i8] c"(%u, %u) \00"
define void @print_blocks(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @get_nr_blocks(i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %54
13: ; preds = %1
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %54
18: ; preds = %13
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %26
23: ; preds = %18
%24 = load i32, i32* %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %21
br label %27
27: ; preds = %26
store i32 0, i32* %4
br label %28
28: ; preds = %49, %27
%29 = load i32, i32* %4
%30 = load i32, i32* %3
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %52
32: ; preds = %28
%33 = load i32, i32* %2
%34 = load i32, i32* %4
%35 = call i32 @get_block(i32 %33, i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %54
41: ; preds = %32
%42 = load i32, i32* %5
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %41
br label %49
45: ; preds = %41
%46 = load i32, i32* %4
%47 = load i32, i32* %5
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %46, i32 %47)
br label %49
49: ; preds = %45, %44
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %4
br label %28
52: ; preds = %28
%53 = call i32 @putchar(i8 signext 10)
br label %54
54: ; preds = %52, %38, %16, %10
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"get_nr_blocks failed!\0A\00"
@.str.3 = [12 x i8] c"%d blocks\0A\0A\00"
@.str.4 = [19 x i8] c"get_block failed!\0A\00"
@.str.5 = [10 x i8] c"(%u, %u) \00"
@str = [9 x i8] c"1 block\0A\00"
@str.6 = [20 x i8] c"no allocated blocks\00"
define void @print_blocks(i32 %0) {
%2 = tail call i32 @get_nr_blocks(i32 %0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %31
7: ; preds = %1
switch i32 %2, label %12 [
i32 0, label %8
i32 1, label %10
]
8: ; preds = %7
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
br label %31
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %14
12: ; preds = %7
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %2)
br label %14
14: ; preds = %12, %10
br label %15
15: ; preds = %14, %26
%16 = phi i32 [ %27, %26 ], [ 0, %14 ]
%17 = tail call i32 @get_block(i32 %0, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = icmp eq i32 %17, 0
br i1 %20, label %26, label %21
21: ; preds = %19
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %16, i32 %17)
br label %26
23: ; preds = %15
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %31
26: ; preds = %21, %19
%27 = add nuw nsw i32 %16, 1
%28 = icmp eq i32 %27, %2
br i1 %28, label %29, label %15
29: ; preds = %26
%30 = tail call i32 @putchar(i8 signext 10)
br label %31
31: ; preds = %23, %29, %8, %4
ret void
}