#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*/ is_root_dir(char const *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, ...);
char *strrchr(char const *, char);
int apply_h_mod(const char *path, char *buf, size_t buf_len) {
  char *p = strrchr(path, '/');
  if (p == NULL) {
    snprintf(buf, buf_len, ".");
  } else {
    snprintf(buf, buf_len, "%s", path);
    if (!is_root_dir(path)) {
      buf[p - path + 1] = '\0';
      if (!is_root_dir(buf))
        buf[p - path] = '\0';
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<apply_h_mod>:
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   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117c <apply_h_mod+0x4c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    0xe90(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11eb <apply_h_mod+0xbb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe73(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <is_root_dir@plt>
cmp    $0x0,%eax
jne    11e6 <apply_h_mod+0xb6>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1050 <is_root_dir@plt>
cmp    $0x0,%eax
jne    11e1 <apply_h_mod+0xb1>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11e6 <apply_h_mod+0xb6>
jmp    11eb <apply_h_mod+0xbb>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apply_h_mod>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1192 <apply_h_mod+0x62>
mov    %rax,%r15
lea    0xea8(%rip),%rdx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <is_root_dir@plt>
test   %eax,%eax
jne    11a6 <apply_h_mod+0x76>
sub    %rbx,%r15
movb   $0x0,0x1(%r15,%r14,1)
mov    %r14,%rdi
call   1050 <is_root_dir@plt>
test   %eax,%eax
jne    11a6 <apply_h_mod+0x76>
movb   $0x0,(%r14,%r15,1)
jmp    11a6 <apply_h_mod+0x76>
lea    0xe67(%rip),%rdx
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1030 <snprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @apply_h_mod(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 (i8*, i64, i8*, ...) @snprintf(i8* %13, i64 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %46
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = load i64, i64* %6
%19 = load i8*, i8** %4
%20 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %17, i64 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %4
%22 = call i32 @is_root_dir(i8* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %45, label %24
24: ; preds = %16
%25 = load i8*, i8** %5
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = add nsw i64 %30, 1
%32 = getelementptr inbounds i8, i8* %25, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %5
%34 = call i32 @is_root_dir(i8* %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %44, label %36
36: ; preds = %24
%37 = load i8*, i8** %5
%38 = load i8*, i8** %7
%39 = load i8*, i8** %4
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = getelementptr inbounds i8, i8* %37, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %36, %24
br label %45
45: ; preds = %44, %16
br label %46
46: ; preds = %45, %12
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @apply_h_mod(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 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %22
8: ; preds = %3
%9 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%10 = tail call i32 @is_root_dir(i8* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = ptrtoint i8* %4 to i64
%14 = ptrtoint i8* %0 to i64
%15 = sub i64 %13, %14
%16 = add nsw i64 %15, 1
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 0, i8* %17
%18 = tail call i32 @is_root_dir(i8* %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %21
br label %22
22: ; preds = %8, %20, %12, %6
ret i32 0
}