#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, char *, int);
char *strchr(char *, char);
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);
  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 *ptr = strchr(buffer, '\n');
  if (ptr)
    *ptr = '\0';
  char *rv = strdup(buffer);
  if (!rv)
    return NULL;
  if (strlen(rv) == 0) {
    free(rv);
    return NULL;
  }
  return rv;
}


x86 assembly (clang, -O0):
<pid_proc_comm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1080 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11b7 <pid_proc_comm+0x37>
movq   $0x0,-0x8(%rbp)
jmp    12dd <pid_proc_comm+0x15d>
mov    -0x18(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11eb <pid_proc_comm+0x6b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dd <pid_proc_comm+0x15d>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0xfff,%edx
call   1070 <read@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
ja     122e <pid_proc_comm+0xae>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dd <pid_proc_comm+0x15d>
mov    -0x1028(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    -0x1020(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
je     1275 <pid_proc_comm+0xf5>
mov    -0x1030(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1020(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jne    12a3 <pid_proc_comm+0x123>
movq   $0x0,-0x8(%rbp)
jmp    12dd <pid_proc_comm+0x15d>
mov    -0x1038(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    12d2 <pid_proc_comm+0x152>
mov    -0x1038(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dd <pid_proc_comm+0x15d>
mov    -0x1038(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_proc_comm>:
push   %rbx
sub    $0x1010,%rsp
mov    %edi,%edx
lea    0xe7f(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1215 <pid_proc_comm+0xa5>
mov    0x8(%rsp),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %ebx,%ebx
js     1215 <pid_proc_comm+0xa5>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xfff,%edx
call   1060 <read@plt>
test   %rax,%rax
je     120e <pid_proc_comm+0x9e>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11f0 <pid_proc_comm+0x80>
movb   $0x0,(%rax)
lea    0x10(%rsp),%rdi
call   1090 <strdup@plt>
test   %rax,%rax
je     1215 <pid_proc_comm+0xa5>
cmpb   $0x0,(%rax)
jne    1217 <pid_proc_comm+0xa7>
mov    %rax,%rdi
call   1030 <free@plt>
jmp    1215 <pid_proc_comm+0xa5>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x1010,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 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*
%9 = alloca i8*
store i32 %0, i32* %3
%10 = load i32, i32* %3
%11 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %58
14: ; preds = %1
%15 = load i8*, i8** %4
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
store i8* null, i8** %2
br label %58
22: ; preds = %14
%23 = load i8*, i8** %4
%24 = call i32 @free(i8* %23)
%25 = load i32, i32* %5
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%27 = call i64 @read(i32 %25, i8* %26, i32 4095)
store i64 %27, i64* %7
%28 = icmp ule i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
store i8* null, i8** %2
br label %58
32: ; preds = %22
%33 = load i64, i64* %7
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%38 = call i8* @strchr(i8* %37, i8 signext 10)
store i8* %38, i8** %8
%39 = load i8*, i8** %8
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = load i8*, i8** %8
store i8 0, i8* %42
br label %43
43: ; preds = %41, %32
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%45 = call i8* @strdup(i8* %44)
store i8* %45, i8** %9
%46 = load i8*, i8** %9
%47 = icmp ne i8* %46, null
br i1 %47, label %49, label %48
48: ; preds = %43
store i8* null, i8** %2
br label %58
49: ; preds = %43
%50 = load i8*, i8** %9
%51 = call i64 @strlen(i8* %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i8*, i8** %9
%55 = call i32 @free(i8* %54)
store i8* null, i8** %2
br label %58
56: ; preds = %49
%57 = load i8*, i8** %9
store i8* %57, i8** %2
br label %58
58: ; preds = %56, %53, %48, %29, %19, %13
%59 = load i8*, i8** %2
ret i8* %59
}

LLVM IR (-O2):
@.str = [14 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 ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %36, 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 %36, 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 %34
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* @strchr(i8* %15, i8 signext 10)
%24 = icmp eq i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %20
store i8 0, i8* %23
br label %26
26: ; preds = %25, %20
%27 = call i8* @strdup(i8* %15)
%28 = icmp eq i8* %27, null
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = load i8, i8* %27
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @free(i8* %27)
br label %34
34: ; preds = %32, %26, %29, %18
%35 = phi i8* [ null, %18 ], [ null, %32 ], [ null, %26 ], [ %27, %29 ]
br label %36
36: ; preds = %7, %1, %34
%37 = phi i8* [ %35, %34 ], [ null, %1 ], [ null, %7 ]
ret i8* %37
}