#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);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ *gets(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(void) {
  FILE *fp;
  char words[40];
  if ((fp = fopen("words", "a+")) == NULL) {
    fprintf(stdout, "Can't open \"word\" file.\n");
    exit(1);
  }
  puts("Enter words to add to the file: press the Enter");
  puts("Key at the beginning of a line to terminate.");
  while (gets(words) != NULL && words[0] != '\0')
    fprintf(fp, "%s ", words);
  puts("File contents: ");
  rewind(fp);
  while (fscanf(fp, "%s", words) == 1)
    puts(words);
  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    0xe6a(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11d1 <main+0x51>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
lea    -0x40(%rbp),%rdi
call   1080 <gets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x41(%rbp)
je     1211 <main+0x91>
movsbl -0x40(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1221 <main+0xa1>
jmp    123e <main+0xbe>
mov    -0x10(%rbp),%rax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    11e9 <main+0x69>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xe31(%rip),%rsi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    127e <main+0xfe>
lea    -0x40(%rbp),%rdi
call   1030 <puts@plt>
jmp    1253 <main+0xd3>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12a8 <main+0x128>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%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   %rbx
sub    $0x30,%rsp
lea    0xe70(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     128b <main+0x10b>
mov    %rax,%r15
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1080 <gets@plt>
test   %rax,%rax
je     1203 <main+0x83>
cmpb   $0x0,(%rsp)
je     1203 <main+0x83>
lea    0xea5(%rip),%r14
mov    %rsp,%rbx
nopl   (%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1080 <gets@plt>
test   %rax,%rax
je     1203 <main+0x83>
cmpb   $0x0,(%rsp)
jne    11e0 <main+0x60>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <rewind@plt>
lea    0xe75(%rip),%rsi
mov    %rsp,%rdx
mov    %r15,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    125b <main+0xdb>
mov    %rsp,%rbx
lea    0xe5b(%rip),%r14
nopl   0x0(%rax,%rax,1)
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+0xc0>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     127f <main+0xff>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

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