#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int main(void) {
  FILE *fp;
  char words[41];
  if ((fp = fopen("wordy", "a+")) == NULL) {
    fprintf(stderr, "Can't open \"wordy\" file\n");
    exit(EXIT_FAILURE);
  }
  puts("Enter words to add to the file; provide the # key"
       " at the beginning of a line to terminate");
  while ((fscanf(stdin, "%40s", words) == 1) && (words[0] != '#')) {
    fprintf(fp, "%s\n", words);
  }
  puts("File contents:");
  rewind(fp);
  while (fscanf(fp, "%s", words) == 1) {
    puts(words);
  }
  puts("Done!");
  if (fclose(fp) != 0) {
    fprintf(stderr, "error closing file\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11c5 <main+0x55>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%rdi
lea    -0x40(%rbp),%rdx
lea    0xe96(%rip),%rsi
call   1060 <fscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x41(%rbp)
jne    1208 <main+0x98>
movsbl -0x40(%rbp),%eax
cmp    $0x23,%eax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1218 <main+0xa8>
jmp    1235 <main+0xc5>
mov    -0x10(%rbp),%rax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    11d1 <main+0x61>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xe3b(%rip),%rsi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    1275 <main+0x105>
lea    -0x40(%rbp),%rdi
call   1030 <puts@plt>
jmp    124a <main+0xda>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12ab <main+0x13b>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
lea    0xe7c(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     129b <main+0x12b>
mov    %rax,%r15
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e39(%rip),%r13
mov    0x0(%r13),%rdi
lea    0xec2(%rip),%rsi
mov    %rsp,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    120a <main+0x9a>
cmpb   $0x23,(%rsp)
je     120a <main+0x9a>
lea    0xead(%rip),%r14
mov    %rsp,%rbx
lea    0xe9e(%rip),%r12
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    120a <main+0x9a>
cmpb   $0x23,(%rsp)
jne    11e0 <main+0x70>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <rewind@plt>
lea    0xe6f(%rip),%rsi
mov    %rsp,%rdx
mov    %r15,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    125b <main+0xeb>
mov    %rsp,%rbx
lea    0xe55(%rip),%r14
nop
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
je     1240 <main+0xd0>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     128b <main+0x11b>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
lea    0xd5e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"wordy\00"
@.str.1 = [3 x i8] c"a+\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Can't open \22wordy\22 file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [90 x i8] c"Enter words to add to the file; provide the # key at the beginning of a line to terminate\00"
@stdin = global i32* null
@.str.4 = [5 x i8] c"%40s\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [15 x i8] c"File contents:\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [6 x i8] c"Done!\00"
@.str.9 = [20 x i8] c"error closing file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [41 x i8]
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = call i32 @puts(i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.3, i64 0, i64 0))
br label %13
13: ; preds = %25, %11
%14 = load i32*, i32** @stdin
%15 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%16 = call i32 @fscanf(i32* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %15)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 35
br label %23
23: ; preds = %18, %13
%24 = phi i1 [ false, %13 ], [ %22, %18 ]
br i1 %24, label %25, label %30
25: ; preds = %23
%26 = load i32*, i32** %2
%27 = ptrtoint i32* %26 to i32
%28 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %28)
br label %13
30: ; preds = %23
%31 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%32 = load i32*, i32** %2
%33 = call i32 @rewind(i32* %32)
br label %34
34: ; preds = %39, %30
%35 = load i32*, i32** %2
%36 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%37 = call i32 @fscanf(i32* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %36)
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%41 = call i32 @puts(i8* %40)
br label %34
42: ; preds = %34
%43 = call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%44 = load i32*, i32** %2
%45 = call i64 @fclose(i32* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
br label %50
50: ; preds = %47, %42
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"wordy\00"
@.str.1 = [3 x i8] c"a+\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Can't open \22wordy\22 file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [90 x i8] c"Enter words to add to the file; provide the # key at the beginning of a line to terminate\00"
@stdin = global i32* null
@.str.4 = [5 x i8] c"%40s\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [15 x i8] c"File contents:\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [6 x i8] c"Done!\00"
@.str.9 = [20 x i8] c"error closing file\0A\00"
define i32 @main() {
%1 = alloca [41 x i8]
%2 = getelementptr inbounds [41 x i8], [41 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([90 x i8], [90 x i8]* @.str.3, i64 0, i64 0))
%12 = load i32*, i32** @stdin
%13 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%14 = icmp eq i32 %13, 1
%15 = load i8, i8* %2
%16 = icmp ne i8 %15, 35
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %18, label %29
18: ; preds = %10
%19 = ptrtoint i32* %3 to i64
%20 = trunc i64 %19 to i32
br label %21
21: ; preds = %18, %21
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%23 = load i32*, i32** @stdin
%24 = call i32 @fscanf(i32* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%25 = icmp eq i32 %24, 1
%26 = load i8, i8* %2
%27 = icmp ne i8 %26, 35
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %21, label %29
29: ; preds = %21, %10
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%31 = call i32 @rewind(i32* %3)
%32 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %38
34: ; preds = %29, %34
%35 = call i32 @puts(i8* %2)
%36 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%37 = icmp eq i32 %36, 1
br i1 %37, label %34, label %38
38: ; preds = %34, %29
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%40 = call i64 @fclose(i32* %3)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
br label %45
45: ; preds = %42, %38
ret i32 0
}