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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int asprintf(char **, char *, int /*<<< orphan*/ const);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
size_t read(int, unsigned char *, int);
char *strdup(char *);
scalar_t__ strlen(char *);
char *pid_proc_comm(const pid_t pid) {
  char *fname;
  int fd;
  if (asprintf(&fname, "/proc/%d//comm", pid) == -1)
    return NULL;
  if ((fd = open(fname, O_RDONLY)) < 0) {
    free(fname);
    return NULL;
  }
  free(fname);
  unsigned char buffer[4096];
  ssize_t len;
  if ((len = read(fd, buffer, sizeof(buffer) - 1)) <= 0) {
    close(fd);
    return NULL;
  }
  buffer[len] = '\0';
  close(fd);
  char *rv = strdup((char *)buffer);
  if (strlen(rv) == 0) {
    free(rv);
    return NULL;
  }
  return rv;
}


x86 assembly (clang, -O0):
<pid_proc_comm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11a7 <pid_proc_comm+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1282 <pid_proc_comm+0x112>
mov    -0x18(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11db <pid_proc_comm+0x6b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <pid_proc_comm+0x112>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0xfff,%edx
call   1060 <read@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
ja     121e <pid_proc_comm+0xae>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <pid_proc_comm+0x112>
mov    -0x1028(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
lea    -0x1020(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1277 <pid_proc_comm+0x107>
mov    -0x1030(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <pid_proc_comm+0x112>
mov    -0x1030(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_proc_comm>:
push   %rbx
sub    $0x1010,%rsp
mov    %edi,%edx
lea    0xe8f(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     11e5 <pid_proc_comm+0x85>
mov    0x8(%rsp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %ebx,%ebx
js     11e5 <pid_proc_comm+0x85>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xfff,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11de <pid_proc_comm+0x7e>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
lea    0x10(%rsp),%rdi
call   1080 <strdup@plt>
cmpb   $0x0,(%rax)
jne    11e7 <pid_proc_comm+0x87>
mov    %rax,%rdi
call   1030 <free@plt>
jmp    11e5 <pid_proc_comm+0x85>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x1010,%rsp
pop    %rbx
ret

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

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