#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fputc(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int memfile_close(FILE *file, char **buffer, size_t *size) {
  int rc;
  rc = fputc(0, file) ? -1 : 0;
  fclose(file);
  if (rc == 0)
    (*size)--;
  else {
    free(*buffer);
    *buffer = NULL;
    *size = 0;
  }
  return rc;
}


x86 assembly (clang, -O0):
<memfile_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
xor    %edi,%edi
call   1050 <fputc@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1189 <memfile_close+0x59>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,(%rax)
jmp    11ab <memfile_close+0x7b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memfile_close>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r12,%r12
je     116e <memfile_close+0x3e>
mov    (%r15),%rdi
call   1030 <free@plt>
movq   $0x0,(%r15)
xor    %eax,%eax
jmp    1175 <memfile_close+0x45>
mov    (%r14),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,(%r14)
neg    %r12
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @memfile_close(i32* %0, i8** %1, i64* %2) {
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
%8 = load i32*, i32** %4
%9 = call i64 @fputc(i32 0, i32* %8)
%10 = icmp ne i64 %9, 0
%11 = zext i1 %10 to i64
%12 = select i1 %10, i32 -1, i32 0
store i32 %12, i32* %7
%13 = load i32*, i32** %4
%14 = call i32 @fclose(i32* %13)
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i64*, i64** %6
%19 = load i64, i64* %18
%20 = add i64 %19, -1
store i64 %20, i64* %18
br label %27
21: ; preds = %3
%22 = load i8**, i8*** %5
%23 = load i8*, i8** %22
%24 = call i32 @free(i8* %23)
%25 = load i8**, i8*** %5
store i8* null, i8** %25
%26 = load i64*, i64** %6
store i64 0, i64* %26
br label %27
27: ; preds = %21, %17
%28 = load i32, i32* %7
ret i32 %28
}

LLVM IR (-O2):
define i32 @memfile_close(i32* nocapture %0, i8** nocapture %1, i64* nocapture %2) {
%4 = tail call i64 @fputc(i32 0, i32* %0)
%5 = icmp ne i64 %4, 0
%6 = tail call i32 @fclose(i32* %0)
br i1 %5, label %10, label %7
7: ; preds = %3
%8 = load i64, i64* %2
%9 = add i64 %8, -1
br label %13
10: ; preds = %3
%11 = load i8*, i8** %1
%12 = tail call i32 @free(i8* %11)
store i8* null, i8** %1
br label %13
13: ; preds = %10, %7
%14 = phi i64 [ 0, %10 ], [ %9, %7 ]
store i64 %14, i64* %2
%15 = sext i1 %5 to i32
ret i32 %15
}