#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*/ fname;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int _POSIX_PATH_MAX;
int /*<<< orphan*/ fd_printf(int /*<<< orphan*/, char *);
int fd_readline(int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
char *strdup(char *);
char *read_filename(void) {
  char fname[_POSIX_PATH_MAX];
  int r;
  fd_printf(STDOUT_FILENO, "\r\n*** file: ");
  r = fd_readline(STDIN_FILENO, STDOUT_FILENO, fname, sizeof(fname));
  fd_printf(STDOUT_FILENO, "\r\n");
  if (r < 0)
    return NULL;
  else
    return strdup(fname);
}


x86 assembly (clang, -O0):
<read_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x28(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1040 <fd_printf@plt>
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%edi
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%ecx
call   1030 <fd_readline@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1040 <fd_printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    11d4 <read_filename+0xa4>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    11e8 <read_filename+0xb8>
mov    -0x30(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_filename>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2eb7(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e89(%rip),%r15
mov    (%r15),%edi
lea    0xe97(%rip),%rsi
call   1040 <fd_printf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%esi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1030 <fd_readline@plt>
mov    %eax,%r14d
mov    (%r15),%edi
lea    0xe7b(%rip),%rsi
call   1040 <fd_printf@plt>
test   %r14d,%r14d
js     11a6 <read_filename+0x76>
mov    %rbx,%rdi
call   1050 <strdup@plt>
jmp    11a8 <read_filename+0x78>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_POSIX_PATH_MAX = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [13 x i8] c"\0D\0A*** file: \00"
@STDIN_FILENO = global i32 0
@.str.1 = [3 x i8] c"\0D\0A\00"
define i8* @read_filename() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @_POSIX_PATH_MAX
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %2
%9 = alloca i8, i64 %7
store i64 %7, i64* %3
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @fd_printf(i32 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @STDIN_FILENO
%13 = load i32, i32* @STDOUT_FILENO
%14 = trunc i64 %7 to i32
%15 = call i32 @fd_readline(i32 %12, i32 %13, i8* %9, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @fd_printf(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %0
store i8* null, i8** %1
store i32 1, i32* %5
br label %23
21: ; preds = %0
%22 = call i8* @strdup(i8* %9)
store i8* %22, i8** %1
store i32 1, i32* %5
br label %23
23: ; preds = %21, %20
%24 = load i8*, i8** %2
%25 = load i8*, i8** %1
ret i8* %25
}

LLVM IR (-O2):
@_POSIX_PATH_MAX = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [13 x i8] c"\0D\0A*** file: \00"
@STDIN_FILENO = global i32 0
@.str.1 = [3 x i8] c"\0D\0A\00"
define noalias i8* @read_filename() {
%1 = load i32, i32* @_POSIX_PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = load i32, i32* @STDOUT_FILENO
%5 = tail call i32 @fd_printf(i32 %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @STDIN_FILENO
%7 = load i32, i32* @STDOUT_FILENO
%8 = call i32 @fd_readline(i32 %6, i32 %7, i8* %3, i32 %1)
%9 = load i32, i32* @STDOUT_FILENO
%10 = call i32 @fd_printf(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp slt i32 %8, 0
br i1 %11, label %14, label %12
12: ; preds = %0
%13 = call i8* @strdup(i8* %3)
br label %14
14: ; preds = %0, %12
%15 = phi i8* [ %13, %12 ], [ null, %0 ]
ret i8* %15
}