#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 *);
char getchar();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  FILE *arquivo;
  char caracter;
  if (argc < 2) {
    printf("\nErro: Digite o nome do arquivo !!!\n\n");
    exit(1);
  }
  if ((arquivo = fopen(argv[1], "w")) == NULL) {
    printf("Erro ao abrir arquivo!!!\n\n");
    exit(1);
  }
  do {
    caracter = getchar();
    putc(caracter, arquivo);
  } while (caracter != '$');
  fclose(arquivo);
  printf("\nGravado com sucesso em %s\n\n", argv[1]);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1198 <main+0x38>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d2 <main+0x72>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11d7 <main+0x77>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1050 <putc@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x24,%eax
jne    11d7 <main+0x77>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jle    11db <main+0x6b>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e4 <main+0x74>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1070 <getchar@plt>
movsbl %al,%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <putc@plt>
cmp    $0x24,%bpl
jne    11a0 <main+0x30>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%r14),%rsi
lea    0xe35(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe57(%rip),%rdi
jmp    11eb <main+0x7b>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"\0AErro: Digite o nome do arquivo !!!\0A\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [27 x i8] c"Erro ao abrir arquivo!!!\0A\0A\00"
@.str.3 = [29 x i8] c"\0AGravado com sucesso em %s\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
br label %23
23: ; preds = %28, %22
%24 = call signext i8 (...) @getchar()
store i8 %24, i8* %7
%25 = load i8, i8* %7
%26 = load i32*, i32** %6
%27 = call i32 @putc(i8 signext %25, i32* %26)
br label %28
28: ; preds = %23
%29 = load i8, i8* %7
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 36
br i1 %31, label %23, label %32
32: ; preds = %28
%33 = load i32*, i32** %6
%34 = call i32 @fclose(i32* %33)
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %37)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [29 x i8] c"\0AGravado com sucesso em %s\0A\0A\00"
@str = [26 x i8] c"Erro ao abrir arquivo!!!\0A\00"
@str.4 = [37 x i8] c"\0AErro: Digite o nome do arquivo !!!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.4, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7, %15
%16 = tail call signext i8 (...) @getchar()
%17 = tail call i32 @putc(i8 signext %16, i32* %10)
%18 = icmp eq i8 %16, 36
br i1 %18, label %19, label %15
19: ; preds = %15
%20 = tail call i32 @fclose(i32* %10)
%21 = load i8*, i8** %8
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %21)
ret i32 0
}