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

/* 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);
int os_procfs_pname_get(pid_t pid, char *buf, int size) {
  char pname[2048];
  int procfd;
  int len;
  snprintf(pname, sizeof(pname), "/proc/%d/comm", pid);
  if ((procfd = open(pname, O_RDONLY)) < 0) {
    return (-1);
  }
  if ((len = read(procfd, buf, size)) < 0) {
    (void)close(procfd);
    return (-1);
  }
  buf[len - 1] = 0;
  (void)close(procfd);
  return (0);
}


x86 assembly (clang, -O0):
<os_procfs_pname_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x800,%esi
lea    0xe95(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x820(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x824(%rbp)
cmp    $0x0,%eax
jge    11a0 <os_procfs_pname_get+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <os_procfs_pname_get+0xbe>
mov    -0x824(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x828(%rbp)
cmp    $0x0,%eax
jge    11d8 <os_procfs_pname_get+0x98>
mov    -0x824(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <os_procfs_pname_get+0xbe>
mov    -0x10(%rbp),%rax
mov    -0x828(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x824(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_procfs_pname_get>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ecx
lea    0xea6(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x800,%esi
call   1030 <snprintf@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     119d <os_procfs_pname_get+0x5d>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11a4 <os_procfs_pname_get+0x64>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %ebx,%ebx
jmp    11a9 <os_procfs_pname_get+0x69>
mov    $0xffffffff,%ebx
jmp    11b0 <os_procfs_pname_get+0x70>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/comm\00"
@O_RDONLY = global i32 0
define i32 @os_procfs_pname_get(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca [2048 x i8]
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 2048, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %3
%11 = call i32 @read(i32 %8, i8* %1, i32 %2)
%12 = icmp slt i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%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 = %10, %13
%18 = phi i32 [ 0, %13 ], [ -1, %10 ]
%19 = call i32 @close(i32 %8)
br label %20
20: ; preds = %17, %3
%21 = phi i32 [ -1, %3 ], [ %18, %17 ]
ret i32 %21
}