#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int main() {
  FILE *fp;
  char line[128];
  printf("Type some text. Use EOF (CTRL+D) to end.\n");
  fp = fopen("out.txt", "w");
  if (!fp) {
    printf("Sorry, could not open %s for writing.\n", "out.txt");
    return EXIT_FAILURE;
  }
  while (fgets(line, sizeof(line), stdin)) {
    fputs(line, fp);
  }
  fclose(fp);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb3(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b8 <main+0x68>
lea    0xe9b(%rip),%rdi
lea    0xe8a(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120b <main+0xbb>
jmp    11bd <main+0x6d>
lea    -0x90(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     11f6 <main+0xa6>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <fputs@plt>
jmp    11bd <main+0x6d>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
lea    0xe81(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f1 <main+0x91>
mov    %rax,%r14
mov    0x2e4e(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rdi
mov    $0x80,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     11e0 <main+0x80>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fputs@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    11c0 <main+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2de1(%rip),%rax
jmp    120d <main+0xad>
lea    0xe12(%rip),%rdi
lea    0xe01(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Type some text. Use EOF (CTRL+D) to end.\0A\00"
@.str.1 = [8 x i8] c"out.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [39 x i8] c"Sorry, could not open %s for writing.\0A\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [128 x i8]
store i32 0, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%5 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
store i32 %10, i32* %1
br label %25
11: ; preds = %0
br label %12
12: ; preds = %17, %11
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* @stdin
%15 = call i64 @fgets(i8* %13, i32 128, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%19 = load i32*, i32** %2
%20 = call i32 @fputs(i8* %18, i32* %19)
br label %12
21: ; preds = %12
%22 = load i32*, i32** %2
%23 = call i32 @fclose(i32* %22)
%24 = load i32, i32* @EXIT_SUCCESS
store i32 %24, i32* %1
br label %25
25: ; preds = %21, %8
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"out.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [39 x i8] c"Sorry, could not open %s for writing.\0A\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [41 x i8] c"Type some text. Use EOF (CTRL+D) to end.\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = load i32, i32* @stdin
%8 = call i64 @fgets(i8* %2, i32 128, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %12
10: ; preds = %0
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %19
12: ; preds = %6, %12
%13 = call i32 @fputs(i8* %2, i32* %4)
%14 = load i32, i32* @stdin
%15 = call i64 @fgets(i8* %2, i32 128, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %12
17: ; preds = %12, %6
%18 = call i32 @fclose(i32* %4)
br label %19
19: ; preds = %17, %10
%20 = phi i32* [ @EXIT_SUCCESS, %17 ], [ @EXIT_FAILURE, %10 ]
%21 = load i32, i32* %20
ret i32 %21
}