#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*/ fix_path(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int get_path_from_pid(int pid, char *path) {
  char *pathname;
  char *buf;
  char *cpid;
  pathname = malloc(1024 * sizeof(char));
  buf = malloc(1024 * sizeof(char));
  cpid = malloc(1024 * sizeof(char));
  sprintf(cpid, "%d", pid);
  strcpy(pathname, "/proc/");
  strcat(pathname, cpid);
  strcat(pathname, "/exe");
  readlink(pathname, buf, 1024);
  fix_path(buf);
  strcpy(path, buf);
  printf("LOG: get_path_from_pid com %d, %s\n", pid, path);
  return 0;
}


x86 assembly (clang, -O0):
<get_path_from_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe49(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fix_path@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xdf0(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_path_from_pid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe54(%rip),%rsi
mov    %rax,%rdi
mov    %r15d,%edx
call   1090 <sprintf@plt>
lea    0xe45(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
lea    0xe32(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcat@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    %r12,%rdi
call   1050 <fix_path@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
lea    0xe05(%rip),%rdi
mov    %r15d,%esi
mov    %r14,%rdx
call   1060 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [5 x i8] c"/exe\00"
@.str.3 = [35 x i8] c"LOG: get_path_from_pid com %d, %s\0A\00"
define i32 @get_path_from_pid(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = call i8* @malloc(i32 1024)
store i8* %8, i8** %5
%9 = call i8* @malloc(i32 1024)
store i8* %9, i8** %6
%10 = call i8* @malloc(i32 1024)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = load i32, i32* %3
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i8*, i8** %5
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = load i8*, i8** %5
%17 = load i8*, i8** %7
%18 = call i32 @strcat(i8* %16, i8* %17)
%19 = load i8*, i8** %5
%20 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = load i8*, i8** %5
%22 = load i8*, i8** %6
%23 = call i32 @readlink(i8* %21, i8* %22, i32 1024)
%24 = load i8*, i8** %6
%25 = call i32 @fix_path(i8* %24)
%26 = load i8*, i8** %4
%27 = load i8*, i8** %6
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i32, i32* %3
%30 = load i8*, i8** %4
%31 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %29, i8* %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [5 x i8] c"/exe\00"
@.str.3 = [35 x i8] c"LOG: get_path_from_pid com %d, %s\0A\00"
define i32 @get_path_from_pid(i32 %0, i8* %1) {
%3 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%4 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%5 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%6 = tail call i32 @sprintf(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = tail call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @strcat(i8* %3, i8* %5)
%9 = tail call i32 @strcat(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @readlink(i8* %3, i8* %4, i32 1024)
%11 = tail call i32 @fix_path(i8* %4)
%12 = tail call i32 @strcpy(i8* %1, i8* %4)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %0, i8* %1)
ret i32 0
}