#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*/ gets(char *);
int /*<<< orphan*/ printf(char *);
int main() {
  char c[1000];
  FILE *fptr;
  fptr = fopen("program.txt", "w");
  if (fptr == NULL) {
    printf("Error!");
    exit(1);
  }
  printf("Enter a sentence:\n");
  gets(c);
  fprintf(fptr, "%s", c);
  fclose(fptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x3f8(%rbp)
cmpq   $0x0,-0x3f8(%rbp)
jne    11b0 <main+0x50>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
lea    -0x3f0(%rbp),%rdi
call   1060 <gets@plt>
mov    -0x3f8(%rbp),%rdi
lea    -0x3f0(%rbp),%rdx
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x3f8(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x3e8,%rsp
lea    0xe7f(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11cc <main+0x5c>
mov    %rax,%rbx
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   1070 <gets@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"program.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"Error!\00"
@.str.3 = [19 x i8] c"Enter a sentence:\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 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 %10
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%12 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%13 = call i32 @gets(i8* %12)
%14 = load i32*, i32** %3
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%16 = call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %15)
%17 = load i32*, i32** %3
%18 = call i32 @fclose(i32* %17)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"program.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"Error!\00"
@str = [18 x i8] c"Enter a sentence:\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%10 = call i32 @gets(i8* %2)
%11 = call i32 @fputs(i8* %2, i32* %3)
%12 = call i32 @fclose(i32* %3)
ret i32 0
}