#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
char *get_proc_name(void) {
  int i, ret;
  char proc_name[(512)];
  char *proc = NULL;
  char *ptr = NULL;
  memset(proc_name, 0, (512));
  ret = readlink("/proc/self/exe", proc_name, (512));
  if (ret < 0 || ret >= (512)) {
    fprintf(stderr, "get proc path failed!\n");
    return NULL;
  }
  for (i = ret, ptr = proc_name; i > 0; i--) {
    if (ptr[i] == '/') {
      ptr += i + 1;
      break;
    }
  }
  if (i == 0) {
    return NULL;
  }
  proc = (char *)calloc(1, ret - i);
  if (proc) {
    strncpy(proc, ptr, ret - i);
  }
  return proc;
}


x86 assembly (clang, -O0):
<get_proc_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movq   $0x0,-0x218(%rbp)
movq   $0x0,-0x220(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
lea    -0x210(%rbp),%rsi
lea    0xe6e(%rip),%rdi
mov    $0x200,%edx
call   1040 <readlink@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11b6 <get_proc_name+0x66>
cmpl   $0x200,-0x10(%rbp)
jl     11d8 <get_proc_name+0x88>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a0 <get_proc_name+0x150>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x220(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1240 <get_proc_name+0xf0>
mov    -0x220(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    122d <get_proc_name+0xdd>
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    -0x220(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x220(%rbp)
jmp    1240 <get_proc_name+0xf0>
jmp    1232 <get_proc_name+0xe2>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    11ec <get_proc_name+0x9c>
cmpl   $0x0,-0xc(%rbp)
jne    1257 <get_proc_name+0x107>
movq   $0x0,-0x8(%rbp)
jmp    12a0 <get_proc_name+0x150>
mov    -0x10(%rbp),%esi
sub    -0xc(%rbp),%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     1295 <get_proc_name+0x145>
mov    -0x218(%rbp),%rdi
mov    -0x220(%rbp),%rsi
mov    -0x10(%rbp),%edx
sub    -0xc(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x218(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_name>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
lea    0xe8c(%rip),%rdi
mov    %r14,%rsi
mov    $0x200,%edx
call   1040 <readlink@plt>
cmp    $0x1ff,%eax
jbe    119f <get_proc_name+0x4f>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11d6 <get_proc_name+0x86>
mov    %eax,%ebp
test   %eax,%eax
je     11d6 <get_proc_name+0x86>
mov    $0x1,%eax
cmovle %ebp,%eax
add    $0xffffffff,%eax
mov    %ebp,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ecx,%edx
cmpb   $0x2f,(%rsp,%rdx,1)
je     11e7 <get_proc_name+0x97>
lea    -0x1(%rcx),%edx
cmp    $0x1,%ecx
mov    %edx,%ecx
jg     11c0 <get_proc_name+0x70>
test   %eax,%eax
jne    11f0 <get_proc_name+0xa0>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0x1(%rcx),%eax
lea    (%rsp,%rax,1),%r14
mov    %ecx,%eax
sub    %eax,%ebp
mov    $0x1,%edi
mov    %ebp,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d8 <get_proc_name+0x88>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
jmp    11d8 <get_proc_name+0x88>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"get proc path failed!\0A\00"
define i8* @get_proc_name() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca i8*
%6 = alloca i8*
store i8* null, i8** %5
store i8* null, i8** %6
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 512)
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%10 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9, i32 512)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = load i32, i32* %3
%15 = icmp sge i32 %14, 512
br i1 %15, label %16, label %19
16: ; preds = %13, %0
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %1
br label %64
19: ; preds = %13
%20 = load i32, i32* %3
store i32 %20, i32* %2
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
store i8* %21, i8** %6
br label %22
22: ; preds = %40, %19
%23 = load i32, i32* %2
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %43
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 47
br i1 %32, label %33, label %39
33: ; preds = %25
%34 = load i32, i32* %2
%35 = add nsw i32 %34, 1
%36 = load i8*, i8** %6
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %6
br label %43
39: ; preds = %25
br label %40
40: ; preds = %39
%41 = load i32, i32* %2
%42 = add nsw i32 %41, -1
store i32 %42, i32* %2
br label %22
43: ; preds = %33, %22
%44 = load i32, i32* %2
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
store i8* null, i8** %1
br label %64
47: ; preds = %43
%48 = load i32, i32* %3
%49 = load i32, i32* %2
%50 = sub nsw i32 %48, %49
%51 = call i64 @calloc(i32 1, i32 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %5
%53 = load i8*, i8** %5
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %62
55: ; preds = %47
%56 = load i8*, i8** %5
%57 = load i8*, i8** %6
%58 = load i32, i32* %3
%59 = load i32, i32* %2
%60 = sub nsw i32 %58, %59
%61 = call i32 @strncpy(i8* %56, i8* %57, i32 %60)
br label %62
62: ; preds = %55, %47
%63 = load i8*, i8** %5
store i8* %63, i8** %1
br label %64
64: ; preds = %62, %46, %16
%65 = load i8*, i8** %1
ret i8* %65
}

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