#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*/ chop(char *, char);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
void basedir(const char *path, char *output, size_t size) {
  char *p;
  strncpy(output, path, size);
  chop(output, '/');
  p = strrchr(output, '/');
  if (!p) {
    strncpy(output, ".", 1);
    goto out;
  }
  *p = '\0';
  if (!*output) {
    strncpy(output, "/", 1);
  }
out:
  return;
}


x86 assembly (clang, -O0):
<basedir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <chop@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <basedir+0x6c>
mov    -0x10(%rbp),%rdi
lea    0xe73(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncpy@plt>
jmp    11ca <basedir+0x9a>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11c5 <basedir+0x95>
mov    -0x10(%rbp),%rdi
lea    0xe47(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncpy@plt>
jmp    11ca <basedir+0x9a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basedir>:
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rsi
mov    %rbx,%rdi
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <chop@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1168 <basedir+0x38>
movb   $0x0,(%rax)
cmpb   $0x0,(%rbx)
je     1171 <basedir+0x41>
pop    %rbx
ret
lea    0xe91(%rip),%rsi
jmp    1178 <basedir+0x48>
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
pop    %rbx
jmp    1030 <strncpy@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define void @basedir(i8* %0, i8* %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = tail call i32 @strncpy(i8* %1, i8* %0, i32 %4)
%6 = tail call i32 @chop(i8* %1, i8 signext 47)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @strncpy(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %16
11: ; preds = %3
store i8 0, i8* %7
%12 = load i8, i8* %1
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @strncpy(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %16
16: ; preds = %11, %14, %9
ret void
}