#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
char getc(int /*<<< orphan*/);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  FILE *fp;
  char ch, filename[40], mode[4];
  while (1) {
    printf("\nEnter a filename: ");
    gets(filename);
    printf("\nEnter a mode (max 3 characters): ");
    gets(mode);
    if ((fp = fopen(filename, mode)) != NULL) {
      printf("\nSuccessful opening %s in mode %s.\n", filename, mode);
      fclose(fp);
      puts("Enter x to exit, any other to continue.");
      if ((ch = getc(stdin)) == 'x')
        break;
      else
        continue;
    } else {
      fprintf(stderr, "\nError opening file %s in mode %s.\n", filename, mode);
      puts("Enter x to exit, any other to try again.");
      if ((ch = getc(stdin)) == 'x')
        break;
      else
        continue;
    }
  }
  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    0xe73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x50(%rbp),%rdi
call   1070 <gets@plt>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x54(%rbp),%rdi
call   1070 <gets@plt>
lea    -0x50(%rbp),%rdi
lea    -0x54(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1221 <main+0xb1>
lea    -0x50(%rbp),%rsi
lea    -0x54(%rbp),%rdx
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1090 <getc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
cmp    $0x78,%eax
jne    121c <main+0xac>
jmp    1271 <main+0x101>
jmp    1186 <main+0x16>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rdx
lea    -0x54(%rbp),%rcx
lea    0xe4a(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1090 <getc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
cmp    $0x78,%eax
jne    126c <main+0xfc>
jmp    1271 <main+0x101>
jmp    1186 <main+0x16>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xe7b(%rip),%r14
lea    0x10(%rsp),%r12
lea    0xe83(%rip),%r15
lea    0xc(%rsp),%rbx
mov    0x2e4b(%rip),%rbp
jmp    11d3 <main+0x63>
nop
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xed3(%rip),%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1060 <fprintf@plt>
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
mov    0x0(%rbp),%edi
call   1090 <getc@plt>
cmp    $0x78,%al
je     122f <main+0xbf>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
call   1070 <gets@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <gets@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11a0 <main+0x30>
mov    %rax,%r13
lea    0xe26(%rip),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
lea    0xe2e(%rip),%rdi
jmp    11c2 <main+0x52>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"\0AEnter a filename: \00"
@.str.1 = [35 x i8] c"\0AEnter a mode (max 3 characters): \00"
@.str.2 = [36 x i8] c"\0ASuccessful opening %s in mode %s.\0A\00"
@.str.3 = [40 x i8] c"Enter x to exit, any other to continue.\00"
@stdin = global i32 0
@stderr = global i32 0
@.str.4 = [36 x i8] c"\0AError opening file %s in mode %s.\0A\00"
@.str.5 = [41 x i8] c"Enter x to exit, any other to try again.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8
%8 = alloca [40 x i8]
%9 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %10
10: ; preds = %2, %33, %45
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%13 = call i32 @gets(i8* %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%16 = call i32 @gets(i8* %15)
%17 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%19 = call i32* @fopen(i8* %17, i8* %18)
store i32* %19, i32** %6
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %34
21: ; preds = %10
%22 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %22, i8* %23)
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
%27 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* @stdin
%29 = call signext i8 @getc(i32 %28)
store i8 %29, i8* %7
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 120
br i1 %31, label %32, label %33
32: ; preds = %21
br label %46
33: ; preds = %21
br label %10
34: ; preds = %10
%35 = load i32, i32* @stderr
%36 = getelementptr inbounds [40 x i8], [40 x i8]* %8, i64 0, i64 0
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %36, i8* %37)
%39 = call i32 @puts(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* @stdin
%41 = call signext i8 @getc(i32 %40)
store i8 %41, i8* %7
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 120
br i1 %43, label %44, label %45
44: ; preds = %34
br label %46
45: ; preds = %34
br label %10
46: ; preds = %44, %32
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"\0AEnter a filename: \00"
@.str.1 = [35 x i8] c"\0AEnter a mode (max 3 characters): \00"
@.str.2 = [36 x i8] c"\0ASuccessful opening %s in mode %s.\0A\00"
@.str.3 = [40 x i8] c"Enter x to exit, any other to continue.\00"
@stdin = global i32 0
@stderr = global i32 0
@.str.4 = [36 x i8] c"\0AError opening file %s in mode %s.\0A\00"
@.str.5 = [41 x i8] c"Enter x to exit, any other to try again.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [40 x i8]
%4 = alloca [4 x i8]
%5 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
br label %7
7: ; preds = %21, %2
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @gets(i8* %5)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @gets(i8* %6)
%12 = call i32* @fopen(i8* %5, i8* %6)
%13 = icmp eq i32* %12, null
br i1 %13, label %22, label %14
14: ; preds = %7
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %6)
%16 = call i32 @fclose(i32* %12)
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* @stdin
%19 = call signext i8 @getc(i32 %18)
%20 = icmp eq i8 %19, 120
br i1 %20, label %29, label %21
21: ; preds = %14, %22
br label %7
22: ; preds = %7
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %5, i8* %6)
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
%26 = load i32, i32* @stdin
%27 = call signext i8 @getc(i32 %26)
%28 = icmp eq i8 %27, 120
br i1 %28, label %29, label %21
29: ; preds = %22, %14
ret i32 0
}