#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*/ *fopen(char *, char *);
char getc(int /*<<< orphan*/ *);
char getchar();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ puts(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  FILE *fpr;
  char ch;
  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, end with #:");
  do {
    ch = getchar();
    putc(ch, fp);
  } while (ch != '#');
  fclose(fp);
  if ((fpr = fopen(argv[1], "r")) == NULL) {
    printf("Cannot open file. \n");
    exit(2);
  }
  puts("You've inputed:");
  do {
    ch = getc(fpr);
    putchar(ch);
  } while (ch != '#');
  fclose(fpr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c6 <main+0x36>
lea    0xe49(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe45(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11fe <main+0x6e>
lea    0xe2d(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1070 <putc@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x23,%eax
jne    120a <main+0x7a>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1271 <main+0xe1>
lea    0xdba(%rip),%rdi
call   1060 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    0xddd(%rip),%rdi
call   1040 <puts@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <getc@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%edi
call   1030 <putchar@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x23,%eax
jne    127d <main+0xed>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    1237 <main+0xb7>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1240 <main+0xc0>
mov    %rax,%rbx
lea    0xe4f(%rip),%rdi
call   1040 <puts@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <getchar@plt>
movsbl %al,%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <putc@plt>
cmp    $0x23,%bpl
jne    11c0 <main+0x40>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x8(%r14),%rdi
lea    0xe38(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1256 <main+0xd6>
mov    %rax,%rbx
lea    0xe26(%rip),%rdi
call   1040 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <getc@plt>
movsbl %al,%ebp
mov    %ebp,%edi
call   1030 <putchar@plt>
cmp    $0x23,%bpl
jne    1210 <main+0x90>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe0c(%rip),%rdi
jmp    1247 <main+0xc7>
lea    0xdf0(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   1090 <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 = [35 x i8] c"Plese input something, end with #:\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [16 x i8] c"You've inputed:\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca 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 ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %30, %23
%26 = call signext i8 (...) @getchar()
store i8 %26, i8* %8
%27 = load i8, i8* %8
%28 = load i32*, i32** %6
%29 = call i32 @putc(i8 signext %27, i32* %28)
br label %30
30: ; preds = %25
%31 = load i8, i8* %8
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 35
br i1 %33, label %25, label %34
34: ; preds = %30
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %40, i32** %7
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %45
42: ; preds = %34
%43 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 2)
unreachable
45: ; preds = %34
%46 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %47
47: ; preds = %52, %45
%48 = load i32*, i32** %7
%49 = call signext i8 @getc(i32* %48)
store i8 %49, i8* %8
%50 = load i8, i8* %8
%51 = call i32 @putchar(i8 signext %50)
br label %52
52: ; preds = %47
%53 = load i8, i8* %8
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 35
br i1 %55, label %47, label %56
56: ; preds = %52
%57 = load i32*, i32** %7
%58 = call i32 @fclose(i32* %57)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"Plese input something, end with #:\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [16 x i8] c"You've inputed:\00"
@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 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %17, %15
%18 = tail call signext i8 (...) @getchar()
%19 = tail call i32 @putc(i8 signext %18, i32* %10)
%20 = icmp eq i8 %18, 35
br i1 %20, label %21, label %17
21: ; preds = %17
%22 = tail call i32 @fclose(i32* %10)
%23 = load i8*, i8** %8
%24 = tail call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%28 = tail call i32 @exit(i32 2)
unreachable
29: ; preds = %21
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %31, %29
%32 = tail call signext i8 @getc(i32* %24)
%33 = tail call i32 @putchar(i8 signext %32)
%34 = icmp eq i8 %32, 35
br i1 %34, label %35, label %31
35: ; preds = %31
%36 = tail call i32 @fclose(i32* %24)
ret i32 0
}