#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*/ strncpy(char *, char const *, size_t);
char *strrchr(char *, char);
int al_get_dir_name(char *dest, size_t len, const char *path, int count) {
  char buf[256];
  char *occ;
  strncpy(buf, path, 256);
  buf[255] = '\0';
  for (int i = 0; i <= count; i++) {
    occ = strrchr(buf, '/');
    if (occ == NULL) {
      *dest = '\0';
      return -1;
    }
    *occ = '\0';
  }
  strncpy(dest, occ + 1, len);
  dest[len - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<al_get_dir_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x31(%rbp)
movl   $0x0,-0x13c(%rbp)
mov    -0x13c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     11c3 <al_get_dir_name+0xa3>
lea    -0x130(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    11a5 <al_get_dir_name+0x85>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <al_get_dir_name+0xd2>
mov    -0x138(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x13c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x13c(%rbp)
jmp    115d <al_get_dir_name+0x3d>
mov    -0x10(%rbp),%rdi
mov    -0x138(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<al_get_dir_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %ecx,%ebp
mov    %rdx,%rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rbx
mov    $0x100,%edx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <strncpy@plt>
movb   $0x0,0xff(%rsp)
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1197 <al_get_dir_name+0x77>
movb   $0x0,(%rax)
add    $0xffffffff,%ebp
jne    1160 <al_get_dir_name+0x40>
add    $0x1,%rax
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
add    %r14,%r15
add    $0xffffffffffffffff,%r15
xor    %eax,%eax
jmp    119c <al_get_dir_name+0x7c>
mov    $0xffffffff,%eax
movb   $0x0,(%r15)
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @al_get_dir_name(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%14 = load i8*, i8** %8
%15 = call i32 @strncpy(i8* %13, i8* %14, i64 256)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 255
store i8 0, i8* %16
store i32 0, i32* %12
br label %17
17: ; preds = %30, %4
%18 = load i32, i32* %12
%19 = load i32, i32* %9
%20 = icmp sle i32 %18, %19
br i1 %20, label %21, label %33
21: ; preds = %17
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%23 = call i8* @strrchr(i8* %22, i8 signext 47)
store i8* %23, i8** %11
%24 = load i8*, i8** %11
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i8*, i8** %6
store i8 0, i8* %27
store i32 -1, i32* %5
br label %43
28: ; preds = %21
%29 = load i8*, i8** %11
store i8 0, i8* %29
br label %30
30: ; preds = %28
%31 = load i32, i32* %12
%32 = add nsw i32 %31, 1
store i32 %32, i32* %12
br label %17
33: ; preds = %17
%34 = load i8*, i8** %6
%35 = load i8*, i8** %11
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = load i64, i64* %7
%38 = call i32 @strncpy(i8* %34, i8* %36, i64 %37)
%39 = load i8*, i8** %6
%40 = load i64, i64* %7
%41 = sub i64 %40, 1
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
store i32 0, i32* %5
br label %43
43: ; preds = %33, %26
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
define i32 @al_get_dir_name(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%7 = call i32 @strncpy(i8* %6, i8* %2, i64 256)
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 255
store i8 0, i8* %8
%9 = icmp sgt i32 %3, -1
br label %10
10: ; preds = %4, %14
%11 = phi i32 [ %15, %14 ], [ 0, %4 ]
%12 = call i8* @strrchr(i8* %6, i8 signext 47)
%13 = icmp eq i8* %12, null
br i1 %13, label %22, label %14
14: ; preds = %10
store i8 0, i8* %12
%15 = add nuw i32 %11, 1
%16 = icmp eq i32 %11, %3
br i1 %16, label %17, label %10
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %12, i64 1
%19 = call i32 @strncpy(i8* %0, i8* %18, i64 %1)
%20 = add i64 %1, -1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
br label %22
22: ; preds = %10, %17
%23 = phi i8* [ %21, %17 ], [ %0, %10 ]
%24 = phi i32 [ 0, %17 ], [ -1, %10 ]
store i8 0, i8* %23
ret i32 %24
}