#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*/ fputc(char, int /*<<< orphan*/ *);
char getche();
int /*<<< orphan*/ printf(char *);
int main() {
  FILE *fp;
  char ch;
  if ((fp = fopen("arqtext.txt", "w")) == NULL) {
    printf("\nO arquivo não pode ser aberto");
    exit(1);
  } else {
    printf("\nO arquivo foi aberto para gravação");
  }
  printf("\nEntre com o texto:");
  while ((ch = getche()) != '\r') {
    fputc(ch, fp);
  }
  fclose(fp);
  printf("\nFim do programa!");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a6 <main+0x46>
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe81(%rip),%rdi
call   1040 <printf@plt>
lea    0xe9b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getche@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0xd,%eax
je     11e8 <main+0x88>
mov    -0x11(%rbp),%al
mov    -0x10(%rbp),%rsi
movsbl %al,%edi
call   1050 <fputc@plt>
jmp    11be <main+0x5e>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe98(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d0 <main+0x70>
mov    %rax,%rbx
lea    0xeab(%rip),%rdi
call   1040 <printf@plt>
lea    0xec5(%rip),%rdi
call   1040 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1060 <getche@plt>
cmp    $0xd,%al
je     11b8 <main+0x58>
movsbl %al,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
jmp    11a0 <main+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xea1(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"arqtext.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [32 x i8] c"\0AO arquivo n\C3\A3o pode ser aberto\00"
@.str.3 = [38 x i8] c"\0AO arquivo foi aberto para grava\C3\A7\C3\A3o\00"
@.str.4 = [20 x i8] c"\0AEntre com o texto:\00"
@.str.5 = [18 x i8] c"\0AFim do programa!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8
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** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %11
11: ; preds = %9
%12 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %13
13: ; preds = %17, %11
%14 = call signext i8 (...) @getche()
store i8 %14, i8* %3
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 13
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i8, i8* %3
%19 = load i32*, i32** %2
%20 = call i32 @fputc(i8 signext %18, i32* %19)
br label %13
21: ; preds = %13
%22 = load i32*, i32** %2
%23 = call i32 @fclose(i32* %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"arqtext.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [32 x i8] c"\0AO arquivo n\C3\A3o pode ser aberto\00"
@.str.3 = [38 x i8] c"\0AO arquivo foi aberto para grava\C3\A7\C3\A3o\00"
@.str.4 = [20 x i8] c"\0AEntre com o texto:\00"
@.str.5 = [18 x i8] c"\0AFim do programa!\00"
define i32 @main() {
%1 = 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))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%9 = tail call signext i8 (...) @getche()
%10 = icmp eq i8 %9, 13
br i1 %10, label %16, label %11
11: ; preds = %6, %11
%12 = phi i8 [ %14, %11 ], [ %9, %6 ]
%13 = tail call i32 @fputc(i8 signext %12, i32* %1)
%14 = tail call signext i8 (...) @getche()
%15 = icmp eq i8 %14, 13
br i1 %15, label %16, label %11
16: ; preds = %11, %6
%17 = tail call i32 @fclose(i32* %1)
%18 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}