#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;
typedef int ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ close(int);
int /*<<< orphan*/ die(char *, ...);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int vsnprintf(char *, int, char *, int /*<<< orphan*/);
int write(int, char *, int);
void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap) {
  char buf[4096];
  int fd;
  ssize_t written;
  int buf_len;
  buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
  if (buf_len < 0) {
    die("vsnprintf failed: %s\n", strerror(errno));
  }
  if (buf_len >= sizeof(buf)) {
    die("vsnprintf output truncated\n");
  }
  fd = open(filename, O_WRONLY);
  if (fd < 0) {
    if ((errno == ENOENT) && enoent_ok)
      return;
    die("open of %s failed: %s\n", filename, strerror(errno));
  }
  written = write(fd, buf, buf_len);
  if (written != buf_len) {
    if (written >= 0) {
      die("short write to %s\n", filename);
    } else {
      die("write to %s failed: %s\n", filename, strerror(errno));
    }
  }
  if (close(fd) != 0) {
    die("close of %s failed: %s\n", filename, strerror(errno));
  }
}


x86 assembly (clang, -O0):
<vmaybe_write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x1000,%esi
call   1030 <vsnprintf@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0x0,-0x102c(%rbp)
jge    11c3 <vmaybe_write_file+0x63>
mov    0x2e35(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
movslq -0x102c(%rbp),%rax
cmp    $0x1000,%rax
jb     11e4 <vmaybe_write_file+0x84>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    1263 <vmaybe_write_file+0x103>
mov    0x2dd0(%rip),%rax
mov    (%rax),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
jne    1232 <vmaybe_write_file+0xd2>
cmpl   $0x0,-0x4(%rbp)
je     1232 <vmaybe_write_file+0xd2>
jmp    1333 <vmaybe_write_file+0x1d3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x1038(%rbp),%rsi
mov    %eax,%edx
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x1024(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x102c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x1028(%rbp)
mov    -0x1028(%rbp),%eax
cmp    -0x102c(%rbp),%eax
je     12ed <vmaybe_write_file+0x18d>
cmpl   $0x0,-0x1028(%rbp)
jl     12b7 <vmaybe_write_file+0x157>
mov    -0x10(%rbp),%rsi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
jmp    12e8 <vmaybe_write_file+0x188>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    0x2d17(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x1040(%rbp),%rsi
mov    %eax,%edx
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
jmp    12ed <vmaybe_write_file+0x18d>
mov    -0x1024(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     1333 <vmaybe_write_file+0x1d3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    0x2ccc(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x1048(%rbp),%rsi
mov    %eax,%edx
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmaybe_write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1030 <vsnprintf@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1190 <vmaybe_write_file+0x30>
cmp    $0x1000,%ebp
jae    11af <vmaybe_write_file+0x4f>
jmp    11bd <vmaybe_write_file+0x5d>
mov    0x2e49(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xe5a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <die@plt>
lea    0xe60(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    120b <vmaybe_write_file+0xab>
mov    0x2e05(%rip),%rax
mov    (%rax),%rdi
test   %r15d,%r15d
je     11f3 <vmaybe_write_file+0x93>
mov    0x2de6(%rip),%rax
cmp    (%rax),%rdi
je     128f <vmaybe_write_file+0x12f>
call   1080 <strerror@plt>
lea    0xe33(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <die@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     1254 <vmaybe_write_file+0xf4>
test   %eax,%eax
js     1232 <vmaybe_write_file+0xd2>
lea    0xe23(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <die@plt>
jmp    1254 <vmaybe_write_file+0xf4>
mov    0x2da7(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xe14(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <die@plt>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
je     128f <vmaybe_write_file+0x12f>
mov    0x2d79(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xdfe(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <die@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"vsnprintf failed: %s\0A\00"
@errno = global i64 0
@.str.1 = [28 x i8] c"vsnprintf output truncated\0A\00"
@O_WRONLY = global i32 0
@ENOENT = global i64 0
@.str.2 = [23 x i8] c"open of %s failed: %s\0A\00"
@.str.3 = [19 x i8] c"short write to %s\0A\00"
@.str.4 = [24 x i8] c"write to %s failed: %s\0A\00"
@.str.5 = [24 x i8] c"close of %s failed: %s\0A\00"
define void @vmaybe_write_file(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = call i32 @vsnprintf(i8* %13, i32 4096, i8* %14, i32 %15)
store i32 %16, i32* %12
%17 = load i32, i32* %12
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %4
%20 = load i64, i64* @errno
%21 = call i32 @strerror(i64 %20)
%22 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %4
%24 = load i32, i32* %12
%25 = sext i32 %24 to i64
%26 = icmp uge i64 %25, 4096
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = load i8*, i8** %6
%31 = load i32, i32* @O_WRONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %29
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOENT
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* %5
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
br label %77
43: ; preds = %39, %35
%44 = load i8*, i8** %6
%45 = load i64, i64* @errno
%46 = call i32 @strerror(i64 %45)
%47 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %44, i32 %46)
br label %48
48: ; preds = %43, %29
%49 = load i32, i32* %10
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%51 = load i32, i32* %12
%52 = call i32 @write(i32 %49, i8* %50, i32 %51)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = load i32, i32* %12
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %68
56: ; preds = %48
%57 = load i32, i32* %11
%58 = icmp sge i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i8*, i8** %6
%61 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %60)
br label %67
62: ; preds = %56
%63 = load i8*, i8** %6
%64 = load i64, i64* @errno
%65 = call i32 @strerror(i64 %64)
%66 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %63, i32 %65)
br label %67
67: ; preds = %62, %59
br label %68
68: ; preds = %67, %48
%69 = load i32, i32* %10
%70 = call i64 @close(i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %68
%73 = load i8*, i8** %6
%74 = load i64, i64* @errno
%75 = call i32 @strerror(i64 %74)
%76 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %73, i32 %75)
br label %77
77: ; preds = %42, %72, %68
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"vsnprintf failed: %s\0A\00"
@errno = global i64 0
@.str.1 = [28 x i8] c"vsnprintf output truncated\0A\00"
@O_WRONLY = global i32 0
@ENOENT = global i64 0
@.str.2 = [23 x i8] c"open of %s failed: %s\0A\00"
@.str.3 = [19 x i8] c"short write to %s\0A\00"
@.str.4 = [24 x i8] c"write to %s failed: %s\0A\00"
@.str.5 = [24 x i8] c"close of %s failed: %s\0A\00"
define void @vmaybe_write_file(i32 %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = call i32 @vsnprintf(i8* %6, i32 4096, i8* %2, i32 %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = load i64, i64* @errno
%11 = tail call i32 @strerror(i64 %10)
%12 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %15
13: ; preds = %4
%14 = icmp ugt i32 %7, 4095
br i1 %14, label %15, label %17
15: ; preds = %9, %13
%16 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
%18 = load i32, i32* @O_WRONLY
%19 = tail call i32 @open(i8* %1, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @ENOENT
%24 = icmp eq i64 %22, %23
%25 = icmp ne i32 %0, 0
%26 = and i1 %25, %24
br i1 %26, label %48, label %27
27: ; preds = %21
%28 = tail call i32 @strerror(i64 %22)
%29 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %28)
br label %30
30: ; preds = %27, %17
%31 = call i32 @write(i32 %19, i8* %6, i32 %7)
%32 = icmp eq i32 %31, %7
br i1 %32, label %41, label %33
33: ; preds = %30
%34 = icmp sgt i32 %31, -1
br i1 %34, label %35, label %37
35: ; preds = %33
%36 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %41
37: ; preds = %33
%38 = load i64, i64* @errno
%39 = tail call i32 @strerror(i64 %38)
%40 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32 %39)
br label %41
41: ; preds = %35, %37, %30
%42 = tail call i64 @close(i32 %19)
%43 = icmp eq i64 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %41
%45 = load i64, i64* @errno
%46 = tail call i32 @strerror(i64 %45)
%47 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %1, i32 %46)
br label %48
48: ; preds = %41, %44, %21
ret void
}