#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*/ ext2_close(int);
int /*<<< orphan*/ ext2_fini();
int /*<<< orphan*/ ext2_init();
int ext2_open(char *, int /*<<< orphan*/);
int ext2_read(int, char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  ext2_init();
  int i = ext2_open("/home/barracuda/help.txt", 0);
  if (i < 0)
    return 0;
  char *buf = malloc(1024);
  int j, k;
  for (j = 0; j < 512; j++) {
    if ((k = ext2_read(i, buf, 2)) == 0)
      break;
    buf[k] = 0;
    printf("%s", buf);
  }
  free(buf);
  ext2_close(i);
  ext2_fini();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <ext2_init@plt>
lea    0xe63(%rip),%rdi
xor    %esi,%esi
call   1070 <ext2_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11bd <main+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    1243 <main+0xc3>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x200,-0x14(%rbp)
jge    122b <main+0xab>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1090 <ext2_read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    1201 <main+0x81>
jmp    122b <main+0xab>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xe01(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d2 <main+0x52>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   1080 <ext2_close@plt>
mov    $0x0,%al
call   1040 <ext2_fini@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <ext2_init@plt>
lea    0xe6b(%rip),%rdi
xor    %esi,%esi
call   1070 <ext2_open@plt>
test   %eax,%eax
js     1201 <main+0x81>
mov    %eax,%r15d
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    $0x200,%ebp
lea    0xe5d(%rip),%r14
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1090 <ext2_read@plt>
test   %eax,%eax
je     11ea <main+0x6a>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
add    $0xffffffff,%ebp
jne    11c0 <main+0x40>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1080 <ext2_close@plt>
xor    %eax,%eax
call   1040 <ext2_fini@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"/home/barracuda/help.txt\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (...) @ext2_init()
%7 = call i32 @ext2_open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 0, i32* %1
br label %38
11: ; preds = %0
%12 = call i8* @malloc(i32 1024)
store i8* %12, i8** %3
store i32 0, i32* %4
br label %13
13: ; preds = %29, %11
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 512
br i1 %15, label %16, label %32
16: ; preds = %13
%17 = load i32, i32* %2
%18 = load i8*, i8** %3
%19 = call i32 @ext2_read(i32 %17, i8* %18, i32 2)
store i32 %19, i32* %5
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %32
22: ; preds = %16
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %3
%28 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %22
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %13
32: ; preds = %21, %13
%33 = load i8*, i8** %3
%34 = call i32 @free(i8* %33)
%35 = load i32, i32* %2
%36 = call i32 @ext2_close(i32 %35)
%37 = call i32 (...) @ext2_fini()
br label %38
38: ; preds = %32, %10
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [25 x i8] c"/home/barracuda/help.txt\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = tail call i32 (...) @ext2_init()
%2 = tail call i32 @ext2_open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %20, label %4
4: ; preds = %0
%5 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
br label %6
6: ; preds = %4, %10
%7 = phi i32 [ 0, %4 ], [ %14, %10 ]
%8 = tail call i32 @ext2_read(i32 %2, i8* %5, i32 2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %6
%11 = sext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %5, i64 %11
store i8 0, i8* %12
%13 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%14 = add nuw nsw i32 %7, 1
%15 = icmp eq i32 %14, 512
br i1 %15, label %16, label %6
16: ; preds = %6, %10
%17 = tail call i32 @free(i8* %5)
%18 = tail call i32 @ext2_close(i32 %2)
%19 = tail call i32 (...) @ext2_fini()
br label %20
20: ; preds = %0, %16
ret i32 0
}