#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);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int get_file(int pid, char *name, char *buf) {
  char file[80];
  int fd, num_read;
  sprintf(file, "/proc/%d/%s", pid, name);
  if ((fd = open(file, O_RDONLY, 0)) < 0) {
    fprintf(stderr, "open: %s: %s\n", file, strerror(errno));
    return 0;
  }
  if ((num_read = read(fd, buf, 512)) < 0) {
    fprintf(stderr, "read: %s: %s\n", file, strerror(errno));
    return 0;
  }
  if (num_read > 0) {
    buf[num_read - 1] = 0;
  }
  close(fd);
  return num_read;
}


x86 assembly (clang, -O0):
<get_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe78(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x74(%rbp)
cmp    $0x0,%eax
jge    11f7 <get_file+0x97>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rdx
mov    %rax,%rcx
lea    0xe26(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128d <get_file+0x12d>
mov    -0x74(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0x78(%rbp)
cmp    $0x0,%eax
jge    1264 <get_file+0x104>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x94(%rbp),%edi
mov    -0x90(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc7(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128d <get_file+0x12d>
cmpl   $0x0,-0x78(%rbp)
jle    127f <get_file+0x11f>
mov    -0x18(%rbp),%rax
mov    -0x78(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x74(%rbp),%edi
call   1030 <close@plt>
mov    -0x78(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rdx,%r14
mov    %rsi,%rcx
mov    %edi,%edx
lea    0xe89(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1070 <sprintf@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %edx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11c5 <get_file+0x65>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11f2 <get_file+0x92>
mov    %eax,%ebx
je     11bc <get_file+0x5c>
lea    -0x1(%rbx),%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebp,%edi
call   1030 <close@plt>
jmp    121f <get_file+0xbf>
mov    0x2e0c(%rip),%rax
mov    (%rax),%ebp
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe29(%rip),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %rax,%rcx
call   1050 <fprintf@plt>
jmp    121f <get_file+0xbf>
mov    0x2ddf(%rip),%rax
mov    (%rax),%ebx
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe0a(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x50,%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
@stderr = global i32 0
@.str.1 = [14 x i8] c"open: %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [14 x i8] c"read: %s: %s\0A\00"
define i32 @get_file(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [80 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13)
%15 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16, i32 0)
store i32 %17, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %23)
store i32 0, i32* %4
br label %49
25: ; preds = %3
%26 = load i32, i32* %9
%27 = load i8*, i8** %7
%28 = call i32 @read(i32 %26, i8* %27, i32 512)
store i32 %28, i32* %10
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %32, i8* %34)
store i32 0, i32* %4
br label %49
36: ; preds = %25
%37 = load i32, i32* %10
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i8*, i8** %7
%41 = load i32, i32* %10
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 0, i8* %44
br label %45
45: ; preds = %39, %36
%46 = load i32, i32* %9
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %10
store i32 %48, i32* %4
br label %49
49: ; preds = %45, %30, %19
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@.str = [12 x i8] c"/proc/%d/%s\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"open: %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [14 x i8] c"read: %s: %s\0A\00"
define i32 @get_file(i32 %0, i8* %1, i8* nocapture %2) {
%4 = alloca [80 x i8]
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %5, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %13)
br label %31
15: ; preds = %3
%16 = tail call i32 @read(i32 %8, i8* %2, i32 512)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %21)
br label %31
23: ; preds = %15
%24 = icmp eq i32 %16, 0
br i1 %24, label %29, label %25
25: ; preds = %23
%26 = add nsw i32 %16, -1
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %2, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %25, %23
%30 = tail call i32 @close(i32 %8)
br label %31
31: ; preds = %29, %18, %10
%32 = phi i32 [ 0, %10 ], [ 0, %18 ], [ %16, %29 ]
ret i32 %32
}