#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 O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int openat(int, char *, int, int);
int spew_buf(int dirfd, char *path, char *buf, size_t len) {
  FILE *stream;
  int fd = openat(dirfd, path, O_WRONLY | O_CREAT, 0644);
  if (fd < 0)
    return -1;
  stream = fdopen(fd, "wb");
  if (!stream)
    return -1;
  size_t count = fwrite(buf, 1, len, stream);
  fclose(stream);
  return count;
}


x86 assembly (clang, -O0):
<spew_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    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
mov    0x2e8a(%rip),%rax
or     (%rax),%edx
mov    $0x1a4,%ecx
call   1040 <openat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1193 <spew_buf+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <spew_buf+0xa7>
mov    -0x2c(%rbp),%edi
lea    0xe63(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bd <spew_buf+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <spew_buf+0xa7>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spew_buf>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    0x2e7b(%rip),%rax
mov    0x2e9c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x1a4,%ecx
call   1040 <openat@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     11a8 <spew_buf+0x68>
lea    0xe85(%rip),%rsi
mov    %eax,%edi
call   1050 <fdopen@plt>
test   %rax,%rax
je     11a8 <spew_buf+0x68>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [3 x i8] c"wb\00"
define i32 @spew_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 i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = load i32, i32* %6
%14 = load i8*, i8** %7
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = call i32 @openat(i32 %13, i8* %14, i32 %17, i32 420)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 -1, i32* %5
br label %37
22: ; preds = %4
%23 = load i32, i32* %11
%24 = call i32* @fdopen(i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %10
%25 = load i32*, i32** %10
%26 = icmp ne i32* %25, null
br i1 %26, label %28, label %27
27: ; preds = %22
store i32 -1, i32* %5
br label %37
28: ; preds = %22
%29 = load i8*, i8** %8
%30 = load i64, i64* %9
%31 = load i32*, i32** %10
%32 = call i64 @fwrite(i8* %29, i32 1, i64 %30, i32* %31)
store i64 %32, i64* %12
%33 = load i32*, i32** %10
%34 = call i32 @fclose(i32* %33)
%35 = load i64, i64* %12
%36 = trunc i64 %35 to i32
store i32 %36, i32* %5
br label %37
37: ; preds = %28, %27, %21
%38 = load i32, i32* %5
ret i32 %38
}

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