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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
char *strdup(char *);
char *get_pid_comm(const pid_t pid) {
  char buffer[4096];
  int fd;
  ssize_t ret;
  (void)snprintf(buffer, sizeof(buffer), "/proc/%i/comm", pid);
  if ((fd = open(buffer, O_RDONLY)) < 0)
    return NULL;
  if ((ret = read(fd, buffer, sizeof(buffer))) <= 0) {
    (void)close(fd);
    return NULL;
  }
  (void)close(fd);
  buffer[ret - 1] = '\0';
  return strdup(buffer);
}


x86 assembly (clang, -O0):
<get_pid_comm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x1000,%esi
lea    0xe8c(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jge    11aa <get_pid_comm+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    1216 <get_pid_comm+0xc6>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1018(%rbp)
cmp    $0x0,%eax
jg     11e8 <get_pid_comm+0x98>
mov    -0x1014(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1216 <get_pid_comm+0xc6>
mov    -0x1014(%rbp),%edi
call   1040 <close@plt>
mov    -0x1018(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_comm>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%ecx
lea    0xe9d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1030 <snprintf@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11bb <get_pid_comm+0x6b>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1040 <close@plt>
test   %r14d,%r14d
jle    11bb <get_pid_comm+0x6b>
add    $0xffffffff,%r14d
movb   $0x0,(%rsp,%r14,1)
mov    %rsp,%rdi
call   1070 <strdup@plt>
jmp    11bd <get_pid_comm+0x6d>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%i/comm\00"
@O_RDONLY = global i32 0
define i8* @get_pid_comm(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %32
15: ; preds = %1
%16 = load i32, i32* %5
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 4096)
store i32 %18, i32* %6
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
store i8* null, i8** %2
br label %32
23: ; preds = %15
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %6
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%31 = call i8* @strdup(i8* %30)
store i8* %31, i8** %2
br label %32
32: ; preds = %23, %20, %14
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%i/comm\00"
@O_RDONLY = global i32 0
define noalias i8* @get_pid_comm(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %1
%9 = call i32 @read(i32 %6, i8* %3, i32 4096)
%10 = icmp slt i32 %9, 1
%11 = call i32 @close(i32 %6)
br i1 %10, label %17, label %12
12: ; preds = %8
%13 = add nsw i32 %9, -1
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i8* @strdup(i8* %3)
br label %17
17: ; preds = %8, %1, %12
%18 = phi i8* [ %16, %12 ], [ null, %1 ], [ null, %8 ]
ret i8* %18
}