#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 */
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *strrchr(char *, char);
char *get_pkgname(char *pkgname) {
  int i;
  char *name = malloc(sizeof(char) * 512);
  memset(name, 0, 512);
  if (!pkgname) {
    char *cwd = getcwd(NULL, 0);
    pkgname = strrchr(cwd, '/');
    pkgname++;
    free(cwd);
  }
  for (i = 0; i < 512; ++i) {
    if (pkgname[i] == '-' || pkgname[i] == '.')
      break;
    else
      name[i] = pkgname[i];
  }
  return name;
}


x86 assembly (clang, -O0):
<get_pkgname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x200,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11bb <get_pkgname+0x6b>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x200,-0xc(%rbp)
jge    1227 <get_pkgname+0xd7>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2d,%eax
je     11f9 <get_pkgname+0xa9>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    11fe <get_pkgname+0xae>
jmp    1227 <get_pkgname+0xd7>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1219 <get_pkgname+0xc9>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c2 <get_pkgname+0x72>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pkgname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x200,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
test   %rbx,%rbx
jne    11a1 <get_pkgname+0x51>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
movzbl -0x1(%rbx,%rax,1),%ecx
lea    -0x2d(%rcx),%edx
cmp    $0x2,%dl
jb     11de <get_pkgname+0x8e>
mov    %cl,-0x1(%r14,%rax,1)
movzbl (%rbx,%rax,1),%ecx
lea    -0x2d(%rcx),%edx
cmp    $0x2,%dl
jb     11de <get_pkgname+0x8e>
mov    %cl,(%r14,%rax,1)
add    $0x2,%rax
cmp    $0x201,%rax
jne    11b0 <get_pkgname+0x60>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @get_pkgname(i8* %0) {
%2 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%3 = tail call i32 @memset(i8* %2, i32 0, i32 512)
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = tail call i8* @getcwd(i32* null, i32 0)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 47)
%8 = getelementptr inbounds i8, i8* %7, i64 1
%9 = tail call i32 @free(i8* %6)
br label %10
10: ; preds = %5, %1
%11 = phi i8* [ %0, %1 ], [ %8, %5 ]
br label %12
12: ; preds = %25, %10
%13 = phi i64 [ 0, %10 ], [ %27, %25 ]
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = add i8 %15, -45
%17 = icmp ult i8 %16, 2
br i1 %17, label %29, label %18
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %2, i64 %13
store i8 %15, i8* %19
%20 = or i64 %13, 1
%21 = getelementptr inbounds i8, i8* %11, i64 %20
%22 = load i8, i8* %21
%23 = add i8 %22, -45
%24 = icmp ult i8 %23, 2
br i1 %24, label %29, label %25
25: ; preds = %18
%26 = getelementptr inbounds i8, i8* %2, i64 %20
store i8 %22, i8* %26
%27 = add nuw nsw i64 %13, 2
%28 = icmp eq i64 %27, 512
br i1 %28, label %29, label %12
29: ; preds = %25, %18, %12
ret i8* %2
}