#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*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *getstring();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
FILE *open_file(char mode) {
  printf("Insert filename:\n");
  char *filename = getstring();
  FILE *file;
  switch (mode) {
  case 'r':
    file = fopen(filename, "r");
    break;
  case 'w':
    file = fopen(filename, "w+");
    break;
  default:
    puts("Wrong use of the open_file function");
    file = fopen(filename, "r+");
  }
  if (filename) {
    free(filename);
  }
  if (file == NULL) {
    puts("Error reading file");
  }
  return file;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
lea    0xe9b(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getstring@plt>
mov    %rax,-0x10(%rbp)
movsbl -0x1(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x72,%eax
je     119b <open_file+0x4b>
jmp    118a <open_file+0x3a>
mov    -0x1c(%rbp),%eax
sub    $0x77,%eax
je     11b4 <open_file+0x64>
jmp    11cd <open_file+0x7d>
mov    -0x10(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    11ed <open_file+0x9d>
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    11ed <open_file+0x9d>
lea    0xe43(%rip),%rdi
call   1050 <puts@plt>
mov    -0x10(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1201 <open_file+0xb1>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1218 <open_file+0xc8>
lea    0xe2b(%rip),%rdi
call   1050 <puts@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xef2(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1040 <getstring@plt>
mov    %rax,%r14
cmp    $0x72,%ebx
je     116f <open_file+0x2f>
cmp    $0x77,%ebx
jne    1178 <open_file+0x38>
lea    0xe95(%rip),%rsi
jmp    118b <open_file+0x4b>
lea    0xe8a(%rip),%rsi
jmp    118b <open_file+0x4b>
lea    0xe86(%rip),%rdi
call   1050 <puts@plt>
lea    0xe9e(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %r14,%r14
je     11a3 <open_file+0x63>
mov    %r14,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
jne    11b4 <open_file+0x74>
lea    0xe7d(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Insert filename:\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [36 x i8] c"Wrong use of the open_file function\00"
@.str.4 = [3 x i8] c"r+\00"
@.str.5 = [19 x i8] c"Error reading file\00"
define i32* @open_file(i8 signext %0) {
%2 = alloca i8
%3 = alloca i8*
%4 = alloca i32*
store i8 %0, i8* %2
%5 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = call i8* (...) @getstring()
store i8* %6, i8** %3
%7 = load i8, i8* %2
%8 = sext i8 %7 to i32
switch i32 %8, label %15 [
i32 114, label %9
i32 119, label %12
]
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %4
br label %19
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %14, i32** %4
br label %19
15: ; preds = %1
%16 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%17 = load i8*, i8** %3
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %18, i32** %4
br label %19
19: ; preds = %15, %12, %9
%20 = load i8*, i8** %3
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %3
%24 = call i32 @free(i8* %23)
br label %25
25: ; preds = %22, %19
%26 = load i32*, i32** %4
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = load i32*, i32** %4
ret i32* %31
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [36 x i8] c"Wrong use of the open_file function\00"
@.str.4 = [3 x i8] c"r+\00"
@.str.5 = [19 x i8] c"Error reading file\00"
@str = [17 x i8] c"Insert filename:\00"
define i32* @open_file(i8 signext %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%3 = tail call i8* (...) @getstring()
%4 = sext i8 %0 to i32
switch i32 %4, label %6 [
i32 114, label %8
i32 119, label %5
]
5: ; preds = %1
br label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %8
8: ; preds = %1, %6, %5
%9 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %6 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %5 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %1 ]
%10 = tail call i32* @fopen(i8* %3, i8* %9)
%11 = icmp eq i8* %3, null
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @free(i8* %3)
br label %14
14: ; preds = %12, %8
%15 = icmp eq i32* %10, null
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
ret i32* %10
}