#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*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  char str[64];
  if ((fp = fopen("foo.txt", "w")) == NULL) {
    fprintf(stderr, "Could not open file %s.\n", "foo.txt");
    exit(-1);
  }
  do {
    printf("\nEnter a new string. Press <Enter> to finish: ");
    gets(str);
    fputs(str, fp);
    putc('\n', fp);
    if (ferror(fp)) {
      perror("Erro na gravacao");
      fclose(fp);
      exit(1);
    }
  } while (strlen(str) > 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe33(%rip),%rdi
lea    0xe34(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    120d <main+0x5d>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
lea    0xe02(%rip),%rdx
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
jmp    1212 <main+0x62>
lea    0xe0a(%rip),%rdi
call   1060 <printf@plt>
lea    -0x60(%rbp),%rdi
call   10a0 <gets@plt>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fputs@plt>
mov    -0x18(%rbp),%rsi
mov    $0xa,%edi
call   1080 <putc@plt>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1274 <main+0xc4>
lea    0xdf6(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    1279 <main+0xc9>
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jg     1212 <main+0x62>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
lea    0xe40(%rip),%rdi
lea    0xe41(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     124c <main+0x9c>
mov    %rax,%rbx
lea    0xe48(%rip),%r14
mov    %rsp,%r15
xchg   %ax,%ax
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   10a0 <gets@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <fputs@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1080 <putc@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    122e <main+0x7e>
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
jg     11e0 <main+0x30>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe1d(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
lea    0xd9d(%rip),%rdx
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <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 = [47 x i8] c"\0AEnter a new string. Press <Enter> to finish: \00"
@.str.4 = [17 x i8] c"Erro na gravacao\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]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = 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* %8, i32** %6
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, 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))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
br label %15
15: ; preds = %33, %14
%16 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 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)
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i32 @fputs(i8* %19, i32* %20)
%22 = load i32*, i32** %6
%23 = call i32 @putc(i8 signext 10, i32* %22)
%24 = load i32*, i32** %6
%25 = call i64 @ferror(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %15
br label %33
33: ; preds = %32
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%35 = call i64 @strlen(i8* %34)
%36 = icmp sgt i64 %35, 0
br i1 %36, label %15, label %37
37: ; preds = %33
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 = [47 x i8] c"\0AEnter a new string. Press <Enter> to finish: \00"
@.str.4 = [17 x i8] c"Erro na gravacao\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, %22
%12 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%13 = call i32 @gets(i8* %4)
%14 = call i32 @fputs(i8* %4, i32* %5)
%15 = call i32 @putc(i8 signext 10, i32* %5)
%16 = call i64 @ferror(i32* %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @fclose(i32* %5)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %11
%23 = call i64 @strlen(i8* %4)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %11, label %25
25: ; preds = %22
ret i32 0
}