#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 */
struct mustach_itf {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fmustach(char const *, struct mustach_itf *, void *, int /*<<< orphan*/ *);
scalar_t__ fputc(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int mustach(const char *template, struct mustach_itf *itf, void *closure,
            char **result, size_t *size) {
  int rc;
  FILE *file;
  size_t s;
  *result = NULL;
  if (size == NULL)
    size = &s;
  file = open_memstream(result, size);
  if (file == NULL) {
    rc = -1;
    errno = ENOMEM;
  } else {
    rc = fmustach(template, itf, closure, file);
    if (rc == 0)
      rc = fputc(0, file) ? -1 : 0;
    fclose(file);
    if (rc >= 0)
      (*size)--;
    else {
      free(*result);
      *result = NULL;
      *size = 0;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<mustach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
jne    118a <mustach+0x3a>
lea    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c4 <mustach+0x74>
movl   $0xffffffff,-0x2c(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1b(%rip),%rax
mov    %ecx,(%rax)
jmp    1252 <mustach+0x102>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1070 <fmustach@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1205 <mustach+0xb5>
mov    -0x38(%rbp),%rsi
xor    %edi,%edi
call   1060 <fputc@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x2c(%rbp)
jl     122b <mustach+0xdb>
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,(%rax)
jmp    124d <mustach+0xfd>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
jmp    1252 <mustach+0x102>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mustach>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbp
movq   $0x0,(%rcx)
test   %r8,%r8
mov    %rsp,%r15
cmovne %r8,%r15
mov    %rcx,%rdi
mov    %r15,%rsi
call   1040 <open_memstream@plt>
test   %rax,%rax
je     11c3 <mustach+0x73>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %rax,%rcx
call   1070 <fmustach@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11b1 <mustach+0x61>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
neg    %rax
sbb    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %ebp,%ebp
js     11dc <mustach+0x8c>
addq   $0xffffffffffffffff,(%r15)
jmp    11f2 <mustach+0xa2>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    0x2e05(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
jmp    11f2 <mustach+0xa2>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
movq   $0x0,(%r15)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mustach_itf = type { i32 }
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @mustach(i8* %0, %struct.mustach_itf* %1, i8* %2, i8** %3, i64* %4) {
%6 = alloca i8*
%7 = alloca %struct.mustach_itf*
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i64*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i64
store i8* %0, i8** %6
store %struct.mustach_itf* %1, %struct.mustach_itf** %7
store i8* %2, i8** %8
store i8** %3, i8*** %9
store i64* %4, i64** %10
%14 = load i8**, i8*** %9
store i8* null, i8** %14
%15 = load i64*, i64** %10
%16 = icmp eq i64* %15, null
br i1 %16, label %17, label %18
17: ; preds = %5
store i64* %13, i64** %10
br label %18
18: ; preds = %17, %5
%19 = load i8**, i8*** %9
%20 = load i64*, i64** %10
%21 = call i32* @open_memstream(i8** %19, i64* %20)
store i32* %21, i32** %12
%22 = load i32*, i32** %12
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %18
store i32 -1, i32* %11
%25 = load i32, i32* @ENOMEM
store i32 %25, i32* @errno
br label %56
26: ; preds = %18
%27 = load i8*, i8** %6
%28 = load %struct.mustach_itf*, %struct.mustach_itf** %7
%29 = load i8*, i8** %8
%30 = load i32*, i32** %12
%31 = call i32 @fmustach(i8* %27, %struct.mustach_itf* %28, i8* %29, i32* %30)
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %26
%35 = load i32*, i32** %12
%36 = call i64 @fputc(i32 0, i32* %35)
%37 = icmp ne i64 %36, 0
%38 = zext i1 %37 to i64
%39 = select i1 %37, i32 -1, i32 0
store i32 %39, i32* %11
br label %40
40: ; preds = %34, %26
%41 = load i32*, i32** %12
%42 = call i32 @fclose(i32* %41)
%43 = load i32, i32* %11
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = load i64*, i64** %10
%47 = load i64, i64* %46
%48 = add i64 %47, -1
store i64 %48, i64* %46
br label %55
49: ; preds = %40
%50 = load i8**, i8*** %9
%51 = load i8*, i8** %50
%52 = call i32 @free(i8* %51)
%53 = load i8**, i8*** %9
store i8* null, i8** %53
%54 = load i64*, i64** %10
store i64 0, i64* %54
br label %55
55: ; preds = %49, %45
br label %56
56: ; preds = %55, %24
%57 = load i32, i32* %11
ret i32 %57
}

LLVM IR (-O2):
%struct.mustach_itf = type { i32 }
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @mustach(i8* %0, %struct.mustach_itf* %1, i8* %2, i8** %3, i64* %4) {
%6 = alloca i64
%7 = bitcast i64* %6 to i8*
store i8* null, i8** %3
%8 = icmp eq i64* %4, null
%9 = select i1 %8, i64* %6, i64* %4
%10 = call i32* @open_memstream(i8** %3, i64* %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = load i32, i32* @ENOMEM
store i32 %13, i32* @errno
br label %31
14: ; preds = %5
%15 = call i32 @fmustach(i8* %0, %struct.mustach_itf* %1, i8* %2, i32* %10)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i64 @fputc(i32 0, i32* %10)
%19 = icmp ne i64 %18, 0
%20 = sext i1 %19 to i32
br label %21
21: ; preds = %17, %14
%22 = phi i32 [ %20, %17 ], [ %15, %14 ]
%23 = call i32 @fclose(i32* %10)
%24 = icmp sgt i32 %22, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i64, i64* %9
%27 = add i64 %26, -1
store i64 %27, i64* %9
br label %31
28: ; preds = %21
%29 = load i8*, i8** %3
%30 = call i32 @free(i8* %29)
store i8* null, i8** %3
store i64 0, i64* %9
br label %31
31: ; preds = %25, %28, %12
%32 = phi i32 [ -1, %12 ], [ %22, %25 ], [ %22, %28 ]
ret i32 %32
}