#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*/ FILENAME_MAX;
int /*<<< orphan*/ *fopen(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
FILE *pathopen(const char *name, const char *p, const char *mode,
               char *fullname) {
  FILE *fp;
  char buf[FILENAME_MAX + 1];
  char *bp, lastch;
  lastch = 'a';
  while (*p) {
    bp = buf;
    while (*p && *p != ':')
      lastch = *bp++ = *p++;
    if (lastch != '/')
      *bp++ = '/';
    strcpy(bp, name);
    if ((fp = fopen(buf, mode)) != NULL) {
      strncpy(fullname, buf, FILENAME_MAX);
      return fp;
    }
    if (*p)
      p++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<pathopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movb   $0x61,-0x49(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     127e <pathopen+0x14e>
mov    -0x58(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x59(%rbp)
je     11b5 <pathopen+0x85>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
setne  %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    11c5 <pathopen+0x95>
jmp    11ef <pathopen+0xbf>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%al
mov    -0x48(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x48(%rbp)
mov    %al,(%rcx)
mov    %al,-0x49(%rbp)
jmp    1190 <pathopen+0x60>
movsbl -0x49(%rbp),%eax
cmp    $0x2f,%eax
je     120e <pathopen+0xde>
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x48(%rbp)
movb   $0x2f,(%rax)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1260 <pathopen+0x130>
mov    -0x58(%rbp),%rsi
mov    -0x28(%rbp),%rdi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    128d <pathopen+0x15d>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1279 <pathopen+0x149>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    117b <pathopen+0x4b>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pathopen>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x38(%rbp)
mov    0x2e9c(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    (%rsi),%dl
test   %dl,%dl
je     11d3 <pathopen+0xa3>
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rcx,-0x30(%rbp)
mov    $0x61,%r15b
mov    %r13,%rdi
mov    %edx,%eax
nopw   0x0(%rax,%rax,1)
test   %al,%al
je     1188 <pathopen+0x58>
cmp    $0x3a,%al
jne    11c1 <pathopen+0x91>
cmp    $0x2f,%r15b
je     1195 <pathopen+0x65>
movb   $0x2f,(%rdi)
add    $0x1,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
mov    -0x38(%rbp),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
jne    11d8 <pathopen+0xa8>
cmpb   $0x1,(%rbx)
sbb    $0xffffffffffffffff,%rbx
movzbl (%rbx),%eax
mov    %r13,%rdi
test   %al,%al
jne    1180 <pathopen+0x50>
jmp    11d3 <pathopen+0xa3>
mov    %al,(%rdi)
add    $0x1,%rdi
mov    0x1(%rbx),%dl
add    $0x1,%rbx
mov    %eax,%r15d
jmp    1178 <pathopen+0x48>
xor    %r14d,%r14d
jmp    11f0 <pathopen+0xc0>
mov    %rax,%r14
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    -0x30(%rbp),%rdi
mov    %r13,%rsi
call   1030 <strncpy@plt>
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
define i32* @pathopen(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i8
%15 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%16 = load i32, i32* @FILENAME_MAX
%17 = add nsw i32 %16, 1
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %11
%20 = alloca i8, i64 %18
store i64 %18, i64* %12
store i8 97, i8* %14
br label %21
21: ; preds = %70, %4
%22 = load i8*, i8** %7
%23 = load i8, i8* %22
%24 = icmp ne i8 %23, 0
br i1 %24, label %25, label %71
25: ; preds = %21
store i8* %20, i8** %13
br label %26
26: ; preds = %38, %25
%27 = load i8*, i8** %7
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i8*, i8** %7
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 58
br label %36
36: ; preds = %31, %26
%37 = phi i1 [ false, %26 ], [ %35, %31 ]
br i1 %37, label %38, label %44
38: ; preds = %36
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %7
%41 = load i8, i8* %39
%42 = load i8*, i8** %13
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %13
store i8 %41, i8* %42
store i8 %41, i8* %14
br label %26
44: ; preds = %36
%45 = load i8, i8* %14
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 47
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i8*, i8** %13
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %13
store i8 47, i8* %49
br label %51
51: ; preds = %48, %44
%52 = load i8*, i8** %13
%53 = load i8*, i8** %6
%54 = call i32 @strcpy(i8* %52, i8* %53)
%55 = load i8*, i8** %8
%56 = call i32* @fopen(i8* %20, i8* %55)
store i32* %56, i32** %10
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %63
58: ; preds = %51
%59 = load i8*, i8** %9
%60 = load i32, i32* @FILENAME_MAX
%61 = call i32 @strncpy(i8* %59, i8* %20, i32 %60)
%62 = load i32*, i32** %10
store i32* %62, i32** %5
store i32 1, i32* %15
br label %72
63: ; preds = %51
%64 = load i8*, i8** %7
%65 = load i8, i8* %64
%66 = icmp ne i8 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load i8*, i8** %7
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %7
br label %70
70: ; preds = %67, %63
br label %21
71: ; preds = %21
store i32* null, i32** %5
store i32 1, i32* %15
br label %72
72: ; preds = %71, %58
%73 = load i8*, i8** %11
%74 = load i32*, i32** %5
ret i32* %74
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
define noalias i32* @pathopen(i8* %0, i8* nocapture %1, i8* nocapture %2, i8* %3) {
%5 = load i32, i32* @FILENAME_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = load i8, i8* %1
%10 = icmp eq i8 %9, 0
br i1 %10, label %43, label %11
11: ; preds = %4, %21
%12 = phi i8 [ %24, %21 ], [ %9, %4 ]
%13 = phi i8* [ %22, %21 ], [ %1, %4 ]
%14 = phi i8* [ %23, %21 ], [ %8, %4 ]
%15 = phi i8 [ %18, %21 ], [ 97, %4 ]
%16 = icmp eq i8 %15, 47
br label %17
17: ; preds = %11, %36
%18 = phi i8 [ %41, %36 ], [ %12, %11 ]
%19 = phi i8* [ %40, %36 ], [ %13, %11 ]
%20 = phi i8* [ %8, %36 ], [ %14, %11 ]
switch i8 %18, label %21 [
i8 0, label %25
i8 58, label %25
]
21: ; preds = %17
%22 = getelementptr inbounds i8, i8* %19, i64 1
%23 = getelementptr inbounds i8, i8* %20, i64 1
store i8 %18, i8* %20
%24 = load i8, i8* %22
br label %11
25: ; preds = %17, %17
br i1 %16, label %28, label %26
26: ; preds = %25
%27 = getelementptr inbounds i8, i8* %20, i64 1
store i8 47, i8* %20
br label %28
28: ; preds = %26, %25
%29 = phi i8* [ %27, %26 ], [ %20, %25 ]
%30 = call i32 @strcpy(i8* %29, i8* %0)
%31 = call i32* @fopen(i8* %8, i8* %2)
%32 = icmp eq i32* %31, null
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i32, i32* @FILENAME_MAX
%35 = call i32 @strncpy(i8* %3, i8* %8, i32 %34)
br label %43
36: ; preds = %28
%37 = load i8, i8* %19
%38 = icmp ne i8 %37, 0
%39 = zext i1 %38 to i64
%40 = getelementptr i8, i8* %19, i64 %39
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 0
br i1 %42, label %43, label %17
43: ; preds = %36, %4, %33
%44 = phi i32* [ %31, %33 ], [ null, %4 ], [ null, %36 ]
ret i32* %44
}