#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 /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int errno;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stdin;
int strlen(char *);
int main() {
  char buf[1000];
  char *filename = buf;
  fgets(filename, 1000, stdin);
  printf("filename: %s\n", filename);
  int len = strlen(filename);
  if (filename[len - 1] == '\n')
    filename[len - 1] = '\0';
  int fd = open(filename, O_RDONLY);
  if (fd == -1) {
    printf("Error in open! errno=%d\n", errno);
  } else {
    printf("open successful!\n");
    int sz;
    do {
      sz = read(fd, buf, 1000);
      if (sz == -1) {
        printf("Error in read! errno=%d\n", errno);
        break;
      }
      buf[sz] = '\0';
      printf("%s", buf);
    } while (sz);
    if (close(fd))
      printf("Error in close! errno=%d\n", errno);
    else
      printf("close successful!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x3f0(%rbp),%rax
mov    %rax,-0x3f8(%rbp)
mov    -0x3f8(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1070 <fgets@plt>
mov    -0x3f8(%rbp),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3f8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x3fc(%rbp)
mov    -0x3f8(%rbp),%rax
mov    -0x3fc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11f8 <main+0x98>
mov    -0x3f8(%rbp),%rax
mov    -0x3fc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x3f8(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x400(%rbp)
cmpl   $0xffffffff,-0x400(%rbp)
jne    123c <main+0xdc>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1305 <main+0x1a5>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x400(%rbp),%edi
lea    -0x3f0(%rbp),%rsi
mov    $0x3e8,%edx
call   1060 <read@plt>
mov    %eax,-0x404(%rbp)
cmpl   $0xffffffff,-0x404(%rbp)
jne    1290 <main+0x130>
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c1 <main+0x161>
movslq -0x404(%rbp),%rax
movb   $0x0,-0x3f0(%rbp,%rax,1)
lea    -0x3f0(%rbp),%rsi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x404(%rbp)
jne    124a <main+0xea>
mov    -0x400(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     12f2 <main+0x192>
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1300 <main+0x1a0>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1305 <main+0x1a5>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3e8,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   1080 <fgets@plt>
lea    0xe63(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11bd <main+0x4d>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1220 <main+0xb0>
mov    %eax,%r15d
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
lea    0xe54(%rip),%r14
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1232 <main+0xc2>
mov    %eax,%ebp
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
jne    11f0 <main+0x80>
jmp    1249 <main+0xd9>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdi
jmp    1266 <main+0xf6>
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    0xde5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%edi
call   1060 <close@plt>
test   %rax,%rax
je     126f <main+0xff>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
lea    0xddd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    127b <main+0x10b>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [14 x i8] c"filename: %s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"Error in open! errno=%d\0A\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"open successful!\0A\00"
@.str.3 = [25 x i8] c"Error in read! errno=%d\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [26 x i8] c"Error in close! errno=%d\0A\00"
@.str.6 = [19 x i8] c"close successful!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 1000, i32 %9)
%11 = load i8*, i8** %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %4
%15 = load i8*, i8** %3
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %29
23: ; preds = %0
%24 = load i8*, i8** %3
%25 = load i32, i32* %4
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %23, %0
%30 = load i8*, i8** %3
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* @errno
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %68
38: ; preds = %29
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %55, %38
%41 = load i32, i32* %5
%42 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 1000)
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* @errno
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %58
49: ; preds = %40
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %51
store i8 0, i8* %52
%53 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %49
%56 = load i32, i32* %6
%57 = icmp ne i32 %56, 0
br i1 %57, label %40, label %58
58: ; preds = %55, %46
%59 = load i32, i32* %5
%60 = call i64 @close(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load i32, i32* @errno
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %63)
br label %67
65: ; preds = %58
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %67
67: ; preds = %65, %62
br label %68
68: ; preds = %67, %35
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [14 x i8] c"filename: %s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"Error in open! errno=%d\0A\00"
@errno = global i32 0
@.str.3 = [25 x i8] c"Error in read! errno=%d\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [26 x i8] c"Error in close! errno=%d\0A\00"
@str = [17 x i8] c"open successful!\00"
@str.7 = [18 x i8] c"close successful!\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i32 @fgets(i8* %2, i32 1000, i32 %3)
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %2)
%6 = call i32 @strlen(i8* %2)
%7 = add nsw i32 %6, -1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 10
br i1 %11, label %12, label %13
12: ; preds = %0
store i8 0, i8* %9
br label %13
13: ; preds = %12, %0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %2, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %41
20: ; preds = %13
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %28, %20
%23 = call i32 @read(i32 %15, i8* %2, i32 1000)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %33
28: ; preds = %22
%29 = sext i32 %23 to i64
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%32 = icmp eq i32 %23, 0
br i1 %32, label %33, label %22
33: ; preds = %28, %25
%34 = call i64 @close(i32 %15)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i32, i32* @errno
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %37)
br label %41
39: ; preds = %33
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
br label %41
41: ; preds = %36, %39, %17
ret i32 0
}