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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *search_in_path(char *);
char *strdup(char *);
char *strrchr(char *, char);
char *find_base_path(char *filename) {
  char *x;
  char *path = strdup(filename);
  x = strrchr(path, '/');
  if (x) {
    *x = 0;
  } else {
    x = search_in_path(filename);
    if (x) {
      free(path);
      return x;
    }
  }
  return path;
}


x86 assembly (clang, -O0):
<find_base_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1182 <find_base_path+0x42>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b5 <find_base_path+0x75>
mov    -0x10(%rbp),%rdi
call   1060 <search_in_path@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b0 <find_base_path+0x70>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bd <find_base_path+0x7d>
jmp    11b5 <find_base_path+0x75>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_base_path>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1166 <find_base_path+0x26>
movb   $0x0,(%rax)
jmp    1181 <find_base_path+0x41>
mov    %r14,%rdi
call   1060 <search_in_path@plt>
test   %rax,%rax
je     1181 <find_base_path+0x41>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @find_base_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @strdup(i8* %6)
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i8*, i8** %4
store i8 0, i8* %13
br label %24
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i8* @search_in_path(i8* %15)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = call i32 @free(i8* %20)
%22 = load i8*, i8** %4
store i8* %22, i8** %2
br label %26
23: ; preds = %14
br label %24
24: ; preds = %23, %12
%25 = load i8*, i8** %5
store i8* %25, i8** %2
br label %26
26: ; preds = %24, %19
%27 = load i8*, i8** %2
ret i8* %27
}

LLVM IR (-O2):
define i8* @find_base_path(i8* %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %6, label %5
5: ; preds = %1
store i8 0, i8* %3
br label %11
6: ; preds = %1
%7 = tail call i8* @search_in_path(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @free(i8* %2)
br label %11
11: ; preds = %5, %6, %9
%12 = phi i8* [ %7, %9 ], [ %2, %6 ], [ %2, %5 ]
ret i8* %12
}