#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strchr(char const *, char);
char *basePath(const char *path) {
  char *pos = strchr(path, '\\');
  char *last = NULL;
  while (pos != NULL) {
    last = pos;
    pos++;
    pos = strchr(pos, '\\');
  }
  if (last == NULL) {
    return (char *)path;
  } else {
    int len = (int)(last - path);
    char *str = (char *)malloc(len + 1);
    if (len > 0) {
      memcpy(str, path, len);
    }
    str[len] = '\0';
    return str;
  }
}


x86 assembly (clang, -O0):
<basePath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x5c,%esi
call   1030 <strchr@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118c <basePath+0x5c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x5c,%esi
call   1030 <strchr@plt>
mov    %rax,-0x18(%rbp)
jmp    1156 <basePath+0x26>
cmpq   $0x0,-0x20(%rbp)
jne    11a4 <basePath+0x74>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ef <basePath+0xbf>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11db <basePath+0xab>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basePath>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x5c,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1190 <basePath+0x60>
nopw   0x0(%rax,%rax,1)
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    $0x5c,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1150 <basePath+0x20>
sub    %r14,%rbx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    1185 <basePath+0x55>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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