#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 atoi(char *);
char getchar();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int myfs_file_read(int, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ strlen(char *);
void read_file(char *fdStr) {
  int fd;
  if (!strlen(fdStr)) {
    printf("Input file descriptor to read:\n-> ");
    scanf("%d", &fd);
  } else {
    fd = atoi(fdStr);
  }
  int data_size;
  printf("Input read size:\n-> ");
  scanf("%d", &data_size);
  while (getchar() != '\n')
    ;
  char buf[data_size + 1];
  memset(buf, 0, data_size + 1);
  switch (myfs_file_read(fd, buf, data_size)) {
  case -1:
    printf("No mounted filesystem!\n");
    break;
  case -2:
    printf("There are no file in the disk!\n");
    break;
  case -3:
    printf("Block read error!\n");
    break;
  case -4:
    printf("File descriptor not found!\n");
    break;
  case -5:
    printf("Read size too big!\n");
    break;
  default:
    printf("== content ==\n");
    for (int i = 0; i < data_size; ++i) {
      printf("%c", buf[i]);
    }
    printf("\n== Successful read! ==\n");
  }
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    11b1 <read_file+0x41>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe94(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1080 <scanf@plt>
jmp    11bd <read_file+0x4d>
mov    -0x8(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0xc(%rbp)
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe65(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <scanf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     11f3 <read_file+0x83>
jmp    11db <read_file+0x6b>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x38(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%edx
call   1070 <myfs_file_read@plt>
add    $0x5,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x4,%eax
ja     12bd <read_file+0x14d>
mov    -0x30(%rbp),%rax
lea    0xdab(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1314 <read_file+0x1a4>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1314 <read_file+0x1a4>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1314 <read_file+0x1a4>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1314 <read_file+0x1a4>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1314 <read_file+0x1a4>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1306 <read_file+0x196>
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12d2 <read_file+0x162>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1050 <strlen@plt>
test   %eax,%eax
je     11b8 <read_file+0x28>
mov    %rbx,%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x28(%rbp)
jmp    11d6 <read_file+0x46>
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe56(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <scanf@plt>
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe38(%rip),%rdi
lea    -0x24(%rbp),%rsi
call   10a0 <scanf@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1080 <getchar@plt>
cmp    $0xa,%al
jne    1200 <read_file+0x70>
mov    %rsp,%r15
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    -0x24(%rbp),%edx
add    $0x1,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %r12,%rsi
call   1090 <myfs_file_read@plt>
cmp    $0xfffffffa,%eax
jbe    125d <read_file+0xcd>
add    $0x5,%eax
cltq
lea    0xe84(%rip),%rcx
movslq (%rcx,%rax,4),%r14
add    %rcx,%r14
jmp    1297 <read_file+0x107>
lea    0xe4d(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4f(%rip),%r14
cmpl   $0x0,-0x24(%rbp)
jle    1297 <read_file+0x107>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r12,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
movslq -0x24(%rbp),%rax
cmp    %rax,%rbx
jl     1280 <read_file+0xf0>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %r15,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Input file descriptor to read:\0A-> \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [21 x i8] c"Input read size:\0A-> \00"
@.str.3 = [24 x i8] c"No mounted filesystem!\0A\00"
@.str.4 = [32 x i8] c"There are no file in the disk!\0A\00"
@.str.5 = [19 x i8] c"Block read error!\0A\00"
@.str.6 = [28 x i8] c"File descriptor not found!\0A\00"
@.str.7 = [20 x i8] c"Read size too big!\0A\00"
@.str.8 = [15 x i8] c"== content ==\0A\00"
@.str.9 = [3 x i8] c"%c\00"
@.str.10 = [25 x i8] c"\0A== Successful read! ==\0A\00"
define void @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
br label %17
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %3
br label %17
17: ; preds = %14, %11
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
br label %20
20: ; preds = %24, %17
%21 = call signext i8 (...) @getchar()
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %25
24: ; preds = %20
br label %20
25: ; preds = %20
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
%28 = zext i32 %27 to i64
%29 = call i8* @llvm.stacksave()
store i8* %29, i8** %5
%30 = alloca i8, i64 %28
store i64 %28, i64* %6
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
%33 = call i32 @memset(i8* %30, i32 0, i32 %32)
%34 = load i32, i32* %3
%35 = load i32, i32* %4
%36 = call i32 @myfs_file_read(i32 %34, i8* %30, i32 %35)
switch i32 %36, label %47 [
i32 -1, label %37
i32 -2, label %39
i32 -3, label %41
i32 -4, label %43
i32 -5, label %45
]
37: ; preds = %25
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %65
39: ; preds = %25
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %65
41: ; preds = %25
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %65
43: ; preds = %25
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %65
45: ; preds = %25
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
br label %65
47: ; preds = %25
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %7
br label %49
49: ; preds = %60, %47
%50 = load i32, i32* %7
%51 = load i32, i32* %4
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %63
53: ; preds = %49
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %30, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %53
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
store i32 %62, i32* %7
br label %49
63: ; preds = %49
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0))
br label %65
65: ; preds = %63, %45, %43, %41, %39, %37
%66 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"Input file descriptor to read:\0A-> \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [21 x i8] c"Input read size:\0A-> \00"
@str = [19 x i8] c"Read size too big!\00"
@str.11 = [27 x i8] c"File descriptor not found!\00"
@str.12 = [18 x i8] c"Block read error!\00"
@str.13 = [31 x i8] c"There are no file in the disk!\00"
@str.14 = [23 x i8] c"No mounted filesystem!\00"
@str.15 = [14 x i8] c"== content ==\00"
@str.16 = [24 x i8] c"\0A== Successful read! ==\00"
@reltable.read_file = [5 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([19 x i8]* @str to i64), i64 ptrtoint ([5 x i32]* @reltable.read_file to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([27 x i8]* @str.11 to i64), i64 ptrtoint ([5 x i32]* @reltable.read_file to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([18 x i8]* @str.12 to i64), i64 ptrtoint ([5 x i32]* @reltable.read_file to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([31 x i8]* @str.13 to i64), i64 ptrtoint ([5 x i32]* @reltable.read_file to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([23 x i8]* @str.14 to i64), i64 ptrtoint ([5 x i32]* @reltable.read_file to i64)) to i32)]
define void @read_file(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
br label %12
10: ; preds = %1
%11 = tail call i32 @atoi(i8* %0)
store i32 %11, i32* %2
br label %12
12: ; preds = %10, %7
%13 = bitcast i32* %3 to i8*
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
br label %16
16: ; preds = %16, %12
%17 = call signext i8 (...) @getchar()
%18 = icmp eq i8 %17, 10
br i1 %18, label %19, label %16
19: ; preds = %16
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
%24 = alloca i8, i64 %22
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
%27 = call i32 @memset(i8* %24, i32 0, i32 %26)
%28 = load i32, i32* %2
%29 = load i32, i32* %3
%30 = call i32 @myfs_file_read(i32 %28, i8* %24, i32 %29)
%31 = icmp ugt i32 %30, -6
br i1 %31, label %46, label %32
32: ; preds = %19
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.15, i64 0, i64 0))
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %32, %36
%37 = phi i64 [ %42, %36 ], [ 0, %32 ]
%38 = getelementptr inbounds i8, i8* %24, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = call i32 @putchar(i32 %40)
%42 = add nuw nsw i64 %37, 1
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = icmp slt i64 %42, %44
br i1 %45, label %36, label %51
46: ; preds = %19
%47 = add i32 %30, 5
%48 = sext i32 %47 to i64
%49 = shl i64 %48, 2
%50 = call i8* @llvm.load.relative.i64(i8* bitcast ([5 x i32]* @reltable.read_file to i8*), i64 %49)
br label %51
51: ; preds = %36, %46, %32
%52 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @str.16, i64 0, i64 0), %32 ], [ %50, %46 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @str.16, i64 0, i64 0), %36 ]
%53 = call i32 @puts(i8* dereferenceable(1) %52)
ret void
}