#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 strlen(char const *);
char *strrchr(char const *, char);
char *xmemdup(char const *, int);
char *xstrdup(char *);
char *os_GetBasename(char const *path) {
  char const *dp, *sp;
  int l;
  if (!path)
    return xstrdup("");
  l = strlen(path);
  sp = strrchr(path, '/');
  dp = strrchr(path, '.');
  if (!sp)
    sp = path;
  else
    sp++;
  if (!dp || sp >= dp)
    dp = path + l;
  return xmemdup(sp, dp - sp);
}


x86 assembly (clang, -O0):
<os_GetBasename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <os_GetBasename+0x2c>
lea    0xea2(%rip),%rdi
call   1050 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1202 <os_GetBasename+0xc2>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b4 <os_GetBasename+0x74>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11c0 <os_GetBasename+0x80>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d9 <os_GetBasename+0x99>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11e8 <os_GetBasename+0xa8>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1060 <xmemdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_GetBasename>:
test   %rdi,%rdi
je     1192 <os_GetBasename+0x52>
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
lea    0x1(%rbp),%rdi
test   %rbp,%rbp
cmove  %rbx,%rdi
add    %r14d,%ebx
cmp    %rax,%rdi
cmovb  %eax,%ebx
sub    %edi,%ebx
mov    %ebx,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <xmemdup@plt>
lea    0xe67(%rip),%rdi
jmp    1050 <xstrdup@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @os_GetBasename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = call i8* @xstrdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %2
br label %46
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %6
%14 = load i8*, i8** %3
%15 = call i8* @strrchr(i8* %14, i8 signext 47)
store i8* %15, i8** %5
%16 = load i8*, i8** %3
%17 = call i8* @strrchr(i8* %16, i8 signext 46)
store i8* %17, i8** %4
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %11
%21 = load i8*, i8** %3
store i8* %21, i8** %5
br label %25
22: ; preds = %11
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
br label %25
25: ; preds = %22, %20
%26 = load i8*, i8** %4
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i8*, i8** %5
%30 = load i8*, i8** %4
%31 = icmp uge i8* %29, %30
br i1 %31, label %32, label %37
32: ; preds = %28, %25
%33 = load i8*, i8** %3
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8* %36, i8** %4
br label %37
37: ; preds = %32, %28
%38 = load i8*, i8** %5
%39 = load i8*, i8** %4
%40 = load i8*, i8** %5
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = call i8* @xmemdup(i8* %38, i32 %44)
store i8* %45, i8** %2
br label %46
46: ; preds = %37, %9
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @os_GetBasename(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i8* @xstrdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %21
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
%7 = tail call i8* @strrchr(i8* %0, i8 signext 47)
%8 = tail call i8* @strrchr(i8* %0, i8 signext 46)
%9 = icmp eq i8* %7, null
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = select i1 %9, i8* %0, i8* %10
%12 = icmp ult i8* %11, %8
%13 = sext i32 %6 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = select i1 %12, i8* %8, i8* %14
%16 = ptrtoint i8* %15 to i64
%17 = ptrtoint i8* %11 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = tail call i8* @xmemdup(i8* %11, i32 %19)
br label %21
21: ; preds = %5, %3
%22 = phi i8* [ %20, %5 ], [ %4, %3 ]
ret i8* %22
}