#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ calloc(int /*<<< orphan*/, int);
int /*<<< orphan*/ free(char *);
char *grab_full_cmd_name_of_pid(int /*<<< orphan*/, int *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
char *strrchr(char *, char);
char *grab_stripped_cmd_name_of_pid(pid_t pid, int *sz) {
  char *fullcmd = grab_full_cmd_name_of_pid(pid, sz), *ret = 0, *cur = 0;
  if (!fullcmd)
    goto out;
  cur = strrchr(fullcmd, '/');
  if (cur) {
    cur++;
  } else {
    cur = fullcmd;
  }
  ret = (char *)calloc(strlen(cur), sizeof(char));
  strcpy(ret, cur);
out:
  if (fullcmd)
    free(fullcmd);
  return ret;
}


x86 assembly (clang, -O0):
<grab_stripped_cmd_name_of_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <grab_full_cmd_name_of_pid@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119f <grab_stripped_cmd_name_of_pid+0x3f>
jmp    11fb <grab_stripped_cmd_name_of_pid+0x9b>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11cd <grab_stripped_cmd_name_of_pid+0x6d>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11d5 <grab_stripped_cmd_name_of_pid+0x75>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
cmpq   $0x0,-0x18(%rbp)
je     120f <grab_stripped_cmd_name_of_pid+0xaf>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grab_stripped_cmd_name_of_pid>:
push   %r15
push   %r14
push   %rbx
call   1080 <grab_full_cmd_name_of_pid@plt>
test   %rax,%rax
je     11b6 <grab_stripped_cmd_name_of_pid+0x56>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %r15,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11b9 <grab_stripped_cmd_name_of_pid+0x59>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @grab_stripped_cmd_name_of_pid(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %3
store i32* %1, i32** %4
%8 = load i32, i32* %3
%9 = load i32*, i32** %4
%10 = call i8* @grab_full_cmd_name_of_pid(i32 %8, i32* %9)
store i8* %10, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
br label %32
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = call i8* @strrchr(i8* %15, i8 signext 47)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %7
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %7
br label %24
22: ; preds = %14
%23 = load i8*, i8** %5
store i8* %23, i8** %7
br label %24
24: ; preds = %22, %19
%25 = load i8*, i8** %7
%26 = call i32 @strlen(i8* %25)
%27 = call i64 @calloc(i32 %26, i32 1)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = load i8*, i8** %7
%31 = call i32 @strcpy(i8* %29, i8* %30)
br label %32
32: ; preds = %24, %13
%33 = load i8*, i8** %5
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i8*, i8** %5
%37 = call i32 @free(i8* %36)
br label %38
38: ; preds = %35, %32
%39 = load i8*, i8** %6
ret i8* %39
}

LLVM IR (-O2):
define i8* @grab_stripped_cmd_name_of_pid(i32 %0, i32* %1) {
%3 = tail call i8* @grab_full_cmd_name_of_pid(i32 %0, i32* %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %15, label %5
5: ; preds = %2
%6 = tail call i8* @strrchr(i8* %3, i8 signext 47)
%7 = icmp eq i8* %6, null
%8 = getelementptr inbounds i8, i8* %6, i64 1
%9 = select i1 %7, i8* %3, i8* %8
%10 = tail call i32 @strlen(i8* %9)
%11 = tail call i64 @calloc(i32 %10, i32 1)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @strcpy(i8* %12, i8* %9)
%14 = tail call i32 @free(i8* %3)
br label %15
15: ; preds = %2, %5
%16 = phi i8* [ %12, %5 ], [ null, %2 ]
ret i8* %16
}