#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);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *fp;
  char str[64];
  int i;
  if ((fp = fopen("foo.txt", "w")) == NULL) {
    fprintf(stderr, "Could not open file %s.\n", "foo.txt");
    exit(-1);
  }
  printf("Enter the word(s) to be written: ");
  gets(str);
  for (i = 0; str[i]; i++)
    fputc(str[i], fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11cd <main+0x5d>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
lea    0xe42(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
call   1070 <gets@plt>
movl   $0x0,-0x64(%rbp)
movslq -0x64(%rbp),%rax
cmpb   $0x0,-0x60(%rbp,%rax,1)
je     121a <main+0xaa>
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,1),%al
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    11e9 <main+0x79>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
lea    0xe82(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e8 <main+0x78>
mov    %rax,%r14
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
mov    %rsp,%rdi
call   1070 <gets@plt>
mov    (%rsp),%al
test   %al,%al
je     11d6 <main+0x66>
lea    0x1(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl %al,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11c0 <main+0x50>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
lea    0xe01(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"foo.txt\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Could not open file %s.\0A\00"
@.str.3 = [34 x i8] c"Enter the word(s) to be written: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [64 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%18 = call i32 @gets(i8* %17)
store i32 0, i32* %8
br label %19
19: ; preds = %32, %15
%20 = load i32, i32* %8
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = icmp ne i8 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %19
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = load i32*, i32** %6
%31 = call i32 @fputc(i8 signext %29, i32* %30)
br label %32
32: ; preds = %25
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
store i32 %34, i32* %8
br label %19
35: ; preds = %19
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"foo.txt\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Could not open file %s.\0A\00"
@.str.3 = [34 x i8] c"Enter the word(s) to be written: \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%13 = call i32 @gets(i8* %4)
%14 = load i8, i8* %4
%15 = icmp eq i8 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %11, %16
%17 = phi i64 [ %20, %16 ], [ 0, %11 ]
%18 = phi i8 [ %22, %16 ], [ %14, %11 ]
%19 = call i32 @fputc(i8 signext %18, i32* %5)
%20 = add nuw i64 %17, 1
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %16
24: ; preds = %16, %11
%25 = call i32 @fclose(i32* %5)
ret i32 0
}