#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*/ O_RDONLY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
size_t fcopy(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int openat(int, char *, int /*<<< orphan*/);
int slurp_buf(int dirfd, char *path, char **buf, size_t *len) {
  FILE *instream, *outstream;
  outstream = open_memstream(buf, len);
  if (!outstream)
    return -1;
  int fd = openat(dirfd, path, O_RDONLY);
  if (fd < 0)
    return -1;
  instream = fdopen(fd, "rb");
  size_t count = fcopy(outstream, instream);
  fclose(outstream);
  fclose(instream);
  return count;
}


x86 assembly (clang, -O0):
<slurp_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <open_memstream@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    118f <slurp_buf+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <slurp_buf+0xaa>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1050 <openat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11bd <slurp_buf+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <slurp_buf+0xaa>
mov    -0x34(%rbp),%edi
lea    0xe39(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <fcopy@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
mov    %rdx,%rdi
mov    %rcx,%rsi
call   1030 <open_memstream@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11bb <slurp_buf+0x6b>
mov    %rax,%rbx
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r15,%rsi
call   1050 <openat@plt>
test   %eax,%eax
js     11bb <slurp_buf+0x6b>
lea    0xe6d(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <fcopy@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"rb\00"
define i32 @slurp_buf(i32 %0, i8* %1, i8** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i64* %3, i64** %9
%14 = load i8**, i8*** %8
%15 = load i64*, i64** %9
%16 = call i32* @open_memstream(i8** %14, i64* %15)
store i32* %16, i32** %11
%17 = load i32*, i32** %11
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %4
store i32 -1, i32* %5
br label %40
20: ; preds = %4
%21 = load i32, i32* %6
%22 = load i8*, i8** %7
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @openat(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %20
store i32 -1, i32* %5
br label %40
28: ; preds = %20
%29 = load i32, i32* %12
%30 = call i32* @fdopen(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %30, i32** %10
%31 = load i32*, i32** %11
%32 = load i32*, i32** %10
%33 = call i64 @fcopy(i32* %31, i32* %32)
store i64 %33, i64* %13
%34 = load i32*, i32** %11
%35 = call i32 @fclose(i32* %34)
%36 = load i32*, i32** %10
%37 = call i32 @fclose(i32* %36)
%38 = load i64, i64* %13
%39 = trunc i64 %38 to i32
store i32 %39, i32* %5
br label %40
40: ; preds = %28, %27, %19
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"rb\00"
define i32 @slurp_buf(i32 %0, i8* %1, i8** %2, i64* %3) {
%5 = tail call i32* @open_memstream(i8** %2, i64* %3)
%6 = icmp eq i32* %5, null
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @openat(i32 %0, i8* %1, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = tail call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = tail call i64 @fcopy(i32* %5, i32* %12)
%14 = tail call i32 @fclose(i32* %5)
%15 = tail call i32 @fclose(i32* %12)
%16 = trunc i64 %13 to i32
br label %17
17: ; preds = %11, %7, %4
%18 = phi i32 [ -1, %4 ], [ %16, %11 ], [ -1, %7 ]
ret i32 %18
}