#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 *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strrchr(char *, char);
int path_dirname(char *path, char *dst, size_t dst_len) {
  char *slash;
  slash = strrchr(path, '/');
  if (slash == NULL) {
    strncpy(dst, ".", dst_len);
  } else {
    slash[0] = '\0';
    strncpy(dst, path, dst_len);
    slash[0] = '/';
  }
  return strlen(dst);
}


x86 assembly (clang, -O0):
<path_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117a <path_dirname+0x4a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe90(%rip),%rsi
call   1030 <strncpy@plt>
jmp    1199 <path_dirname+0x69>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_dirname>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1169 <path_dirname+0x39>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x2f,(%rbx)
jmp    117b <path_dirname+0x4b>
lea    0xe90(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <strlen@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i32 @path_dirname(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** %4
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i32 @strncpy(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 %14)
br label %25
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 0, i8* %18
%19 = load i8*, i8** %5
%20 = load i8*, i8** %4
%21 = load i64, i64* %6
%22 = call i32 @strncpy(i8* %19, i8* %20, i64 %21)
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i64 0
store i8 47, i8* %24
br label %25
25: ; preds = %16, %12
%26 = load i8*, i8** %5
%27 = call i32 @strlen(i8* %26)
ret i32 %27
}

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