#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
char *get_comm_pid(unsigned int pid) {
  char *proc_name;
  char *comm;
  int comm_fd, cnt;
  proc_name = (char *)malloc(50);
  if (proc_name != NULL) {
    sprintf(proc_name, "/proc/%u/comm\0", pid);
  } else
    return NULL;
  comm = (char *)malloc(50);
  if (comm != NULL) {
    memset(comm, 0, 50);
  } else
    return NULL;
  comm_fd = open(proc_name, O_RDONLY);
  if (comm_fd != -1) {
    cnt = read(comm_fd, comm, 50);
    if (cnt != -1) {
      comm[cnt] = '\0';
      close(comm_fd);
      return comm;
    }
    close(comm_fd);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_comm_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x32,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119c <get_comm_pid+0x3c>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe6e(%rip),%rsi
call   1080 <sprintf@plt>
jmp    11a9 <get_comm_pid+0x49>
movq   $0x0,-0x8(%rbp)
jmp    1252 <get_comm_pid+0xf2>
mov    $0x32,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d7 <get_comm_pid+0x77>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1030 <memset@plt>
jmp    11e4 <get_comm_pid+0x84>
movq   $0x0,-0x8(%rbp)
jmp    1252 <get_comm_pid+0xf2>
mov    -0x18(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     124a <get_comm_pid+0xea>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x32,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     1242 <get_comm_pid+0xe2>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1252 <get_comm_pid+0xf2>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_comm_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x32,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e8 <get_comm_pid+0x88>
mov    %rax,%rbx
lea    0xe7e(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1080 <sprintf@plt>
mov    $0x32,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e8 <get_comm_pid+0x88>
mov    %rax,%r15
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1030 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f7 <get_comm_pid+0x97>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x32,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11ed <get_comm_pid+0x8d>
cltq
movb   $0x0,(%r15,%rax,1)
mov    %r15,%r14
jmp    11f0 <get_comm_pid+0x90>
xor    %r14d,%r14d
jmp    11f7 <get_comm_pid+0x97>
xor    %r14d,%r14d
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%u/comm\00\00"
@O_RDONLY = global i32 0
define i8* @get_comm_pid(i32 %0) {
%2 = tail call i64 @malloc(i32 50)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %24, label %5
5: ; preds = %1
%6 = tail call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = tail call i64 @malloc(i32 50)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %24, label %10
10: ; preds = %5
%11 = tail call i32 @memset(i8* %8, i32 0, i32 50)
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %3, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %24, label %15
15: ; preds = %10
%16 = tail call i32 @read(i32 %13, i8* %8, i32 50)
%17 = icmp eq i32 %16, -1
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = sext i32 %16 to i64
%20 = getelementptr inbounds i8, i8* %8, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %15, %18
%22 = phi i8* [ %8, %18 ], [ null, %15 ]
%23 = tail call i32 @close(i32 %13)
br label %24
24: ; preds = %21, %10, %5, %1
%25 = phi i8* [ null, %1 ], [ null, %5 ], [ null, %10 ], [ %22, %21 ]
ret i8* %25
}