#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 */
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, scalar_t__);
void basename_r(const char *path, char *res) {
  char *p;
  strncpy(res, path, strlen(path) + 1);
  p = res + strlen(res) - 1;
  while (p >= res) {
    if (*p == '/') {
      *p = '\0';
      break;
    }
    p--;
  }
  if (strlen(res) == 0) {
    res[0] = '/';
    res[1] = '\0';
  }
}


x86 assembly (clang, -O0):
<basename_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     11bb <basename_r+0x9b>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11aa <basename_r+0x8a>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11bb <basename_r+0x9b>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1180 <basename_r+0x60>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    11dd <basename_r+0xbd>
mov    -0x10(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basename_r>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
nopl   (%rax)
cmp    %rbx,%rax
jb     1165 <basename_r+0x45>
lea    -0x1(%rax),%rcx
cmpb   $0x2f,(%rax)
mov    %rcx,%rax
jne    1150 <basename_r+0x30>
movb   $0x0,0x1(%rcx)
cmpb   $0x0,(%rbx)
jne    116f <basename_r+0x4f>
movw   $0x2f,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @basename_r(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
%10 = add nsw i64 %9, 1
%11 = call i32 @strncpy(i8* %6, i8* %7, i64 %10)
%12 = load i8*, i8** %4
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = getelementptr inbounds i8, i8* %15, i64 -1
store i8* %16, i8** %5
br label %17
17: ; preds = %28, %2
%18 = load i8*, i8** %5
%19 = load i8*, i8** %4
%20 = icmp uge i8* %18, %19
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 47
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i8*, i8** %5
store i8 0, i8* %27
br label %31
28: ; preds = %21
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %5
br label %17
31: ; preds = %26, %17
%32 = load i8*, i8** %4
%33 = call i64 @strlen(i8* %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 47, i8* %37
%38 = load i8*, i8** %4
%39 = getelementptr inbounds i8, i8* %38, i64 1
store i8 0, i8* %39
br label %40
40: ; preds = %35, %31
ret void
}

LLVM IR (-O2):
define void @basename_r(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = add nsw i64 %3, 1
%5 = tail call i32 @strncpy(i8* %1, i8* %0, i64 %4)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = getelementptr inbounds i8, i8* %1, i64 %6
br label %8
8: ; preds = %12, %2
%9 = phi i8* [ %7, %2 ], [ %10, %12 ]
%10 = getelementptr inbounds i8, i8* %9, i64 -1
%11 = icmp ult i8* %10, %1
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = load i8, i8* %10
%14 = icmp eq i8 %13, 47
br i1 %14, label %15, label %8
15: ; preds = %12
store i8 0, i8* %10
br label %16
16: ; preds = %8, %15
%17 = load i8, i8* %1
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
store i8 47, i8* %1
%20 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %20
br label %21
21: ; preds = %19, %16
ret void
}