#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*/ proc_name;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
__attribute__((visibility("default"))) int get_proc_name(char *name,
                                                         size_t len) {
  int i, ret;
  char proc_name[PATH_MAX];
  char *ptr = NULL;
  memset(proc_name, 0, sizeof(proc_name));
  if (-1 == readlink("/proc/self/exe", proc_name, sizeof(proc_name))) {
    fprintf(stderr, "readlink failed!\n");
    return -1;
  }
  ret = strlen(proc_name);
  for (i = ret, ptr = proc_name; i > 0; i--) {
    if (ptr[i] == '/') {
      ptr += i + 1;
      break;
    }
  }
  if (i == 0) {
    fprintf(stderr, "proc path %s is invalid\n", proc_name);
    return -1;
  }
  if (ret - i > (int)len) {
    fprintf(stderr, "proc name length %d is larger than %d\n", ret - i,
            (int)len);
    return -1;
  }
  strncpy(name, ptr, ret - i);
  return 0;
}


x86 assembly (clang, -O0):
<get_proc_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rax
mov    %eax,%edx
lea    0xe4e(%rip),%rdi
call   1040 <readlink@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11f0 <get_proc_name+0xa0>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12f8 <get_proc_name+0x1a8>
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    %ecx,-0x1c(%rbp)
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1257 <get_proc_name+0x107>
mov    -0x38(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1244 <get_proc_name+0xf4>
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    1257 <get_proc_name+0x107>
jmp    1249 <get_proc_name+0xf9>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    120c <get_proc_name+0xbc>
cmpl   $0x0,-0x1c(%rbp)
jne    128f <get_proc_name+0x13f>
mov    -0x50(%rbp),%rdx
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12f8 <get_proc_name+0x1a8>
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jle    12d7 <get_proc_name+0x187>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
lea    0xd7d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12f8 <get_proc_name+0x1a8>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e88(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1060 <memset@plt>
lea    0xe6d(%rip),%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11d4 <get_proc_name+0x84>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11f2 <get_proc_name+0xa2>
mov    %eax,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ecx,%edx
cmpb   $0x2f,(%rbx,%rdx,1)
je     1219 <get_proc_name+0xc9>
lea    -0x1(%rcx),%edx
cmp    $0x1,%ecx
mov    %edx,%ecx
jg     11c0 <get_proc_name+0x70>
jmp    11f8 <get_proc_name+0xa8>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1258 <get_proc_name+0x108>
je     11f8 <get_proc_name+0xa8>
mov    %eax,%ecx
jmp    121f <get_proc_name+0xcf>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1258 <get_proc_name+0x108>
lea    0x1(%rcx),%edx
add    %rdx,%rbx
sub    %ecx,%eax
cmp    %r15d,%eax
jle    1249 <get_proc_name+0xf9>
mov    0x2dab(%rip),%rcx
mov    (%rcx),%edi
lea    0xe04(%rip),%rsi
mov    %eax,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1258 <get_proc_name+0x108>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"readlink failed!\0A\00"
@.str.2 = [25 x i8] c"proc path %s is invalid\0A\00"
@.str.3 = [39 x i8] c"proc name length %d is larger than %d\0A\00"
define i32 @get_proc_name(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
store i8* null, i8** %10
%16 = trunc i64 %13 to i32
%17 = call i32 @memset(i8* %15, i32 0, i32 %16)
%18 = trunc i64 %13 to i32
%19 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %18)
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %11
br label %76
24: ; preds = %2
%25 = call i32 @strlen(i8* %15)
store i32 %25, i32* %7
%26 = load i32, i32* %7
store i32 %26, i32* %6
store i8* %15, i8** %10
br label %27
27: ; preds = %45, %24
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %48
30: ; preds = %27
%31 = load i8*, i8** %10
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 47
br i1 %37, label %38, label %44
38: ; preds = %30
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
%41 = load i8*, i8** %10
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %10
br label %48
44: ; preds = %30
br label %45
45: ; preds = %44
%46 = load i32, i32* %6
%47 = add nsw i32 %46, -1
store i32 %47, i32* %6
br label %27
48: ; preds = %38, %27
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
store i32 1, i32* %11
br label %76
54: ; preds = %48
%55 = load i32, i32* %7
%56 = load i32, i32* %6
%57 = sub nsw i32 %55, %56
%58 = load i64, i64* %5
%59 = trunc i64 %58 to i32
%60 = icmp sgt i32 %57, %59
br i1 %60, label %61, label %69
61: ; preds = %54
%62 = load i32, i32* @stderr
%63 = load i32, i32* %7
%64 = load i32, i32* %6
%65 = sub nsw i32 %63, %64
%66 = load i64, i64* %5
%67 = trunc i64 %66 to i32
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %65, i32 %67)
store i32 -1, i32* %3
store i32 1, i32* %11
br label %76
69: ; preds = %54
%70 = load i8*, i8** %4
%71 = load i8*, i8** %10
%72 = load i32, i32* %7
%73 = load i32, i32* %6
%74 = sub nsw i32 %72, %73
%75 = call i32 @strncpy(i8* %70, i8* %71, i32 %74)
store i32 0, i32* %3
store i32 1, i32* %11
br label %76
76: ; preds = %69, %61, %51, %21
%77 = load i8*, i8** %8
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"readlink failed!\0A\00"
@.str.2 = [25 x i8] c"proc path %s is invalid\0A\00"
@.str.3 = [39 x i8] c"proc name length %d is larger than %d\0A\00"
define i32 @get_proc_name(i8* %0, i64 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @memset(i8* %5, i32 0, i32 %3)
%7 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %44
12: ; preds = %2
%13 = call i32 @strlen(i8* %5)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %12, %25
%16 = phi i32 [ %26, %25 ], [ %13, %12 ]
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %5, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 47
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = add nuw nsw i32 %16, 1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %5, i64 %23
br label %33
25: ; preds = %15
%26 = add nsw i32 %16, -1
%27 = icmp sgt i32 %16, 1
br i1 %27, label %15, label %30
28: ; preds = %12
%29 = icmp eq i32 %13, 0
br i1 %29, label %30, label %33
30: ; preds = %25, %28
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %44
33: ; preds = %21, %28
%34 = phi i32 [ %16, %21 ], [ %13, %28 ]
%35 = phi i8* [ %24, %21 ], [ %5, %28 ]
%36 = sub nsw i32 %13, %34
%37 = trunc i64 %1 to i32
%38 = icmp sgt i32 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32 %37)
br label %44
42: ; preds = %33
%43 = call i32 @strncpy(i8* %0, i8* %35, i32 %36)
br label %44
44: ; preds = %42, %39, %30, %9
%45 = phi i32 [ -1, %9 ], [ -1, %30 ], [ -1, %39 ], [ 0, %42 ]
ret i32 %45
}