#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 */
struct _binbuf {
  int dummy;
};

/* Variables and functions */
scalar_t__ binbuf_read(struct _binbuf *, char *, char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *, char *);
int open_via_path(char *, char *, char *, char *, char **, int,
                  int /*<<< orphan*/);
int binbuf_read_via_path(struct _binbuf *b, char *filename, char *dirname,
                         int crflag) {
  int filedesc;
  char buf[1000], *bufptr;
  if ((filedesc = open_via_path(dirname, filename, "", buf, &bufptr, 1000, 0)) <
      0) {
    error("%s: can't open", filename);
    return (1);
  } else
    close(filedesc);
  if (binbuf_read(b, bufptr, buf, crflag))
    return (1);
  else
    return (0);
}


x86 assembly (clang, -O0):
<binbuf_read_via_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x410(%rbp),%rcx
lea    0xe9e(%rip),%rdx
lea    -0x418(%rbp),%r8
mov    $0x3e8,%r9d
xor    %eax,%eax
movl   $0x0,(%rsp)
call   1040 <open_via_path@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11b3 <binbuf_read_via_path+0x73>
mov    -0x18(%rbp),%rsi
lea    0xe5e(%rip),%rdi
call   1050 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <binbuf_read_via_path+0xb2>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x418(%rbp),%rsi
lea    -0x410(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1060 <binbuf_read@plt>
cmp    $0x0,%rax
je     11eb <binbuf_read_via_path+0xab>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <binbuf_read_via_path+0xb2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binbuf_read_via_path>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %ecx,%r14d
mov    %rdx,%rax
mov    %rsi,%rbp
mov    %rdi,%rbx
movl   $0x0,(%rsp)
lea    0xea9(%rip),%rdx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rax,%rdi
mov    $0x3e8,%r9d
call   1040 <open_via_path@plt>
test   %eax,%eax
js     11aa <binbuf_read_via_path+0x6a>
mov    %eax,%edi
call   1030 <close@plt>
mov    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14d,%ecx
call   1060 <binbuf_read@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
jmp    11be <binbuf_read_via_path+0x7e>
lea    0xe4f(%rip),%rdi
mov    %rbp,%rsi
call   1050 <error@plt>
mov    $0x1,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct._binbuf = type { i32 }
@.str = [1 x i8] zeroinitializer
@.str.1 = [15 x i8] c"%s: can't open\00"
define i32 @binbuf_read_via_path(%struct._binbuf* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct._binbuf*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1000 x i8]
%12 = alloca i8*
store %struct._binbuf* %0, %struct._binbuf** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i8*, i8** %8
%14 = load i8*, i8** %7
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%16 = call i32 @open_via_path(i8* %13, i8* %14, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %15, i8** %12, i32 1000, i32 0)
store i32 %16, i32* %10
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %4
%19 = load i8*, i8** %7
%20 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 1, i32* %5
br label %33
21: ; preds = %4
%22 = load i32, i32* %10
%23 = call i32 @close(i32 %22)
br label %24
24: ; preds = %21
%25 = load %struct._binbuf*, %struct._binbuf** %6
%26 = load i8*, i8** %12
%27 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%28 = load i32, i32* %9
%29 = call i64 @binbuf_read(%struct._binbuf* %25, i8* %26, i8* %27, i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
store i32 1, i32* %5
br label %33
32: ; preds = %24
store i32 0, i32* %5
br label %33
33: ; preds = %32, %31, %18
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
%struct._binbuf = type { i32 }
@.str = [1 x i8] zeroinitializer
@.str.1 = [15 x i8] c"%s: can't open\00"
define i32 @binbuf_read_via_path(%struct._binbuf* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [1000 x i8]
%6 = alloca i8*
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%8 = bitcast i8** %6 to i8*
%9 = call i32 @open_via_path(i8* %2, i8* %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %7, i8** %6, i32 1000, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %19
13: ; preds = %4
%14 = call i32 @close(i32 %9)
%15 = load i8*, i8** %6
%16 = call i64 @binbuf_read(%struct._binbuf* %0, i8* %15, i8* %7, i32 %3)
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %13, %11
%20 = phi i32 [ 1, %11 ], [ %18, %13 ]
ret i32 %20
}