#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 *strrchr(char const *, char);
char *ustrdup(char const *);
char *udirname(const char *f) {
  const char *fin;
  fin = strrchr(f, '/');
  if (fin) {
    char *dup = ustrdup(f);
    dup[fin - f] = '\0';
    return dup;
  } else {
    return ustrdup("./");
  }
}


x86 assembly (clang, -O0):
<udirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1176 <udirname+0x56>
mov    -0x10(%rbp),%rdi
call   1040 <ustrdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1186 <udirname+0x66>
lea    0xe83(%rip),%rdi
call   1040 <ustrdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udirname>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x2f,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     1150 <udirname+0x30>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <ustrdup@plt>
sub    %r14,%rbx
movb   $0x0,(%rax,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xea9(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <ustrdup@plt>

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

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