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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char const *);
int read_proc_file(char *buf, size_t buf_size, int pid, const char *name) {
  int fd;
  int bytes;
  int retcode = -1;
  char filename[256];
  snprintf(filename, sizeof(filename), "/proc/%d/%s", pid, name);
  if ((fd = open(filename, O_RDONLY)) < 0) {
    return retcode;
  }
  bytes = read(fd, buf, buf_size - 1);
  if (bytes < 0) {
    goto error;
  }
  buf[bytes] = '\0';
  retcode = 0;
error:
  close(fd);
  return retcode;
}


x86 assembly (clang, -O0):
<read_proc_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x34(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    $0x100,%esi
lea    0xe85(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x140(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11ac <read_proc_file+0x6c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <read_proc_file+0xb3>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11d2 <read_proc_file+0x92>
jmp    11e5 <read_proc_file+0xa5>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rcx,%r8
mov    %edx,%ecx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xea3(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1030 <snprintf@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a4 <read_proc_file+0x64>
mov    %eax,%ebp
add    $0xffffffffffffffff,%rbx
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
test   %eax,%eax
js     11ab <read_proc_file+0x6b>
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
xor    %ebx,%ebx
jmp    11b0 <read_proc_file+0x70>
mov    $0xffffffff,%ebx
jmp    11b7 <read_proc_file+0x77>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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