#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
size_t read(int, char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strstr(char *, char *);
char *get_program_name_from_pid(int pid, char *buffer, size_t buffer_size) {
  int fd;
  ssize_t len;
  char *aux;
  printf("/proc/%d/cmdline\n", pid);
  sprintf(buffer, "/proc/%d/cmdline", pid);
  if ((fd = open(buffer, O_RDONLY)) < 0)
    return NULL;
  if ((len = read(fd, buffer, buffer_size - 1)) <= 0) {
    close(fd);
    return NULL;
  }
  close(fd);
  buffer[len] = '\0';
  aux = strstr(buffer, "^@");
  if (aux)
    *aux = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<get_program_name_from_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe83(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe82(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11c0 <get_program_name_from_pid+0x60>
movq   $0x0,-0x8(%rbp)
jmp    1239 <get_program_name_from_pid+0xd9>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
ja     11f7 <get_program_name_from_pid+0x97>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1239 <get_program_name_from_pid+0xd9>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1231 <get_program_name_from_pid+0xd1>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_program_name_from_pid>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe8d(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
lea    0xe91(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e0 <get_program_name_from_pid+0x80>
mov    %eax,%ebp
add    $0xffffffffffffffff,%r14
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1040 <close@plt>
test   %r14,%r14
je     11e0 <get_program_name_from_pid+0x80>
movb   $0x0,(%rbx,%r14,1)
lea    0xe55(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     11e2 <get_program_name_from_pid+0x82>
movb   $0x0,(%rax)
jmp    11e2 <get_program_name_from_pid+0x82>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/proc/%d/cmdline\0A\00"
@.str.1 = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
@.str.2 = [3 x i8] c"^@\00"
define i8* @get_program_name_from_pid(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i32, i32* %5
%12 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i8*, i8** %6
%14 = load i32, i32* %5
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %6
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i8* null, i8** %4
br label %45
21: ; preds = %3
%22 = load i32, i32* %8
%23 = load i8*, i8** %6
%24 = load i64, i64* %7
%25 = sub i64 %24, 1
%26 = call i64 @read(i32 %22, i8* %23, i64 %25)
store i64 %26, i64* %9
%27 = icmp ule i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i8* null, i8** %4
br label %45
31: ; preds = %21
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i8*, i8** %6
%35 = load i64, i64* %9
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %6
%38 = call i8* @strstr(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %31
%42 = load i8*, i8** %10
store i8 0, i8* %42
br label %43
43: ; preds = %41, %31
%44 = load i8*, i8** %6
store i8* %44, i8** %4
br label %45
45: ; preds = %43, %28, %20
%46 = load i8*, i8** %4
ret i8* %46
}

LLVM IR (-O2):
@.str = [18 x i8] c"/proc/%d/cmdline\0A\00"
@.str.1 = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
@.str.2 = [3 x i8] c"^@\00"
define i8* @get_program_name_from_pid(i32 %0, i8* %1, i64 %2) {
%4 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %1, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %19, label %9
9: ; preds = %3
%10 = add i64 %2, -1
%11 = tail call i64 @read(i32 %7, i8* %1, i64 %10)
%12 = icmp eq i64 %11, 0
%13 = tail call i32 @close(i32 %7)
br i1 %12, label %19, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 0, i8* %15
%16 = tail call i8* @strstr(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %14
store i8 0, i8* %16
br label %19
19: ; preds = %9, %14, %18, %3
%20 = phi i8* [ null, %3 ], [ %1, %18 ], [ %1, %14 ], [ null, %9 ]
ret i8* %20
}