#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*/ clear_stdin(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ sscanf(char *, char *, char *);
int /*<<< orphan*/ stdin;
void prompt_for_filename(char *res) {
  char buf[100];
  FILE *fp;
  while (1) {
    printf("Input file: ");
    if (fgets(buf, 100, stdin) == NULL) {
      clear_stdin(buf);
      printf("ERROR: You didn't enter any data!\n");
      continue;
    }
    if (sscanf(buf, "%s", res) <= 0) {
      clear_stdin(buf);
      printf("ERROR: You didn't enter a string!\n");
      continue;
    }
    if (!(fp = fopen(res, "r"))) {
      perror("ERROR: Could not open file!\n");
    } else {
      break;
    }
  }
}


x86 assembly (clang, -O0):
<prompt_for_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11b6 <prompt_for_filename+0x56>
lea    -0x70(%rbp),%rdi
call   1030 <clear_stdin@plt>
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
jmp    116f <prompt_for_filename+0xf>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe6b(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x0,%rax
jg     11ee <prompt_for_filename+0x8e>
lea    -0x70(%rbp),%rdi
call   1030 <clear_stdin@plt>
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
jmp    116f <prompt_for_filename+0xf>
mov    -0x8(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmp    $0x0,%rax
jne    121d <prompt_for_filename+0xbd>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
jmp    1222 <prompt_for_filename+0xc2>
jmp    1227 <prompt_for_filename+0xc7>
jmp    116f <prompt_for_filename+0xf>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prompt_for_filename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r15
lea    0xe78(%rip),%r14
mov    0x2e59(%rip),%r12
mov    %rsp,%rbx
lea    0xe74(%rip),%r13
lea    0xe70(%rip),%rbp
jmp    11c1 <prompt_for_filename+0x51>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1030 <clear_stdin@plt>
lea    0xe95(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     11b0 <prompt_for_filename+0x40>
mov    %r13,%rsi
mov    %r15,%rdx
call   1070 <sscanf@plt>
test   %rax,%rax
jle    1210 <prompt_for_filename+0xa0>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
jne    1226 <prompt_for_filename+0xb6>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
jmp    11c1 <prompt_for_filename+0x51>
mov    %rbx,%rdi
call   1030 <clear_stdin@plt>
lea    0xe10(%rip),%rdi
call   1040 <puts@plt>
jmp    11c1 <prompt_for_filename+0x51>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Input file: \00"
@stdin = global i32 0
@.str.1 = [35 x i8] c"ERROR: You didn't enter any data!\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [35 x i8] c"ERROR: You didn't enter a string!\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [29 x i8] c"ERROR: Could not open file!\0A\00"
define void @prompt_for_filename(i8* %0) {
%2 = alloca i8*
%3 = alloca [100 x i8]
%4 = alloca i32*
store i8* %0, i8** %2
br label %5
5: ; preds = %1, %11, %20, %31
%6 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32* @fgets(i8* %7, i32 100, i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %5
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%13 = call i32 @clear_stdin(i8* %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %5
15: ; preds = %5
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%17 = load i8*, i8** %2
%18 = call i64 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = icmp sle i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%22 = call i32 @clear_stdin(i8* %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %5
24: ; preds = %15
%25 = load i8*, i8** %2
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %26, i32** %4
%27 = icmp ne i32* %26, null
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %31
30: ; preds = %24
br label %32
31: ; preds = %28
br label %5
32: ; preds = %30
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"Input file: \00"
@stdin = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [29 x i8] c"ERROR: Could not open file!\0A\00"
@str = [34 x i8] c"ERROR: You didn't enter a string!\00"
@str.6 = [34 x i8] c"ERROR: You didn't enter any data!\00"
define void @prompt_for_filename(i8* %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %12, %1
%5 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32* @fgets(i8* %3, i32 100, i32 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = call i32 @clear_stdin(i8* %3)
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
br label %12
12: ; preds = %9, %16, %22
br label %4
13: ; preds = %4
%14 = call i64 @sscanf(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%15 = icmp slt i64 %14, 1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @clear_stdin(i8* %3)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %12
19: ; preds = %13
%20 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %12
24: ; preds = %19
ret void
}