#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 */
char *malloc(int);
char *strrchr(char const *, char);
char *substring(char const *, int /*<<< orphan*/, int);
char *restrict fs_basepath(const char *restrict filepath) {
  int basepath_length;
  char *restrict last_char = strrchr(filepath, '/');
  char *restrict basepath;
  if (last_char > 0) {
    basepath_length = last_char - filepath;
    basepath = substring(filepath, 0, basepath_length);
  } else {
    basepath = malloc(2);
    *basepath = '.';
    *(basepath) = '\n';
  }
  return basepath;
}


x86 assembly (clang, -O0):
<fs_basepath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    117e <fs_basepath+0x4e>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <substring@plt>
mov    %rax,-0x20(%rbp)
jmp    119a <fs_basepath+0x6a>
mov    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x2e,(%rax)
mov    -0x20(%rbp),%rax
movb   $0xa,(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_basepath>:
push   %rbx
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     1152 <fs_basepath+0x22>
sub    %ebx,%eax
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
pop    %rbx
jmp    1040 <substring@plt>
mov    $0x2,%edi
call   1050 <malloc@plt>
movb   $0xa,(%rax)
pop    %rbx
ret

LLVM IR (-O0):
define i8* @fs_basepath(i8* noalias %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @strrchr(i8* %6, i8 signext 47)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ugt i8* %8, null
br i1 %9, label %10, label %20
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = load i8*, i8** %2
%13 = ptrtoint i8* %11 to i64
%14 = ptrtoint i8* %12 to i64
%15 = sub i64 %13, %14
%16 = trunc i64 %15 to i32
store i32 %16, i32* %3
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = call i8* @substring(i8* %17, i32 0, i32 %18)
store i8* %19, i8** %5
br label %24
20: ; preds = %1
%21 = call i8* @malloc(i32 2)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
store i8 46, i8* %22
%23 = load i8*, i8** %5
store i8 10, i8* %23
br label %24
24: ; preds = %20, %10
%25 = load i8*, i8** %5
ret i8* %25
}

LLVM IR (-O2):
define i8* @fs_basepath(i8* noalias %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %10, label %4
4: ; preds = %1
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
%8 = trunc i64 %7 to i32
%9 = tail call i8* @substring(i8* %0, i32 0, i32 %8)
br label %12
10: ; preds = %1
%11 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
store i8 10, i8* %11
br label %12
12: ; preds = %10, %4
%13 = phi i8* [ %9, %4 ], [ %11, %10 ]
ret i8* %13
}