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

/* Variables and functions */
int EIO;
int ENOENT;
int ENXIO;
int EOF;
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int putc(int, int /*<<< orphan*/ *);
int read_procfile(FILE *fp_out, const char *procfile) {
  FILE *fp;
  if (!fp_out)
    return -ENXIO;
  fp = fopen(procfile, "r");
  if (!fp)
    return -ENOENT;
  while (!feof(fp)) {
    int c = fgetc(fp);
    if (c == EOF)
      return 0;
    if (putc(c, fp_out) == EOF)
      return -EIO;
    if (ferror(fp))
      return -EIO;
  }
  return fclose(fp);
}


x86 assembly (clang, -O0):
<read_procfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118e <read_procfile+0x2e>
xor    %eax,%eax
mov    0x2e4c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1272 <read_procfile+0x112>
mov    -0x18(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c0 <read_procfile+0x60>
xor    %eax,%eax
mov    0x2e0a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1272 <read_procfile+0x112>
jmp    11c5 <read_procfile+0x65>
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11e3 <read_procfile+0x83>
jmp    1266 <read_procfile+0x106>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%eax
jne    120d <read_procfile+0xad>
movl   $0x0,-0x4(%rbp)
jmp    1272 <read_procfile+0x112>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <putc@plt>
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%eax
jne    123b <read_procfile+0xdb>
xor    %eax,%eax
mov    0x2da7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1272 <read_procfile+0x112>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1261 <read_procfile+0x101>
xor    %eax,%eax
mov    0x2d81(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1272 <read_procfile+0x112>
jmp    11c5 <read_procfile+0x65>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_procfile>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11ce <read_procfile+0x6e>
mov    %rdi,%r14
lea    0xe8c(%rip),%rax
mov    %rsi,%rdi
mov    %rax,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d7 <read_procfile+0x77>
mov    %rax,%rbx
mov    0x2e5a(%rip),%r15
xchg   %ax,%ax
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11e8 <read_procfile+0x88>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    (%r15),%eax
je     11f5 <read_procfile+0x95>
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <putc@plt>
cmp    (%r15),%eax
je     11c5 <read_procfile+0x65>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1190 <read_procfile+0x30>
mov    0x2e0c(%rip),%rcx
jmp    11de <read_procfile+0x7e>
mov    0x2dfb(%rip),%rcx
jmp    11de <read_procfile+0x7e>
mov    0x2de2(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
xor    %eax,%eax
jmp    11e2 <read_procfile+0x82>

LLVM IR (-O0):
@ENXIO = global i32 0
@.str = [2 x i8] c"r\00"
@ENOENT = global i32 0
@EOF = global i32 0
@EIO = global i32 0
define i32 @read_procfile(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* @ENXIO
%12 = sub nsw i32 0, %11
store i32 %12, i32* %3
br label %54
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i32, i32* @ENOENT
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %54
21: ; preds = %13
br label %22
22: ; preds = %50, %21
%23 = load i32*, i32** %6
%24 = call i32 @feof(i32* %23)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br i1 %26, label %27, label %51
27: ; preds = %22
%28 = load i32*, i32** %6
%29 = call i32 @fgetc(i32* %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 0, i32* %3
br label %54
34: ; preds = %27
%35 = load i32, i32* %7
%36 = load i32*, i32** %4
%37 = call i32 @putc(i32 %35, i32* %36)
%38 = load i32, i32* @EOF
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i32, i32* @EIO
%42 = sub nsw i32 0, %41
store i32 %42, i32* %3
br label %54
43: ; preds = %34
%44 = load i32*, i32** %6
%45 = call i64 @ferror(i32* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @EIO
%49 = sub nsw i32 0, %48
store i32 %49, i32* %3
br label %54
50: ; preds = %43
br label %22
51: ; preds = %22
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
store i32 %53, i32* %3
br label %54
54: ; preds = %51, %47, %40, %33, %18, %10
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@ENXIO = global i32 0
@.str = [2 x i8] c"r\00"
@ENOENT = global i32 0
@EOF = global i32 0
@EIO = global i32 0
define i32 @read_procfile(i32* %0, i8* nocapture %1) {
%3 = icmp eq i32* %0, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @ENXIO
%6 = sub nsw i32 0, %5
br label %35
7: ; preds = %2
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @ENOENT
%12 = sub nsw i32 0, %11
br label %35
13: ; preds = %7, %27
%14 = tail call i32 @feof(i32* %8)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %13
%17 = tail call i32 @fgetc(i32* %8)
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %35, label %20
20: ; preds = %16
%21 = tail call i32 @putc(i32 %17, i32* %0)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @EIO
%26 = sub nsw i32 0, %25
br label %35
27: ; preds = %20
%28 = tail call i64 @ferror(i32* %8)
%29 = icmp eq i64 %28, 0
br i1 %29, label %13, label %30
30: ; preds = %27
%31 = load i32, i32* @EIO
%32 = sub nsw i32 0, %31
br label %35
33: ; preds = %13
%34 = tail call i32 @fclose(i32* %8)
br label %35
35: ; preds = %16, %24, %30, %33, %10, %4
%36 = phi i32 [ %34, %33 ], [ %12, %10 ], [ %6, %4 ], [ %26, %24 ], [ %32, %30 ], [ 0, %16 ]
ret i32 %36
}