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

/* Variables and functions */
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *get_exec_name(pid_t pid, char *name) {
  char cmd[50] = {0};
  FILE *fd = NULL;
  sprintf(cmd, "readlink /proc/%d/exe", pid);
  fd = popen(cmd, "r");
  if (NULL == fd) {
    return NULL;
  }
  fscanf(fd, "%s", name);
  pclose(fd);
  fd = NULL;
  return name;
}


x86 assembly (clang, -O0):
<get_exec_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1040 <memset@plt>
movq   $0x0,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe7b(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x58(%rbp)
xor    %eax,%eax
cmp    -0x58(%rbp),%rax
jne    11b7 <get_exec_name+0x67>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <get_exec_name+0x94>
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe52(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x58(%rbp),%rdi
call   1030 <pclose@plt>
movq   $0x0,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_exec_name>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,%edx
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movw   $0x0,0x30(%rsp)
lea    0xe95(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
lea    0xe99(%rip),%rsi
mov    %rbx,%rdi
call   1050 <popen@plt>
test   %rax,%rax
je     11a9 <get_exec_name+0x69>
mov    %rax,%rbx
lea    0xe84(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <pclose@plt>
jmp    11ac <get_exec_name+0x6c>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [22 x i8] c"readlink /proc/%d/exe\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
define i8* @get_exec_name(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [50 x i8]
%7 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = bitcast [50 x i8]* %6 to i8*
store i32* null, i32** %7
%9 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%13 = call i32* @popen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp eq i32* null, %14
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* null, i8** %3
br label %24
17: ; preds = %2
%18 = load i32*, i32** %7
%19 = load i8*, i8** %5
%20 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = load i32*, i32** %7
%22 = call i32 @pclose(i32* %21)
store i32* null, i32** %7
%23 = load i8*, i8** %5
store i8* %23, i8** %3
br label %24
24: ; preds = %17, %16
%25 = load i8*, i8** %3
ret i8* %25
}

LLVM IR (-O2):
@.str = [22 x i8] c"readlink /proc/%d/exe\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
define i8* @get_exec_name(i32 %0, i8* %1) {
%3 = alloca [50 x i8]
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%10 = tail call i32 @pclose(i32* %6)
br label %11
11: ; preds = %2, %8
%12 = phi i8* [ %1, %8 ], [ null, %2 ]
ret i8* %12
}