#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 *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
char getchar();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int myfs_file_write(int, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
void write_file(char *fdStr) {
  int fd;
  if (!strlen(fdStr)) {
    printf("Input file descriptor to write:\n-> ");
    scanf("%d", &fd);
  } else {
    fd = atoi(fdStr);
  }
  int data_size;
  printf("Input data size:\n-> ");
  scanf("%d", &data_size);
  while (getchar() != '\n')
    ;
  char buf[data_size + 1];
  memset(buf, 0, data_size + 1);
  printf("Input data:\n-> ");
  fgets(buf, data_size + 1, stdin);
  switch (myfs_file_write(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 allocate error!\n");
    break;
  case -4:
    printf("File descriptor not found!\n");
    break;
  default:
    printf("Written!\n");
  }
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    11bf <write_file+0x3f>
lea    0xe6b(%rip),%rdi
call   1040 <printf@plt>
lea    0xe83(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1090 <scanf@plt>
jmp    11cb <write_file+0x4b>
mov    -0x8(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0xc(%rbp)
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
lea    0xe56(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1090 <scanf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     11ff <write_file+0x7f>
jmp    11e7 <write_file+0x67>
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,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe10(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%esi
add    $0x1,%esi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
call   1070 <fgets@plt>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%edx
call   1050 <myfs_file_write@plt>
add    $0x4,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x3,%eax
ja     12d2 <write_file+0x152>
mov    -0x28(%rbp),%rax
lea    0xd7b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xdc7(%rip),%rdi
call   1040 <printf@plt>
jmp    12de <write_file+0x15e>
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
jmp    12de <write_file+0x15e>
lea    0xddd(%rip),%rdi
call   1040 <printf@plt>
jmp    12de <write_file+0x15e>
lea    0xde3(%rip),%rdi
call   1040 <printf@plt>
jmp    12de <write_file+0x15e>
lea    0xdee(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %eax,%eax
je     11b4 <write_file+0x24>
mov    %rbx,%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    11d0 <write_file+0x40>
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
lea    0xe5d(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <scanf@plt>
lea    0xe50(%rip),%rdi
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   10a0 <scanf@plt>
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <getchar@plt>
cmp    $0xa,%al
jne    11f0 <write_file+0x60>
mov    %rsp,%r14
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    -0x14(%rbp),%edx
add    $0x1,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xe10(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1080 <fgets@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    %rbx,%rsi
call   1060 <myfs_file_write@plt>
cmp    $0xfffffffc,%eax
jb     1270 <write_file+0xe0>
add    $0x4,%eax
cltq
lea    0xe55(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    1277 <write_file+0xe7>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rsp
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [36 x i8] c"Input file descriptor to write:\0A-> \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [21 x i8] c"Input data size:\0A-> \00"
@.str.3 = [16 x i8] c"Input data:\0A-> \00"
@stdin = global i32 0
@str = [27 x i8] c"File descriptor not found!\00"
@str.9 = [22 x i8] c"Block allocate error!\00"
@str.10 = [31 x i8] c"There are no file in the disk!\00"
@str.11 = [23 x i8] c"No mounted filesystem!\00"
@str.12 = [9 x i8] c"Written!\00"
@reltable.write_file = [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([27 x i8]* @str to i64), i64 ptrtoint ([4 x i32]* @reltable.write_file to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([22 x i8]* @str.9 to i64), i64 ptrtoint ([4 x i32]* @reltable.write_file to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([31 x i8]* @str.10 to i64), i64 ptrtoint ([4 x i32]* @reltable.write_file to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([23 x i8]* @str.11 to i64), i64 ptrtoint ([4 x i32]* @reltable.write_file to i64)) to i32)]
define void @write_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 @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 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 @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 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
%31 = load i32, i32* @stdin
%32 = call i32 @fgets(i8* %24, i32 %30, i32 %31)
%33 = load i32, i32* %2
%34 = load i32, i32* %3
%35 = call i32 @myfs_file_write(i32 %33, i8* %24, i32 %34)
%36 = icmp ugt i32 %35, -5
br i1 %36, label %37, label %42
37: ; preds = %19
%38 = add i32 %35, 4
%39 = sext i32 %38 to i64
%40 = shl i64 %39, 2
%41 = call i8* @llvm.load.relative.i64(i8* bitcast ([4 x i32]* @reltable.write_file to i8*), i64 %40)
br label %42
42: ; preds = %19, %37
%43 = phi i8* [ %41, %37 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @str.12, i64 0, i64 0), %19 ]
%44 = call i32 @puts(i8* dereferenceable(1) %43)
ret void
}