#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 */
size_t strlen(char const *);
char *strpbrk(char const *, char *);
char *optk_get_basename(const char *path, size_t *basename_len) {
  const char *basename, *ptr;
  if (path == NULL)
    return NULL;
  basename = ptr = path;
  for (;;) {
    ptr = strpbrk(basename, "/");
    if (ptr == NULL)
      break;
    basename = ptr + 1;
  }
  if (basename_len != NULL)
    *basename_len = strlen(basename);
  return (char *)basename;
}


x86 assembly (clang, -O0):
<optk_get_basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1148 <optk_get_basename+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11af <optk_get_basename+0x8f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1040 <strpbrk@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1178 <optk_get_basename+0x58>
jmp    1189 <optk_get_basename+0x69>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1154 <optk_get_basename+0x34>
cmpq   $0x0,-0x18(%rbp)
je     11a7 <optk_get_basename+0x87>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optk_get_basename>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1158 <optk_get_basename+0x38>
mov    %rsi,%r14
nopl   0x0(%rax)
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
jne    1130 <optk_get_basename+0x10>
test   %r14,%r14
je     115a <optk_get_basename+0x3a>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,(%r14)
jmp    115a <optk_get_basename+0x3a>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i8* @optk_get_basename(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %31
11: ; preds = %2
%12 = load i8*, i8** %4
store i8* %12, i8** %7
store i8* %12, i8** %6
br label %13
13: ; preds = %19, %11
%14 = load i8*, i8** %6
%15 = call i8* @strpbrk(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %13
br label %22
19: ; preds = %13
%20 = load i8*, i8** %7
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8* %21, i8** %6
br label %13
22: ; preds = %18
%23 = load i64*, i64** %5
%24 = icmp ne i64* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = call i64 @strlen(i8* %26)
%28 = load i64*, i64** %5
store i64 %27, i64* %28
br label %29
29: ; preds = %25, %22
%30 = load i8*, i8** %6
store i8* %30, i8** %3
br label %31
31: ; preds = %29, %10
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
define i8* @optk_get_basename(i8* %0, i64* writeonly %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %13, label %4
4: ; preds = %2, %4
%5 = phi i8* [ %8, %4 ], [ %0, %2 ]
%6 = tail call i8* @strchr(i8* %5, i32 47)
%7 = icmp eq i8* %6, null
%8 = getelementptr inbounds i8, i8* %6, i64 1
br i1 %7, label %9, label %4
9: ; preds = %4
%10 = icmp eq i64* %1, null
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = tail call i64 @strlen(i8* %5)
store i64 %12, i64* %1
br label %13
13: ; preds = %9, %11, %2
%14 = phi i8* [ null, %2 ], [ %5, %11 ], [ %5, %9 ]
ret i8* %14
}