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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int get_comm(pid_t pid, char *comm, int len) {
  char filename[1024];
  int fd;
  *comm = '\0';
  snprintf(filename, sizeof(filename), "/proc/%d/comm", pid);
  fd = open(filename, 0);
  if (fd >= 0) {
    len = read(fd, comm, len - 1);
    if (len >= 0)
      comm[len - 1] = '\0';
    close(fd);
  } else
    len = -1;
  return len;
}


x86 assembly (clang, -O0):
<get_comm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x420(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x400,%esi
lea    0xe8e(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jl     11db <get_comm+0x9b>
mov    -0x424(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11cb <get_comm+0x8b>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x424(%rbp),%edi
call   1040 <close@plt>
jmp    11e2 <get_comm+0xa2>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_comm>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%ecx
movb   $0x0,(%rsi)
lea    0xea4(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
js     11a3 <get_comm+0x63>
mov    %eax,%ebx
add    $0xffffffff,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     119a <get_comm+0x5a>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%r14,1)
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11a8 <get_comm+0x68>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/comm\00"
define i32 @get_comm(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8 0, i8* %1
%6 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32 @open(i8* %5, i32 0)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %19
9: ; preds = %3
%10 = add nsw i32 %2, -1
%11 = call i32 @read(i32 %7, i8* %1, i32 %10)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = add nsw i32 %11, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %13, %9
%18 = call i32 @close(i32 %7)
br label %19
19: ; preds = %3, %17
%20 = phi i32 [ %11, %17 ], [ -1, %3 ]
ret i32 %20
}