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

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int openat(int, char const *, int, int);
scalar_t__ vdprintf(int, char const *, int /*<<< orphan*/);
int vspew(int dd, const char *path, const char *format, va_list vargs) {
  int fd = openat(dd, path, O_WRONLY | O_CREAT, 0777);
  if (fd == -1) {
    return -1;
  }
  if (vdprintf(fd, format, vargs) < 0) {
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<vspew>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
mov    0x2e9b(%rip),%rax
or     (%rax),%edx
mov    $0x1ff,%ecx
call   1030 <openat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1182 <vspew+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <vspew+0x8e>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <vdprintf@plt>
cmp    $0x0,%rax
jge    11af <vspew+0x7f>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <vspew+0x8e>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vspew>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    0x2e8f(%rip),%rax
mov    0x2eb0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x1ff,%ecx
call   1030 <openat@plt>
cmp    $0xffffffff,%eax
je     117a <vspew+0x4a>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <vdprintf@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
sar    $0x3f,%rbx
jmp    117f <vspew+0x4f>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @vspew(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i32, i32* %6
%12 = load i8*, i8** %7
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @openat(i32 %11, i8* %12, i32 %15, i32 511)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %4
store i32 -1, i32* %5
br label %32
20: ; preds = %4
%21 = load i32, i32* %10
%22 = load i8*, i8** %8
%23 = load i32, i32* %9
%24 = call i64 @vdprintf(i32 %21, i8* %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i32, i32* %10
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %5
br label %32
29: ; preds = %20
%30 = load i32, i32* %10
%31 = call i32 @close(i32 %30)
store i32 0, i32* %5
br label %32
32: ; preds = %29, %26, %19
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @vspew(i32 %0, i8* %1, i8* %2, i32 %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 511)
%9 = icmp eq i32 %8, -1
br i1 %9, label %15, label %10
10: ; preds = %4
%11 = tail call i64 @vdprintf(i32 %8, i8* %2, i32 %3)
%12 = tail call i32 @close(i32 %8)
%13 = ashr i64 %11, 63
%14 = trunc i64 %13 to i32
br label %15
15: ; preds = %10, %4
%16 = phi i32 [ -1, %4 ], [ %14, %10 ]
ret i32 %16
}