#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*/ ciao_at_exit(int);
int /*<<< orphan*/ ciao_open_emulator(char const *, int /*<<< orphan*/ **,
                                      char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
char *getenv(char *);
int /*<<< orphan*/ stderr;
FILE *ciao_open_embedded_qfile(const char *program_name) {
  FILE *qfile = NULL;
  const char *p;
  for (p = program_name; *p && *p != '/'; p++)
    ;
  if (*p != '/')
    ciao_open_emulator(program_name, &qfile, getenv("PATH"));
  if (qfile == NULL)
    ciao_open_emulator(program_name, &qfile, ".");
  if (qfile == NULL) {
    fprintf(stderr, "%s: file not found\n", program_name);
    ciao_at_exit(1);
  }
  return qfile;
}


x86 assembly (clang, -O0):
<ciao_open_embedded_qfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     1181 <ciao_open_embedded_qfile+0x41>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1191 <ciao_open_embedded_qfile+0x51>
jmp    11a7 <ciao_open_embedded_qfile+0x67>
jmp    1196 <ciao_open_embedded_qfile+0x56>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    115c <ciao_open_embedded_qfile+0x1c>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11db <ciao_open_embedded_qfile+0x9b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0xe3a(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rdx
lea    -0x10(%rbp),%rsi
call   1040 <ciao_open_emulator@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11fa <ciao_open_embedded_qfile+0xba>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
lea    0xe10(%rip),%rdx
call   1040 <ciao_open_emulator@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1228 <ciao_open_embedded_qfile+0xe8>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdee(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <ciao_at_exit@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ciao_open_embedded_qfile>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
mov    %rdi,%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%rax),%ecx
test   %cl,%cl
je     1172 <ciao_open_embedded_qfile+0x32>
cmp    $0x2f,%cl
je     1198 <ciao_open_embedded_qfile+0x58>
add    $0x1,%rax
jmp    1160 <ciao_open_embedded_qfile+0x20>
lea    0xe87(%rip),%rdi
call   1030 <getenv@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1040 <ciao_open_emulator@plt>
mov    0x8(%rsp),%rax
test   %rax,%rax
jne    11dd <ciao_open_embedded_qfile+0x9d>
lea    0xe66(%rip),%rdx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <ciao_open_emulator@plt>
mov    0x8(%rsp),%rax
test   %rax,%rax
jne    11dd <ciao_open_embedded_qfile+0x9d>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <ciao_at_exit@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c".\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"%s: file not found\0A\00"
define i32* @ciao_open_embedded_qfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
store i8* %0, i8** %2
store i32* null, i32** %3
%5 = load i8*, i8** %2
store i8* %5, i8** %4
br label %6
6: ; preds = %19, %1
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 47
br label %16
16: ; preds = %11, %6
%17 = phi i1 [ false, %6 ], [ %15, %11 ]
br i1 %17, label %18, label %22
18: ; preds = %16
br label %19
19: ; preds = %18
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
br label %6
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 47
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i8*, i8** %2
%29 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @ciao_open_emulator(i8* %28, i32** %3, i8* %29)
br label %31
31: ; preds = %27, %22
%32 = load i32*, i32** %3
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i8*, i8** %2
%36 = call i32 @ciao_open_emulator(i8* %35, i32** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %34, %31
%38 = load i32*, i32** %3
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %2
%43 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = call i32 @ciao_at_exit(i32 1)
br label %45
45: ; preds = %40, %37
%46 = load i32*, i32** %3
ret i32* %46
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c".\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"%s: file not found\0A\00"
define i32* @ciao_open_embedded_qfile(i8* %0) {
%2 = alloca i32*
%3 = bitcast i32** %2 to i8*
store i32* null, i32** %2
br label %4
4: ; preds = %7, %1
%5 = phi i8* [ %0, %1 ], [ %8, %7 ]
%6 = load i8, i8* %5
switch i8 %6, label %7 [
i8 47, label %14
i8 0, label %9
]
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %5, i64 1
br label %4
9: ; preds = %4
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @ciao_open_emulator(i8* %0, i32** %2, i8* %10)
%12 = load i32*, i32** %2
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %23
14: ; preds = %4, %9
%15 = call i32 @ciao_open_emulator(i8* %0, i32** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32*, i32** %2
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%21 = call i32 @ciao_at_exit(i32 1)
%22 = load i32*, i32** %2
br label %23
23: ; preds = %9, %18, %14
%24 = phi i32* [ %12, %9 ], [ %22, %18 ], [ %16, %14 ]
ret i32* %24
}