#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
int main(int argc, char *argv[]) {
  FILE *fp;
  FILE *fpr;
  char ch[80];
  if (argc != 2) {
    printf("Please input a filename.\n");
    exit(1);
  }
  if ((fp = fopen(argv[1], "w")) == NULL) {
    printf("Cannot open file. \n");
    exit(1);
  }
  puts("Plese input something:");
  while (fgets(ch, 80, stdin) != NULL && ch[0] != '\n')
    fputs(ch, fp);
  fclose(fp);
  if ((fpr = fopen(argv[1], "r")) == NULL) {
    printf("Cannot open file. \n");
    exit(2);
  }
  puts("You've inputed:");
  while (fgets(ch, 80, fpr) != NULL && ch[0] != '\n')
    fputs(ch, stdout);
  fclose(fpr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a9 <main+0x39>
lea    0xe66(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e1 <main+0x71>
lea    0xe4a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
lea    -0x70(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%rdx
mov    $0x50,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x71(%rbp)
je     1224 <main+0xb4>
movsbl -0x70(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x71(%rbp)
mov    -0x71(%rbp),%al
test   $0x1,%al
jne    1234 <main+0xc4>
jmp    1246 <main+0xd6>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputs@plt>
jmp    11ed <main+0x7d>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xde9(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1287 <main+0x117>
lea    0xda4(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xdbb(%rip),%rdi
call   1030 <puts@plt>
lea    -0x70(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x50,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x72(%rbp)
je     12c4 <main+0x154>
movsbl -0x70(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x72(%rbp)
mov    -0x72(%rbp),%al
test   $0x1,%al
jne    12d4 <main+0x164>
jmp    12ec <main+0x17c>
lea    -0x70(%rbp),%rdi
mov    0x2cf9(%rip),%rax
mov    (%rax),%rsi
call   1060 <fputs@plt>
jmp    1293 <main+0x123>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
cmp    $0x2,%edi
jne    128c <main+0x12c>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1295 <main+0x135>
mov    %rax,%r15
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e42(%rip),%r12
mov    (%r12),%rdx
mov    %rsp,%rdi
mov    $0x50,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11f7 <main+0x97>
cmpb   $0xa,(%rsp)
je     11f7 <main+0x97>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <fputs@plt>
mov    (%r12),%rdx
mov    %rbx,%rdi
mov    $0x50,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11f7 <main+0x97>
cmpb   $0xa,(%rsp)
jne    11d0 <main+0x70>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x8(%r14),%rdi
lea    0xe0f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12ab <main+0x14b>
mov    %rax,%r14
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
mov    $0x50,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1276 <main+0x116>
cmpb   $0xa,(%rsp)
je     1276 <main+0x116>
mov    0x2d8f(%rip),%r15
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    (%r15),%rsi
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    %rbx,%rdi
mov    $0x50,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1276 <main+0x116>
cmpb   $0xa,(%rsp)
jne    1250 <main+0xf0>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdab(%rip),%rdi
jmp    129c <main+0x13c>
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd79(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Please input a filename.\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [20 x i8] c"Cannot open file. \0A\00"
@.str.3 = [23 x i8] c"Plese input something:\00"
@stdin = global i32* null
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [16 x i8] c"You've inputed:\00"
@stdout = global i32* null
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [80 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %6
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %37, %23
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%27 = load i32*, i32** @stdin
%28 = call i32* @fgets(i8* %26, i32 80, i32* %27)
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 10
br label %35
35: ; preds = %30, %25
%36 = phi i1 [ false, %25 ], [ %34, %30 ]
br i1 %36, label %37, label %41
37: ; preds = %35
%38 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%39 = load i32*, i32** %6
%40 = call i32 @fputs(i8* %38, i32* %39)
br label %25
41: ; preds = %35
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %47, i32** %7
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %52
49: ; preds = %41
%50 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 2)
unreachable
52: ; preds = %41
%53 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %66, %52
%55 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%56 = load i32*, i32** %7
%57 = call i32* @fgets(i8* %55, i32 80, i32* %56)
%58 = icmp ne i32* %57, null
br i1 %58, label %59, label %64
59: ; preds = %54
%60 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 10
br label %64
64: ; preds = %59, %54
%65 = phi i1 [ false, %54 ], [ %63, %59 ]
br i1 %65, label %66, label %70
66: ; preds = %64
%67 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%68 = load i32*, i32** @stdout
%69 = call i32 @fputs(i8* %67, i32* %68)
br label %54
70: ; preds = %64
%71 = load i32*, i32** %7
%72 = call i32 @fclose(i32* %71)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [23 x i8] c"Plese input something:\00"
@stdin = global i32* null
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [16 x i8] c"You've inputed:\00"
@stdout = global i32* null
@str.6 = [19 x i8] c"Cannot open file. \00"
@str.7 = [25 x i8] c"Please input a filename.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32*, i32** @stdin
%20 = call i32* @fgets(i8* %4, i32 80, i32* %19)
%21 = icmp ne i32* %20, null
%22 = load i8, i8* %4
%23 = icmp ne i8 %22, 10
%24 = select i1 %21, i1 %23, i1 false
br i1 %24, label %25, label %33
25: ; preds = %17, %25
%26 = call i32 @fputs(i8* %4, i32* %12)
%27 = load i32*, i32** @stdin
%28 = call i32* @fgets(i8* %4, i32 80, i32* %27)
%29 = icmp ne i32* %28, null
%30 = load i8, i8* %4
%31 = icmp ne i8 %30, 10
%32 = select i1 %29, i1 %31, i1 false
br i1 %32, label %25, label %33
33: ; preds = %25, %17
%34 = call i32 @fclose(i32* %12)
%35 = load i8*, i8** %10
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %33
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%43 = call i32* @fgets(i8* %4, i32 80, i32* %36)
%44 = icmp ne i32* %43, null
%45 = load i8, i8* %4
%46 = icmp ne i8 %45, 10
%47 = select i1 %44, i1 %46, i1 false
br i1 %47, label %48, label %56
48: ; preds = %41, %48
%49 = load i32*, i32** @stdout
%50 = call i32 @fputs(i8* %4, i32* %49)
%51 = call i32* @fgets(i8* %4, i32 80, i32* %36)
%52 = icmp ne i32* %51, null
%53 = load i8, i8* %4
%54 = icmp ne i8 %53, 10
%55 = select i1 %52, i1 %54, i1 false
br i1 %55, label %48, label %56
56: ; preds = %48, %41
%57 = call i32 @fclose(i32* %36)
ret i32 0
}