#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 *calloc(int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *);
char *strrchr(char *, char);
char *get_path(char *s) {
  char *tmp;
  char *buf = NULL;
  int len = 0;
  tmp = strrchr(s, '/');
  if (tmp == NULL)
    return NULL;
  buf = calloc(1, 256);
  if (buf == NULL)
    return NULL;
  if (tmp == s) {
    sprintf(buf, "/");
  } else {
    len = tmp - s + 1;
    if (len > 256)
      len = 256;
  }
  snprintf(buf, len, "%s", s);
  return buf;
}


x86 assembly (clang, -O0):
<get_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1185 <get_path+0x45>
movq   $0x0,-0x8(%rbp)
jmp    121d <get_path+0xdd>
mov    $0x1,%edi
mov    $0x100,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b0 <get_path+0x70>
movq   $0x0,-0x8(%rbp)
jmp    121d <get_path+0xdd>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11d3 <get_path+0x93>
mov    -0x20(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1060 <sprintf@plt>
jmp    11fe <get_path+0xbe>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x100,-0x24(%rbp)
jle    11f9 <get_path+0xb9>
movl   $0x100,-0x24(%rbp)
jmp    11fe <get_path+0xbe>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xdf2(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_path>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     117c <get_path+0x4c>
mov    %rax,%rbx
mov    $0x1,%edi
mov    $0x100,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     117c <get_path+0x4c>
mov    %rax,%r15
cmp    %r14,%rbx
je     1181 <get_path+0x51>
sub    %r14d,%ebx
add    $0x1,%ebx
cmp    $0x100,%ebx
mov    $0x100,%esi
cmovl  %ebx,%esi
jmp    1189 <get_path+0x59>
xor    %r15d,%r15d
jmp    119b <get_path+0x6b>
movw   $0x2f,(%r15)
xor    %esi,%esi
lea    0xe70(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%s\00"
define i8* @get_path(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = tail call dereferenceable_or_null(256) i8* @calloc(i32 1, i32 256)
%6 = icmp eq i8* %5, null
br i1 %6, label %22, label %7
7: ; preds = %4
%8 = icmp eq i8* %2, %0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = bitcast i8* %5 to i16*
store i16 47, i16* %10
br label %19
11: ; preds = %7
%12 = ptrtoint i8* %2 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = add i32 %15, 1
%17 = icmp slt i32 %16, 256
%18 = select i1 %17, i32 %16, i32 256
br label %19
19: ; preds = %11, %9
%20 = phi i32 [ 0, %9 ], [ %18, %11 ]
%21 = tail call i32 @snprintf(i8* %5, i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %4, %1, %19
%23 = phi i8* [ %5, %19 ], [ null, %1 ], [ null, %4 ]
ret i8* %23
}