#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int myfs_file_close(int);
int myfs_file_create(char *);
int myfs_file_open(char *);
int myfs_file_write(int, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ trimFilename(char *);
void push_file(char *filename) {
  if (!strlen(filename)) {
    printf("Input filename to push:\n-> ");
    fgets(filename, 256, stdin);
    trimFilename(filename);
  }
  int error_code;
  if ((error_code = myfs_file_create(filename)) != 0) {
    printf("Unable to create file! [%d]\n", error_code);
    return;
  }
  int fd;
  if ((fd = myfs_file_open(filename)) < 0) {
    printf("Unable to open file! [%d]\n", fd);
    return;
  }
  FILE *fin;
  if (!(fin = fopen(filename, "rb"))) {
    printf("Unable to open source file!\n");
    return;
  }
  char buf[1023];
  int data_size;
  while ((data_size = fread(buf, 1, 1023, fin)) > 0) {
    if ((error_code = myfs_file_write(fd, buf, data_size)) < 0) {
      printf("Write error! [%d]\n", error_code);
      fclose(fin);
      if ((error_code = myfs_file_close(fd)) < 0) {
        printf("File close error! [%d]\n", error_code);
      }
      return;
    }
    fflush(fin);
  }
  fclose(fin);
  if ((error_code = myfs_file_close(fd)) < 0) {
    printf("File close error! [%d]\n", error_code);
  }
  printf("%s Pushed!\n", filename);
}


x86 assembly (clang, -O0):
<push_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
jne    120f <push_file+0x4f>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    -0x8(%rbp),%rdi
call   10c0 <trimFilename@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <myfs_file_create@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     123a <push_file+0x7a>
mov    -0xc(%rbp),%esi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1377 <push_file+0x1b7>
mov    -0x8(%rbp),%rdi
call   10a0 <myfs_file_open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1265 <push_file+0xa5>
mov    -0x10(%rbp),%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1377 <push_file+0x1b7>
mov    -0x8(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1296 <push_file+0xd6>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1377 <push_file+0x1b7>
jmp    129b <push_file+0xdb>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3ff,%edx
call   1030 <fread@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jle    1337 <push_file+0x177>
mov    -0x10(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1070 <myfs_file_write@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1329 <push_file+0x169>
mov    -0xc(%rbp),%esi
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%edi
call   10e0 <myfs_file_close@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1324 <push_file+0x164>
mov    -0xc(%rbp),%esi
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1377 <push_file+0x1b7>
mov    -0x18(%rbp),%rdi
call   1090 <fflush@plt>
jmp    129b <push_file+0xdb>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%edi
call   10e0 <myfs_file_close@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1365 <push_file+0x1a5>
mov    -0xc(%rbp),%esi
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<push_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
call   1060 <strlen@plt>
test   %eax,%eax
jne    1215 <push_file+0x45>
lea    0xe10(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   10d0 <trimFilename@plt>
mov    %rbx,%rdi
call   10c0 <myfs_file_create@plt>
test   %eax,%eax
je     123e <push_file+0x6e>
lea    0xdf4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <printf@plt>
mov    %rbx,%rdi
call   10b0 <myfs_file_open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     12fd <push_file+0x12d>
lea    0xdfc(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     130c <push_file+0x13c>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x3ff,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    12c6 <push_file+0xf6>
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1080 <myfs_file_write@plt>
test   %eax,%eax
js     1325 <push_file+0x155>
mov    %rbp,%rdi
call   10a0 <fflush@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x3ff,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jg     1290 <push_file+0xc0>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %r14d,%edi
call   10f0 <myfs_file_close@plt>
test   %eax,%eax
jns    12ea <push_file+0x11a>
lea    0xd89(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd91(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1359 <push_file+0x189>
lea    0xd35(%rip),%rdi
mov    %r14d,%esi
jmp    122a <push_file+0x5a>
lea    0xd7b(%rip),%rdi
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
lea    0xd2b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %r14d,%edi
call   10f0 <myfs_file_close@plt>
test   %eax,%eax
jns    1359 <push_file+0x189>
lea    0xd1a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Input filename to push:\0A-> \00"
@stdin = global i32 0
@.str.1 = [29 x i8] c"Unable to create file! [%d]\0A\00"
@.str.2 = [27 x i8] c"Unable to open file! [%d]\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [29 x i8] c"Unable to open source file!\0A\00"
@.str.5 = [19 x i8] c"Write error! [%d]\0A\00"
@.str.6 = [24 x i8] c"File close error! [%d]\0A\00"
@.str.7 = [12 x i8] c"%s Pushed!\0A\00"
define void @push_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [1023 x i8]
%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 %18, label %11
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %2
%14 = load i32, i32* @stdin
%15 = call i32 @fgets(i8* %13, i32 256, i32 %14)
%16 = load i8*, i8** %2
%17 = call i32 @trimFilename(i8* %16)
br label %18
18: ; preds = %11, %1
%19 = load i8*, i8** %2
%20 = call i32 @myfs_file_create(i8* %19)
store i32 %20, i32* %3
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %3
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %77
25: ; preds = %18
%26 = load i8*, i8** %2
%27 = call i32 @myfs_file_open(i8* %26)
store i32 %27, i32* %4
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %4
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %77
32: ; preds = %25
%33 = load i8*, i8** %2
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %34, i32** %5
%35 = icmp ne i32* %34, null
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %77
38: ; preds = %32
br label %39
39: ; preds = %62, %38
%40 = getelementptr inbounds [1023 x i8], [1023 x i8]* %6, i64 0, i64 0
%41 = load i32*, i32** %5
%42 = call i32 @fread(i8* %40, i32 1, i32 1023, i32* %41)
store i32 %42, i32* %7
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %65
44: ; preds = %39
%45 = load i32, i32* %4
%46 = getelementptr inbounds [1023 x i8], [1023 x i8]* %6, i64 0, i64 0
%47 = load i32, i32* %7
%48 = call i32 @myfs_file_write(i32 %45, i8* %46, i32 %47)
store i32 %48, i32* %3
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %62
50: ; preds = %44
%51 = load i32, i32* %3
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %51)
%53 = load i32*, i32** %5
%54 = call i32 @fclose(i32* %53)
%55 = load i32, i32* %4
%56 = call i32 @myfs_file_close(i32 %55)
store i32 %56, i32* %3
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %50
%59 = load i32, i32* %3
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %58, %50
br label %77
62: ; preds = %44
%63 = load i32*, i32** %5
%64 = call i32 @fflush(i32* %63)
br label %39
65: ; preds = %39
%66 = load i32*, i32** %5
%67 = call i32 @fclose(i32* %66)
%68 = load i32, i32* %4
%69 = call i32 @myfs_file_close(i32 %68)
store i32 %69, i32* %3
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %65
%72 = load i32, i32* %3
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %72)
br label %74
74: ; preds = %71, %65
%75 = load i8*, i8** %2
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i8* %75)
br label %77
77: ; preds = %74, %61, %36, %29, %22
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"Input filename to push:\0A-> \00"
@stdin = global i32 0
@.str.1 = [29 x i8] c"Unable to create file! [%d]\0A\00"
@.str.2 = [27 x i8] c"Unable to open file! [%d]\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.5 = [19 x i8] c"Write error! [%d]\0A\00"
@.str.6 = [24 x i8] c"File close error! [%d]\0A\00"
@.str.7 = [12 x i8] c"%s Pushed!\0A\00"
@str = [28 x i8] c"Unable to open source file!\00"
define void @push_file(i8* %0) {
%2 = alloca [1023 x i8]
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @stdin
%8 = tail call i32 @fgets(i8* %0, i32 256, i32 %7)
%9 = tail call i32 @trimFilename(i8* %0)
br label %10
10: ; preds = %5, %1
%11 = tail call i32 @myfs_file_create(i8* %0)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %53
15: ; preds = %10
%16 = tail call i32 @myfs_file_open(i8* %0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %53
20: ; preds = %15
%21 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %53
25: ; preds = %20
%26 = getelementptr inbounds [1023 x i8], [1023 x i8]* %2, i64 0, i64 0
%27 = call i32 @fread(i8* %26, i32 1, i32 1023, i32* %21)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %44
29: ; preds = %25, %40
%30 = phi i32 [ %42, %40 ], [ %27, %25 ]
%31 = call i32 @myfs_file_write(i32 %16, i8* %26, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%35 = call i32 @fclose(i32* %21)
%36 = call i32 @myfs_file_close(i32 %16)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %52
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %36)
br label %52
40: ; preds = %29
%41 = call i32 @fflush(i32* %21)
%42 = call i32 @fread(i8* %26, i32 1, i32 1023, i32* %21)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %29, label %44
44: ; preds = %40, %25
%45 = call i32 @fclose(i32* %21)
%46 = call i32 @myfs_file_close(i32 %16)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %46)
br label %50
50: ; preds = %48, %44
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i8* %0)
br label %52
52: ; preds = %33, %38, %50
br label %53
53: ; preds = %18, %52, %23, %13
ret void
}