#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int read_proc(char *proc, char *buf, int len) {
  int ret = -1;
  if (!proc || !buf)
    return -1;
  FILE *fp = NULL;
  fp = fopen(proc, "r");
  if (fp) {
    if (fgets(buf, len, fp) != NULL) {
      ret = 0;
    }
    fclose(fp);
  }
  return ret;
}


x86 assembly (clang, -O0):
<read_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1160 <read_proc+0x30>
cmpq   $0x0,-0x18(%rbp)
jne    116c <read_proc+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <read_proc+0x93>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11bd <read_proc+0x8d>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11b4 <read_proc+0x84>
movl   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     117c <read_proc+0x4c>
mov    %rsi,%rbp
test   %rsi,%rsi
je     117c <read_proc+0x4c>
mov    %edx,%r14d
lea    0xead(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117c <read_proc+0x4c>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
cmp    $0x1,%rax
sbb    %ebx,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @read_proc(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 -1, i32* %8
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12, %3
store i32 -1, i32* %4
br label %33
16: ; preds = %12
store i32* null, i32** %9
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %9
%19 = load i32*, i32** %9
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %31
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = load i32*, i32** %9
%25 = call i32* @fgets(i8* %22, i32 %23, i32* %24)
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 0, i32* %8
br label %28
28: ; preds = %27, %21
%29 = load i32*, i32** %9
%30 = call i32 @fclose(i32* %29)
br label %31
31: ; preds = %28, %16
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %33
33: ; preds = %31, %15
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @read_proc(i8* %0, i8* %1, i32 %2) {
%4 = icmp ne i8* %0, null
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
br i1 %6, label %7, label %15
7: ; preds = %3
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = tail call i32* @fgets(i8* %1, i32 %2, i32* %8)
%12 = icmp eq i32* %11, null
%13 = sext i1 %12 to i32
%14 = tail call i32 @fclose(i32* %8)
br label %15
15: ; preds = %7, %10, %3
%16 = phi i32 [ -1, %3 ], [ %13, %10 ], [ -1, %7 ]
ret i32 %16
}