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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strrchr(char *, char);
int trace_get_progname(char *buf, int size) {
  char path[1024] = {0};
  char *str = NULL;
  FILE *fp = NULL;
  sprintf(path, "/proc/%d/cmdline", getpid());
  if ((fp = fopen(path, "r")) == NULL) {
    fprintf(stderr, "open program cmdline file failed\n");
    return -1;
  }
  fgets(path, size, fp);
  fclose(fp);
  if ((str = strrchr(path, '/'))) {
    strcpy(buf, str + 1);
  } else {
    strcpy(buf, path);
  }
  return 0;
}


x86 assembly (clang, -O0):
<trace_get_progname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
movq   $0x0,-0x428(%rbp)
movq   $0x0,-0x430(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%edx
lea    0xe10(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x430(%rbp)
cmp    $0x0,%rax
jne    123a <trace_get_progname+0xaa>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <trace_get_progname+0x11e>
lea    -0x420(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x430(%rbp),%rdx
call   1080 <fgets@plt>
mov    -0x430(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x420(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     1297 <trace_get_progname+0x107>
mov    -0x10(%rbp),%rdi
mov    -0x428(%rbp),%rsi
add    $0x1,%rsi
call   1030 <strcpy@plt>
jmp    12a7 <trace_get_progname+0x117>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trace_get_progname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rsp,%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1219 <trace_get_progname+0x89>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1235 <trace_get_progname+0xa5>
add    $0x1,%rax
mov    %r14,%rdi
mov    %rax,%rsi
jmp    123b <trace_get_progname+0xab>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1242 <trace_get_progname+0xb2>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"open program cmdline file failed\0A\00"
define i32 @trace_get_progname(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = bitcast [1024 x i8]* %6 to i8*
store i8* null, i8** %7
store i32* null, i32** %8
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = call i32 (...) @getpid()
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %8
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
19: ; preds = %2
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%21 = load i32, i32* %5
%22 = load i32*, i32** %8
%23 = call i32 @fgets(i8* %20, i32 %21, i32* %22)
%24 = load i32*, i32** %8
%25 = call i32 @fclose(i32* %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%27 = call i8* @strrchr(i8* %26, i8 signext 47)
store i8* %27, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = load i8*, i8** %4
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = call i32 @strcpy(i8* %30, i8* %32)
br label %38
34: ; preds = %19
%35 = load i8*, i8** %4
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = call i32 @strcpy(i8* %35, i8* %36)
br label %38
38: ; preds = %34, %29
store i32 0, i32* %3
br label %39
39: ; preds = %38, %16
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"open program cmdline file failed\0A\00"
define i32 @trace_get_progname(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %22
12: ; preds = %2
%13 = call i32 @fgets(i8* %4, i32 %1, i32* %7)
%14 = call i32 @fclose(i32* %7)
%15 = call i8* @strrchr(i8* %4, i8 signext 47)
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = getelementptr inbounds i8, i8* %15, i64 1
%19 = call i32 @strcpy(i8* %0, i8* %18)
br label %22
20: ; preds = %12
%21 = call i32 @strcpy(i8* %0, i8* %4)
br label %22
22: ; preds = %17, %20, %9
%23 = phi i32 [ -1, %9 ], [ 0, %20 ], [ 0, %17 ]
ret i32 %23
}