#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 *str_dup(char const *);
char *strrchr(char *, char);
char *dirpath(const char *filepath) {
  char *p, *str = str_dup(filepath);
  if (NULL == (p = strrchr(str, '/'))) {
    if (NULL == (p = strrchr(str, '\\'))) {
      *str = 0;
      return str;
    }
  }
  *(++p) = 0;
  return str;
}


x86 assembly (clang, -O0):
<dirpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <str_dup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1192 <dirpath+0x72>
mov    -0x20(%rbp),%rdi
mov    $0x5c,%esi
call   1030 <strrchr@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    118d <dirpath+0x6d>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ad <dirpath+0x8d>
jmp    1192 <dirpath+0x72>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,0x1(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirpath>:
push   %rbx
call   1040 <str_dup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
jne    1150 <dirpath+0x30>
mov    %rbx,%rdi
mov    $0x5c,%esi
call   1030 <strrchr@plt>
mov    %rbx,%rcx
test   %rax,%rax
je     1157 <dirpath+0x37>
add    $0x1,%rax
mov    %rax,%rcx
movb   $0x0,(%rcx)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @dirpath(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* @str_dup(i8* %6)
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %4
%10 = icmp eq i8* null, %9
br i1 %10, label %11, label %19
11: ; preds = %1
%12 = load i8*, i8** %5
%13 = call i8* @strrchr(i8* %12, i8 signext 92)
store i8* %13, i8** %4
%14 = icmp eq i8* null, %13
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i8*, i8** %5
store i8 0, i8* %16
%17 = load i8*, i8** %5
store i8* %17, i8** %2
br label %23
18: ; preds = %11
br label %19
19: ; preds = %18, %1
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
store i8 0, i8* %21
%22 = load i8*, i8** %5
store i8* %22, i8** %2
br label %23
23: ; preds = %19, %15
%24 = load i8*, i8** %2
ret i8* %24
}

LLVM IR (-O2):
define i8* @dirpath(i8* %0) {
%2 = tail call i8* @str_dup(i8* %0)
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 92)
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5, %1
%9 = phi i8* [ %6, %5 ], [ %3, %1 ]
%10 = getelementptr inbounds i8, i8* %9, i64 1
br label %11
11: ; preds = %5, %8
%12 = phi i8* [ %10, %8 ], [ %2, %5 ]
store i8 0, i8* %12
ret i8* %2
}