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

/* Variables and functions */
scalar_t__ EOF;
int asprintf(char **, char *, char const *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int file_write_value(const char *path, const char *name, const char *format,
                     ...) {
  FILE *file;
  char *rpath;
  int ret;
  va_list value;
  ret = asprintf(&rpath, "%s/%s", path, name);
  if (ret < 0)
    return ret;
  file = fopen(rpath, "w");
  if (!file) {
    ret = -1;
    goto out_free;
  }
  ret = fprintf(file, format, value) == EOF ? -1 : 0;
  fclose(file);
out_free:
  free(rpath);
  return ret;
}


x86 assembly (clang, -O0):
<file_write_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <asprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1194 <file_write_value+0x44>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <file_write_value+0xb4>
mov    -0x30(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bf <file_write_value+0x6f>
movl   $0xffffffff,-0x34(%rbp)
jmp    11f5 <file_write_value+0xa5>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1050 <fprintf@plt>
mov    %rax,%rdx
mov    0x2e0f(%rip),%rax
mov    (%rax),%rsi
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write_value>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0xe97(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
test   %eax,%eax
js     11b6 <file_write_value+0x66>
mov    0x8(%rsp),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ba <file_write_value+0x6a>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <fprintf@plt>
mov    0x2e46(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
sete   %bl
neg    %ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    11bf <file_write_value+0x6f>
mov    %eax,%ebx
jmp    11c9 <file_write_value+0x79>
mov    $0xffffffff,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i64 0
define i32 @file_write_value(i8* %0, i8* %1, i8* nocapture %2, ...) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%7 = icmp slt i32 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %3
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %18, label %12
12: ; preds = %8
%13 = call i64 @fprintf(i32* %10, i8* %2, i32 undef)
%14 = load i64, i64* @EOF
%15 = icmp eq i64 %13, %14
%16 = sext i1 %15 to i32
%17 = call i32 @fclose(i32* %10)
br label %18
18: ; preds = %8, %12
%19 = phi i32 [ %16, %12 ], [ -1, %8 ]
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
br label %22
22: ; preds = %3, %18
%23 = phi i32 [ %19, %18 ], [ %6, %3 ]
ret i32 %23
}