#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 EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getchar();
int putc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
int main(void) {
  int ch;
  FILE *fp = fopen("output.log", "w");
  if (fp == NULL) {
    fprintf(stderr, "Error opening output file\n");
    exit(EXIT_FAILURE);
  }
  puts("Enter your lines of text and they will be written\n"
       "into the output.txt file. When you are finished,\n"
       "generate an end-of-file condition. On Unix this is\n"
       "done by pressing Ctrl-D whereas on Windows/DOS it is\n"
       "done by pressing F6 or Ctrl-Z then Enter.\n");
  while ((ch = getchar()) != EOF) {
    if (putc(ch, fp) == EOF) {
      fprintf(stderr, "Error writing to output file\n");
      exit(EXIT_FAILURE);
    }
  }
  if (fclose(fp) == EOF) {
    fprintf(stderr, "Error closing output file\n");
    exit(EXIT_FAILURE);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c4 <main+0x54>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%eax
je     122c <main+0xbc>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <putc@plt>
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%eax
jne    1227 <main+0xb7>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xf0a(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    11d0 <main+0x60>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%eax
jne    1267 <main+0xf7>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xee8(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe85(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1202 <main+0x92>
mov    %rax,%r14
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e46(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    (%rbx),%eax
je     11ec <main+0x7c>
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <putc@plt>
cmp    (%rbx),%eax
jne    11b0 <main+0x40>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xf45(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
cmp    (%rbx),%eax
je     1214 <main+0xa4>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
jmp    11d9 <main+0x69>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xf18(%rip),%rsi
jmp    11d9 <main+0x69>

LLVM IR (-O0):
@.str = [11 x i8] c"output.log\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Error opening output file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [246 x i8] c"Enter your lines of text and they will be written\0Ainto the output.txt file. When you are finished,\0Agenerate an end-of-file condition. On Unix this is\0Adone by pressing Ctrl-D whereas on Windows/DOS it is\0Adone by pressing F6 or Ctrl-Z then Enter.\0A\00"
@EOF = global i32 0
@.str.4 = [30 x i8] c"Error writing to output file\0A\00"
@.str.5 = [27 x i8] c"Error closing output file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = call i32 @puts(i8* getelementptr inbounds ([246 x i8], [246 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %29, %12
%15 = call i32 (...) @getchar()
store i32 %15, i32* %2
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load i32, i32* %2
%20 = load i32*, i32** %3
%21 = call i32 @putc(i32 %19, i32* %20)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
br label %14
30: ; preds = %14
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* @EOF
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"output.log\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Error opening output file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [246 x i8] c"Enter your lines of text and they will be written\0Ainto the output.txt file. When you are finished,\0Agenerate an end-of-file condition. On Unix this is\0Adone by pressing Ctrl-D whereas on Windows/DOS it is\0Adone by pressing F6 or Ctrl-Z then Enter.\0A\00"
@EOF = global i32 0
@.str.4 = [30 x i8] c"Error writing to output file\0A\00"
@.str.5 = [27 x i8] c"Error closing output file\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([246 x i8], [246 x i8]* @.str.3, i64 0, i64 0))
br label %10
10: ; preds = %14, %8
%11 = tail call i32 (...) @getchar()
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = tail call i32 @putc(i32 %11, i32* %1)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %10
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %10
%24 = tail call i32 @fclose(i32* %1)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
ret i32 0
}