#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 *jpath_isarray(char const *);
char *jpath_lastnode(char const *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *jpath_basename(char const *path) {
  char *p1 = jpath_lastnode(path);
  char *p2 = jpath_isarray(path);
  if (!p2 || p1 == p2)
    return p1 ? p1 : ".";
  if (!p1)
    p1 = (char *)path;
  size_t len = p2 - p1;
  char *p = malloc(len);
  if (!p)
    return NULL;
  memcpy(p, p1, len);
  p[len] = '\0';
  return p;
}


x86 assembly (clang, -O0):
<jpath_basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <jpath_lastnode@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <jpath_isarray@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117f <jpath_basename+0x3f>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11b4 <jpath_basename+0x74>
cmpq   $0x0,-0x18(%rbp)
je     1197 <jpath_basename+0x57>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11a7 <jpath_basename+0x67>
lea    0xe62(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11a7 <jpath_basename+0x67>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <jpath_basename+0xe0>
cmpq   $0x0,-0x18(%rbp)
jne    11c7 <jpath_basename+0x87>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fb <jpath_basename+0xbb>
movq   $0x0,-0x8(%rbp)
jmp    1220 <jpath_basename+0xe0>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jpath_basename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <jpath_lastnode@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1030 <jpath_isarray@plt>
test   %rax,%rax
je     1197 <jpath_basename+0x57>
mov    %rax,%rbx
cmp    %rax,%r12
je     1197 <jpath_basename+0x57>
test   %r12,%r12
cmovne %r12,%r14
sub    %r14,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a7 <jpath_basename+0x67>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    11aa <jpath_basename+0x6a>
test   %r12,%r12
lea    0xe5f(%rip),%r15
cmovne %r12,%r15
jmp    11aa <jpath_basename+0x6a>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i8* @jpath_basename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @jpath_lastnode(i8* %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %3
%11 = call i8* @jpath_isarray(i8* %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %26
18: ; preds = %14, %1
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i8*, i8** %4
br label %24
23: ; preds = %18
br label %24
24: ; preds = %23, %21
%25 = phi i8* [ %22, %21 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %23 ]
store i8* %25, i8** %2
br label %51
26: ; preds = %14
%27 = load i8*, i8** %4
%28 = icmp ne i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = load i8*, i8** %3
store i8* %30, i8** %4
br label %31
31: ; preds = %29, %26
%32 = load i8*, i8** %5
%33 = load i8*, i8** %4
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
store i64 %36, i64* %6
%37 = load i64, i64* %6
%38 = call i8* @malloc(i64 %37)
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %42, label %41
41: ; preds = %31
store i8* null, i8** %2
br label %51
42: ; preds = %31
%43 = load i8*, i8** %7
%44 = load i8*, i8** %4
%45 = load i64, i64* %6
%46 = call i32 @memcpy(i8* %43, i8* %44, i64 %45)
%47 = load i8*, i8** %7
%48 = load i64, i64* %6
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %7
store i8* %50, i8** %2
br label %51
51: ; preds = %42, %41, %24
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i8* @jpath_basename(i8* %0) {
%2 = tail call i8* @jpath_lastnode(i8* %0)
%3 = tail call i8* @jpath_isarray(i8* %0)
%4 = icmp eq i8* %3, null
%5 = icmp eq i8* %2, %3
%6 = select i1 %4, i1 true, i1 %5
%7 = icmp eq i8* %2, null
br i1 %6, label %8, label %10
8: ; preds = %1
%9 = select i1 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %2
br label %20
10: ; preds = %1
%11 = select i1 %7, i8* %0, i8* %2
%12 = ptrtoint i8* %3 to i64
%13 = ptrtoint i8* %11 to i64
%14 = sub i64 %12, %13
%15 = tail call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = tail call i32 @memcpy(i8* %15, i8* %11, i64 %14)
%19 = getelementptr inbounds i8, i8* %15, i64 %14
store i8 0, i8* %19
br label %20
20: ; preds = %17, %10, %8
%21 = phi i8* [ %9, %8 ], [ %15, %17 ], [ null, %10 ]
ret i8* %21
}