#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
int split_path(char **rel_path, char **name) {
  int res = 0;
  int name_n, path_n;
  char *name_part, *path_part;
  char *slash = strrchr(*rel_path, '/');
  if (slash == NULL) {
    name_part = *rel_path;
    name_n = strlen(name_part);
    path_part = "";
    path_n = 0;
  } else {
    name_part = &slash[1];
    name_n = strlen(name_part);
    path_part = *rel_path;
    path_n = strlen(path_part) - (name_n + 1);
  }
  *name = malloc(name_n + 1);
  if (*name != NULL) {
    strncpy(*name, name_part, name_n);
    (*name)[name_n] = 0;
    char *new_path = malloc(path_n + 1);
    if (new_path != NULL) {
      strncpy(new_path, *rel_path, path_n);
      new_path[path_n] = 0;
      *rel_path = new_path;
      res = 1;
    } else {
      free(*name);
      fprintf(stderr, "utils: failed to allocate path\n");
    }
  } else {
    fprintf(stderr, "utils: failed to allocate name\n");
  }
  return res;
}


x86 assembly (clang, -O0):
<split_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c5 <split_path+0x65>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    11fc <split_path+0x9c>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     12b7 <split_path+0x157>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1291 <split_path+0x131>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x14(%rbp)
jmp    12b2 <split_path+0x152>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xd53(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12cc <split_path+0x16c>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    0xd59(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%r13
mov    %r13,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11ac <split_path+0x4c>
mov    %rax,%rbp
add    $0x1,%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    (%r15),%rdi
call   1050 <strlen@plt>
mov    %r12d,%ebx
not    %ebx
add    %eax,%ebx
mov    %rbp,%r13
jmp    11b9 <split_path+0x59>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
xor    %ebx,%ebx
lea    0x1(%r12),%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1212 <split_path+0xb2>
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1040 <strncpy@plt>
mov    (%r14),%rax
movslq %r12d,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0x1(%rbx),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1224 <split_path+0xc4>
mov    %rax,%rbp
mov    (%r15),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
call   1040 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,(%r15)
mov    $0x1,%eax
jmp    1243 <split_path+0xe3>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
jmp    123c <split_path+0xdc>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [32 x i8] c"utils: failed to allocate path\0A\00"
@.str.2 = [32 x i8] c"utils: failed to allocate name\0A\00"
define i32 @split_path(i8** %0, i8** %1) {
%3 = alloca i8**
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8** %0, i8*** %3
store i8** %1, i8*** %4
store i32 0, i32* %5
%12 = load i8**, i8*** %3
%13 = load i8*, i8** %12
%14 = call i8* @strrchr(i8* %13, i8 signext 47)
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %3
%19 = load i8*, i8** %18
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %6
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %9
store i32 0, i32* %7
br label %34
22: ; preds = %2
%23 = load i8*, i8** %10
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = call i32 @strlen(i8* %25)
store i32 %26, i32* %6
%27 = load i8**, i8*** %3
%28 = load i8*, i8** %27
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = call i32 @strlen(i8* %29)
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
%33 = sub nsw i32 %30, %32
store i32 %33, i32* %7
br label %34
34: ; preds = %22, %17
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
%37 = call i8* @malloc(i32 %36)
%38 = load i8**, i8*** %4
store i8* %37, i8** %38
%39 = load i8**, i8*** %4
%40 = load i8*, i8** %39
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %77
42: ; preds = %34
%43 = load i8**, i8*** %4
%44 = load i8*, i8** %43
%45 = load i8*, i8** %8
%46 = load i32, i32* %6
%47 = call i32 @strncpy(i8* %44, i8* %45, i32 %46)
%48 = load i8**, i8*** %4
%49 = load i8*, i8** %48
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
%55 = call i8* @malloc(i32 %54)
store i8* %55, i8** %11
%56 = load i8*, i8** %11
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %70
58: ; preds = %42
%59 = load i8*, i8** %11
%60 = load i8**, i8*** %3
%61 = load i8*, i8** %60
%62 = load i32, i32* %7
%63 = call i32 @strncpy(i8* %59, i8* %61, i32 %62)
%64 = load i8*, i8** %11
%65 = load i32, i32* %7
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 0, i8* %67
%68 = load i8*, i8** %11
%69 = load i8**, i8*** %3
store i8* %68, i8** %69
store i32 1, i32* %5
br label %76
70: ; preds = %42
%71 = load i8**, i8*** %4
%72 = load i8*, i8** %71
%73 = call i32 @free(i8* %72)
%74 = load i32, i32* @stderr
%75 = call i32 @fprintf(i32 %74, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %76
76: ; preds = %70, %58
br label %80
77: ; preds = %34
%78 = load i32, i32* @stderr
%79 = call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %80
80: ; preds = %77, %76
%81 = load i32, i32* %5
ret i32 %81
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [32 x i8] c"utils: failed to allocate path\0A\00"
@.str.2 = [32 x i8] c"utils: failed to allocate name\0A\00"
define i32 @split_path(i8** nocapture %0, i8** nocapture %1) {
%3 = load i8*, i8** %0
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %3, i8 signext 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %3)
br label %15
8: ; preds = %2
%9 = getelementptr inbounds i8, i8* %4, i64 1
%10 = tail call i32 @strlen(i8* %9)
%11 = load i8*, i8** %0
%12 = tail call i32 @strlen(i8* %11)
%13 = xor i32 %10, -1
%14 = add i32 %12, %13
br label %15
15: ; preds = %8, %6
%16 = phi i32 [ 0, %6 ], [ %14, %8 ]
%17 = phi i8* [ %3, %6 ], [ %9, %8 ]
%18 = phi i32 [ %7, %6 ], [ %10, %8 ]
%19 = add nsw i32 %18, 1
%20 = tail call i8* @malloc(i32 %19)
store i8* %20, i8** %1
%21 = icmp eq i8* %20, null
br i1 %21, label %40, label %22
22: ; preds = %15
%23 = tail call i32 @strncpy(i8* %20, i8* %17, i32 %18)
%24 = load i8*, i8** %1
%25 = sext i32 %18 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 0, i8* %26
%27 = add nsw i32 %16, 1
%28 = tail call i8* @malloc(i32 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %35, label %30
30: ; preds = %22
%31 = load i8*, i8** %0
%32 = tail call i32 @strncpy(i8* %28, i8* %31, i32 %16)
%33 = sext i32 %16 to i64
%34 = getelementptr inbounds i8, i8* %28, i64 %33
store i8 0, i8* %34
store i8* %28, i8** %0
br label %43
35: ; preds = %22
%36 = load i8*, i8** %1
%37 = tail call i32 @free(i8* %36)
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %43
40: ; preds = %15
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %30, %35, %40
%44 = phi i32 [ 0, %40 ], [ 1, %30 ], [ 0, %35 ]
ret i32 %44
}