#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*/ memcpy(char *, char *, int);
char *strchr(char *, char);
void all_but_last_part(char *pathname, char *rest) {
  int len;
  char *p = pathname;
  char *p2 = strchr(p, '/');
  if (p2 == NULL) {
    rest[0] = '\0';
    return;
  }
  while (p2 != NULL) {
    p = p2 + 1;
    p2 = strchr(p, '/');
  }
  len = p - pathname - 1;
  memcpy(rest, pathname, len);
  rest[len] = '\0';
  return;
}


x86 assembly (clang, -O0):
<all_but_last_part>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1161 <all_but_last_part+0x41>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11c2 <all_but_last_part+0xa2>
jmp    1166 <all_but_last_part+0x46>
cmpq   $0x0,-0x28(%rbp)
je     1194 <all_but_last_part+0x74>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
jmp    1166 <all_but_last_part+0x46>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<all_but_last_part>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2f,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1175 <all_but_last_part+0x55>
nopw   0x0(%rax,%rax,1)
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1140 <all_but_last_part+0x20>
mov    %r15,%rax
not    %rax
add    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movslq %ebx,%rax
add    %rax,%r14
movb   $0x0,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @all_but_last_part(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = call i8* @strchr(i8* %9, i8 signext 47)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 0, i8* %15
br label %41
16: ; preds = %2
br label %17
17: ; preds = %20, %16
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = call i8* @strchr(i8* %23, i8 signext 47)
store i8* %24, i8** %7
br label %17
25: ; preds = %17
%26 = load i8*, i8** %6
%27 = load i8*, i8** %3
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = sub nsw i64 %30, 1
%32 = trunc i64 %31 to i32
store i32 %32, i32* %5
%33 = load i8*, i8** %4
%34 = load i8*, i8** %3
%35 = load i32, i32* %5
%36 = call i32 @memcpy(i8* %33, i8* %34, i32 %35)
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %25, %13
ret void
}

LLVM IR (-O2):
define void @all_but_last_part(i8* %0, i8* %1) {
%3 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %20, label %5
5: ; preds = %2, %5
%6 = phi i8* [ %8, %5 ], [ %3, %2 ]
%7 = getelementptr inbounds i8, i8* %6, i64 1
%8 = tail call i8* @strchr(i8* %7, i8 signext 47)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %5
10: ; preds = %5
%11 = ptrtoint i8* %7 to i64
%12 = ptrtoint i8* %0 to i64
%13 = xor i64 %12, -1
%14 = add i64 %11, %13
%15 = trunc i64 %14 to i32
%16 = tail call i32 @memcpy(i8* %1, i8* %0, i32 %15)
%17 = shl i64 %14, 32
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds i8, i8* %1, i64 %18
br label %20
20: ; preds = %2, %10
%21 = phi i8* [ %19, %10 ], [ %1, %2 ]
store i8 0, i8* %21
ret void
}