#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
char *strchr(char *, char);
char *strdup(char *);
scalar_t__ strlen(char *);
char *qualifyWithFirst(char *filename, char *searchPath) {
  char *paths = NULL;
  char *savepaths = NULL;
  char *path;
  char *chance;
  FILE *f;
  if (filename == NULL || searchPath == NULL)
    return NULL;
  paths = strdup(searchPath);
  savepaths = paths;
  while (1) {
    if ((path = strchr(paths, ':')) != NULL)
      *path = 0;
    chance = (char *)malloc(strlen(paths) + strlen(filename) + 2);
    sprintf(chance, "%s/%s", paths, filename);
    if (f = fopen(chance, "r")) {
      fclose(f);
      if (path)
        *path = ':';
      free(savepaths);
      return chance;
    }
    free(chance);
    if (path == NULL)
      break;
    paths = path + 1;
  }
  free(savepaths);
  return NULL;
}


x86 assembly (clang, -O0):
<qualifyWithFirst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b6 <qualifyWithFirst+0x36>
cmpq   $0x0,-0x18(%rbp)
jne    11c3 <qualifyWithFirst+0x43>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <qualifyWithFirst+0x147>
mov    -0x18(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11fb <qualifyWithFirst+0x7b>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdc8(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xdbe(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     128c <qualifyWithFirst+0x10c>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
je     1276 <qualifyWithFirst+0xf6>
mov    -0x30(%rbp),%rax
movb   $0x3a,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c7 <qualifyWithFirst+0x147>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x30(%rbp)
jne    12a5 <qualifyWithFirst+0x125>
jmp    12b6 <qualifyWithFirst+0x136>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11d8 <qualifyWithFirst+0x58>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qualifyWithFirst>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rdi,%rdi
je     124f <qualifyWithFirst+0xcf>
test   %rsi,%rsi
je     124f <qualifyWithFirst+0xcf>
mov    %rdi,%r13
mov    %rsi,%rdi
call   10a0 <strdup@plt>
mov    %rax,%r14
lea    0xe4c(%rip),%r15
lea    0xe4b(%rip),%r12
mov    %rax,(%rsp)
nop
mov    %r14,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d8 <qualifyWithFirst+0x58>
movb   $0x0,(%rbx)
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %r13,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r13,%rcx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
jne    1232 <qualifyWithFirst+0xb2>
mov    %rbp,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     1244 <qualifyWithFirst+0xc4>
add    $0x1,%rbx
mov    %rbx,%r14
jmp    11c0 <qualifyWithFirst+0x40>
mov    %rax,%rdi
call   1040 <fclose@plt>
test   %rbx,%rbx
je     1246 <qualifyWithFirst+0xc6>
movb   $0x3a,(%rbx)
jmp    1246 <qualifyWithFirst+0xc6>
xor    %ebp,%ebp
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @qualifyWithFirst(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %13, %2
store i8* null, i8** %3
br label %66
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
store i8* %20, i8** %7
br label %21
21: ; preds = %17, %60
%22 = load i8*, i8** %6
%23 = call i8* @strchr(i8* %22, i8 signext 58)
store i8* %23, i8** %8
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i8*, i8** %8
store i8 0, i8* %26
br label %27
27: ; preds = %25, %21
%28 = load i8*, i8** %6
%29 = call i64 @strlen(i8* %28)
%30 = load i8*, i8** %4
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 %29, %31
%33 = add nsw i64 %32, 2
%34 = call i64 @malloc(i64 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %9
%36 = load i8*, i8** %9
%37 = load i8*, i8** %6
%38 = load i8*, i8** %4
%39 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %37, i8* %38)
%40 = load i8*, i8** %9
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %41, i32** %10
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %54
43: ; preds = %27
%44 = load i32*, i32** %10
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = load i8*, i8** %8
store i8 58, i8* %49
br label %50
50: ; preds = %48, %43
%51 = load i8*, i8** %7
%52 = call i32 @free(i8* %51)
%53 = load i8*, i8** %9
store i8* %53, i8** %3
br label %66
54: ; preds = %27
%55 = load i8*, i8** %9
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %8
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %60
59: ; preds = %54
br label %63
60: ; preds = %54
%61 = load i8*, i8** %8
%62 = getelementptr inbounds i8, i8* %61, i64 1
store i8* %62, i8** %6
br label %21
63: ; preds = %59
%64 = load i8*, i8** %7
%65 = call i32 @free(i8* %64)
store i8* null, i8** %3
br label %66
66: ; preds = %63, %50, %16
%67 = load i8*, i8** %3
ret i8* %67
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @qualifyWithFirst(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %34, label %6
6: ; preds = %2
%7 = tail call i8* @strdup(i8* %1)
br label %8
8: ; preds = %29, %6
%9 = phi i8* [ %7, %6 ], [ %30, %29 ]
%10 = tail call i8* @strchr(i8* dereferenceable(1) %9, i8 signext 58)
%11 = icmp eq i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %8
store i8 0, i8* %10
br label %13
13: ; preds = %12, %8
%14 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%15 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%16 = add i64 %14, 2
%17 = add i64 %16, %15
%18 = tail call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 @sprintf(i8* dereferenceable(1) %19, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %0)
%21 = tail call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %13
%24 = inttoptr i64 %18 to i8*
%25 = tail call i32 @fclose(i32* %21)
br i1 %11, label %31, label %26
26: ; preds = %23
store i8 58, i8* %10
br label %31
27: ; preds = %13
%28 = tail call i32 @free(i8* %19)
br i1 %11, label %31, label %29
29: ; preds = %27
%30 = getelementptr inbounds i8, i8* %10, i64 1
br label %8
31: ; preds = %27, %23, %26
%32 = phi i8* [ %24, %26 ], [ %24, %23 ], [ null, %27 ]
%33 = tail call i32 @free(i8* %7)
br label %34
34: ; preds = %31, %2
%35 = phi i8* [ null, %2 ], [ %32, %31 ]
ret i8* %35
}