#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__ calloc(int, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char const *, char);
char *parse_app_name(const char *uri) {
  if (uri == NULL) {
    return "unknown";
  }
  char *slash = strrchr(uri, '/');
  if (slash == NULL || slash == uri) {
    return "unknown";
  }
  char *begin = slash;
  while ((begin != uri) && (*--begin != '/'))
    ;
  begin++;
  char *result = (char *)calloc(1, slash - begin);
  strncpy(result, begin, slash - begin);
  return result;
}


x86 assembly (clang, -O0):
<parse_app_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1157 <parse_app_name+0x27>
lea    0xeb2(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1227 <parse_app_name+0xf7>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1182 <parse_app_name+0x52>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1192 <parse_app_name+0x62>
lea    0xe77(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1227 <parse_app_name+0xf7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x29(%rbp)
je     11c9 <parse_app_name+0x99>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movsbl -0x1(%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11d9 <parse_app_name+0xa9>
jmp    11de <parse_app_name+0xae>
jmp    119a <parse_app_name+0x6a>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_app_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xec1(%rip),%r15
test   %rdi,%rdi
je     11ac <parse_app_name+0x7c>
mov    %rdi,%r12
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     11ac <parse_app_name+0x7c>
mov    %rax,%r14
cmp    %r12,%rax
je     11ac <parse_app_name+0x7c>
mov    %r14,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %r12,%rax
je     1184 <parse_app_name+0x54>
lea    -0x1(%rax),%rbx
cmpb   $0x2f,-0x1(%rax)
mov    %rbx,%rax
jne    1170 <parse_app_name+0x40>
jmp    1187 <parse_app_name+0x57>
mov    %r12,%rbx
add    $0x1,%rbx
sub    %ebx,%r14d
mov    $0x1,%edi
mov    %r14d,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"unknown\00"
define i8* @parse_app_name(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = icmp eq i8* %0, null
br i1 %3, label %29, label %4
4: ; preds = %1
%5 = tail call i8* @strrchr(i8* %0, i8 signext 47)
%6 = ptrtoint i8* %5 to i64
%7 = icmp eq i8* %5, null
%8 = icmp eq i8* %5, %0
%9 = or i1 %7, %8
br i1 %9, label %29, label %10
10: ; preds = %4
%11 = sub i64 %2, %6
%12 = getelementptr i8, i8* %5, i64 %11
br label %13
13: ; preds = %10, %16
%14 = phi i8* [ %17, %16 ], [ %5, %10 ]
%15 = icmp eq i8* %14, %0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = getelementptr inbounds i8, i8* %14, i64 -1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 47
br i1 %19, label %20, label %13
20: ; preds = %13, %16
%21 = phi i8* [ %17, %16 ], [ %12, %13 ]
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = ptrtoint i8* %22 to i64
%24 = sub i64 %6, %23
%25 = trunc i64 %24 to i32
%26 = tail call i64 @calloc(i32 1, i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = tail call i32 @strncpy(i8* %27, i8* %22, i32 %25)
br label %29
29: ; preds = %20, %4, %1
%30 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %1 ], [ %27, %20 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %4 ]
ret i8* %30
}