#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 */
char *rindex(char *, char);
int /*<<< orphan*/ snprintf(char *, size_t, char *, ...);
void do_dirname(const char *fname, char *dname, size_t dname_len) {
  char *ls;
  snprintf(dname, dname_len, "%s", fname);
  ls = rindex(dname, '/');
  if (!ls) {
    snprintf(dname, dname_len, ".");
    return;
  }
  if (ls == dname) {
    return;
  }
  *ls = '\0';
}


x86 assembly (clang, -O0):
<do_dirname>:
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    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xeb9(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <rindex@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1186 <do_dirname+0x66>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    0xe89(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11a0 <do_dirname+0x80>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1199 <do_dirname+0x79>
jmp    11a0 <do_dirname+0x80>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_dirname>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rcx
lea    0xecc(%rip),%rdx
mov    %rsi,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <rindex@plt>
test   %rax,%rax
je     1163 <do_dirname+0x43>
cmp    %rbx,%rax
je     115b <do_dirname+0x3b>
movb   $0x0,(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe99(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <snprintf@plt>

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

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