#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*/);
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;
  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    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe8f(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11a1 <get_program_name_from_pid+0x51>
movq   $0x0,-0x8(%rbp)
jmp    121a <get_program_name_from_pid+0xca>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
ja     11d8 <get_program_name_from_pid+0x88>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    121a <get_program_name_from_pid+0xca>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1212 <get_program_name_from_pid+0xc2>
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,%edx
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11c0 <get_program_name_from_pid+0x70>
mov    %eax,%ebp
add    $0xffffffffffffffff,%r14
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1030 <close@plt>
test   %r14,%r14
je     11c0 <get_program_name_from_pid+0x70>
movb   $0x0,(%rbx,%r14,1)
lea    0xe63(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     11c2 <get_program_name_from_pid+0x72>
movb   $0x0,(%rax)
jmp    11c2 <get_program_name_from_pid+0x72>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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