#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*/ block_read(unsigned int, unsigned char *);
int /*<<< orphan*/ block_write(unsigned int, unsigned char *);
int /*<<< orphan*/ data_check(unsigned char *, unsigned char *);
int /*<<< orphan*/ data_fill(unsigned char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
void test_blocks(unsigned blocknum) {
  unsigned char data[1024];
  unsigned char rdata[1024];
  unsigned bn;
  int i;
  for (i = 0;; i++) {
    if (i == 0)
      bn = 0;
    else
      bn = 1 << (i - 1);
    if (bn > blocknum)
      bn = blocknum;
    printf("Writing block %u at address %06X.\n", bn, bn * 1024);
    data_fill(data, i, ~0);
    block_write(bn, data);
    if (bn == blocknum)
      break;
  }
  for (i = 0;; i++) {
    if (i == 0)
      bn = 0;
    else
      bn = 1 << (i - 1);
    if (bn > blocknum)
      bn = blocknum;
    printf("Reading block %u at address %06X.\n", bn, bn * 1024);
    data_fill(data, i, ~0);
    block_read(bn, rdata);
    data_check(data, rdata);
    if (bn == blocknum)
      break;
  }
  printf("Done.\n");
}


x86 assembly (clang, -O0):
<test_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
jne    1184 <test_blocks+0x34>
movl   $0x0,-0x814(%rbp)
jmp    119a <test_blocks+0x4a>
mov    -0x818(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x814(%rbp)
mov    -0x814(%rbp),%eax
cmp    -0x4(%rbp),%eax
jbe    11b2 <test_blocks+0x62>
mov    -0x4(%rbp),%eax
mov    %eax,-0x814(%rbp)
mov    -0x814(%rbp),%esi
mov    -0x814(%rbp),%edx
shl    $0xa,%edx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    -0x818(%rbp),%esi
mov    $0xffffffff,%edx
call   1030 <data_fill@plt>
mov    -0x814(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1060 <block_write@plt>
mov    -0x814(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    120c <test_blocks+0xbc>
jmp    1225 <test_blocks+0xd5>
jmp    1211 <test_blocks+0xc1>
mov    -0x818(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x818(%rbp)
jmp    1168 <test_blocks+0x18>
movl   $0x0,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
jne    124b <test_blocks+0xfb>
movl   $0x0,-0x814(%rbp)
jmp    1261 <test_blocks+0x111>
mov    -0x818(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x814(%rbp)
mov    -0x814(%rbp),%eax
cmp    -0x4(%rbp),%eax
jbe    1279 <test_blocks+0x129>
mov    -0x4(%rbp),%eax
mov    %eax,-0x814(%rbp)
mov    -0x814(%rbp),%esi
mov    -0x814(%rbp),%edx
shl    $0xa,%edx
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    -0x818(%rbp),%esi
mov    $0xffffffff,%edx
call   1030 <data_fill@plt>
mov    -0x814(%rbp),%edi
lea    -0x810(%rbp),%rsi
call   1070 <block_read@plt>
lea    -0x410(%rbp),%rdi
lea    -0x810(%rbp),%rsi
call   1040 <data_check@plt>
mov    -0x814(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    12e6 <test_blocks+0x196>
jmp    12ff <test_blocks+0x1af>
jmp    12eb <test_blocks+0x19b>
mov    -0x818(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x818(%rbp)
jmp    122f <test_blocks+0xdf>
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_blocks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %edi,%r13d
mov    %rsp,%r15
xor    %r12d,%r12d
xor    %ebx,%ebx
nopl   0x0(%rax)
lea    -0x1(%rbx),%ecx
mov    $0x1,%ebp
shl    %cl,%ebp
sub    $0x1,%r12d
mov    $0x0,%eax
cmovb  %eax,%ebp
cmp    %r13d,%ebp
mov    %ebp,%r14d
cmova  %r13d,%r14d
mov    %r14d,%edx
shl    $0xa,%edx
lea    0xe53(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    $0xffffffff,%edx
call   1030 <data_fill@plt>
mov    %r14d,%edi
mov    %r15,%rsi
call   1070 <block_write@plt>
add    $0x1,%ebx
cmp    %r13d,%ebp
jb     1180 <test_blocks+0x20>
xor    %r14d,%r14d
xor    %ebx,%ebx
xchg   %ax,%ax
lea    -0x1(%rbx),%ecx
mov    $0x1,%r15d
shl    %cl,%r15d
sub    $0x1,%r14d
mov    $0x0,%eax
cmovb  %eax,%r15d
cmp    %r13d,%r15d
mov    %r15d,%ebp
cmova  %r13d,%ebp
mov    %ebp,%edx
shl    $0xa,%edx
lea    0xe14(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%r12
mov    %r12,%rdi
mov    %ebx,%esi
mov    $0xffffffff,%edx
call   1030 <data_fill@plt>
mov    %ebp,%edi
lea    0x400(%rsp),%rbp
mov    %rbp,%rsi
call   1080 <block_read@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1050 <data_check@plt>
add    $0x1,%ebx
cmp    %r13d,%r15d
jb     11e0 <test_blocks+0x80>
lea    0xdf0(%rip),%rdi
call   1040 <puts@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Writing block %u at address %06X.\0A\00"
@.str.1 = [35 x i8] c"Reading block %u at address %06X.\0A\00"
@.str.2 = [7 x i8] c"Done.\0A\00"
define void @test_blocks(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %6
br label %7
7: ; preds = %37, %1
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %7
store i32 0, i32* %5
br label %15
11: ; preds = %7
%12 = load i32, i32* %6
%13 = sub nsw i32 %12, 1
%14 = shl i32 1, %13
store i32 %14, i32* %5
br label %15
15: ; preds = %11, %10
%16 = load i32, i32* %5
%17 = load i32, i32* %2
%18 = icmp ugt i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* %2
store i32 %20, i32* %5
br label %21
21: ; preds = %19, %15
%22 = load i32, i32* %5
%23 = load i32, i32* %5
%24 = mul i32 %23, 1024
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%27 = load i32, i32* %6
%28 = call i32 @data_fill(i8* %26, i32 %27, i32 -1)
%29 = load i32, i32* %5
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%31 = call i32 @block_write(i32 %29, i8* %30)
%32 = load i32, i32* %5
%33 = load i32, i32* %2
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %21
br label %40
36: ; preds = %21
br label %37
37: ; preds = %36
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
br label %7
40: ; preds = %35
store i32 0, i32* %6
br label %41
41: ; preds = %74, %40
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
store i32 0, i32* %5
br label %49
45: ; preds = %41
%46 = load i32, i32* %6
%47 = sub nsw i32 %46, 1
%48 = shl i32 1, %47
store i32 %48, i32* %5
br label %49
49: ; preds = %45, %44
%50 = load i32, i32* %5
%51 = load i32, i32* %2
%52 = icmp ugt i32 %50, %51
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = load i32, i32* %2
store i32 %54, i32* %5
br label %55
55: ; preds = %53, %49
%56 = load i32, i32* %5
%57 = load i32, i32* %5
%58 = mul i32 %57, 1024
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %56, i32 %58)
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%61 = load i32, i32* %6
%62 = call i32 @data_fill(i8* %60, i32 %61, i32 -1)
%63 = load i32, i32* %5
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%65 = call i32 @block_read(i32 %63, i8* %64)
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%68 = call i32 @data_check(i8* %66, i8* %67)
%69 = load i32, i32* %5
%70 = load i32, i32* %2
%71 = icmp eq i32 %69, %70
br i1 %71, label %72, label %73
72: ; preds = %55
br label %77
73: ; preds = %55
br label %74
74: ; preds = %73
%75 = load i32, i32* %6
%76 = add nsw i32 %75, 1
store i32 %76, i32* %6
br label %41
77: ; preds = %72
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"Writing block %u at address %06X.\0A\00"
@.str.1 = [35 x i8] c"Reading block %u at address %06X.\0A\00"
@str = [6 x i8] c"Done.\00"
define void @test_blocks(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %6
6: ; preds = %6, %1
%7 = phi i32 [ 0, %1 ], [ %19, %6 ]
%8 = icmp eq i32 %7, 0
%9 = add nsw i32 %7, -1
%10 = shl nuw i32 1, %9
%11 = select i1 %8, i32 0, i32 %10
%12 = icmp ugt i32 %11, %0
%13 = select i1 %12, i32 %0, i32 %11
%14 = shl i32 %13, 10
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
%16 = call i32 @data_fill(i8* %4, i32 %7, i32 -1)
%17 = call i32 @block_write(i32 %13, i8* %4)
%18 = icmp ult i32 %11, %0
%19 = add nuw nsw i32 %7, 1
br i1 %18, label %6, label %20
20: ; preds = %6, %20
%21 = phi i32 [ %34, %20 ], [ 0, %6 ]
%22 = icmp eq i32 %21, 0
%23 = add nsw i32 %21, -1
%24 = shl nuw i32 1, %23
%25 = select i1 %22, i32 0, i32 %24
%26 = icmp ugt i32 %25, %0
%27 = select i1 %26, i32 %0, i32 %25
%28 = shl i32 %27, 10
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %28)
%30 = call i32 @data_fill(i8* %4, i32 %21, i32 -1)
%31 = call i32 @block_read(i32 %27, i8* %5)
%32 = call i32 @data_check(i8* %4, i8* %5)
%33 = icmp ult i32 %25, %0
%34 = add nuw nsw i32 %21, 1
br i1 %33, label %20, label %35
35: ; preds = %20
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
ret void
}