#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 EOF;
int /*<<< orphan*/ category_file();
int /*<<< orphan*/ errf(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fwrite(void *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int wr_category(void *buf, size_t sz, FILE *f) {
  if (!sz) {
    return (0);
  }
  if (fwrite(buf, sz, 1, f) < 1) {
    (void)fclose(f);
    (void)unlink(category_file());
    errf(strerror(errno));
    return (EOF);
  }
  return (0);
}


x86 assembly (clang, -O0):
<wr_category>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118b <wr_category+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    11ee <wr_category+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jge    11e7 <wr_category+0x87>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1050 <category_file@plt>
mov    %eax,%edi
call   1030 <unlink@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edi
call   1060 <errf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <wr_category+0x8e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wr_category>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rsi,%rsi
je     11b3 <wr_category+0x53>
mov    %rdx,%r14
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
test   %eax,%eax
jg     11b3 <wr_category+0x53>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
call   1050 <category_file@plt>
mov    %eax,%edi
call   1030 <unlink@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edi
call   1060 <errf@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i32 0
@EOF = global i32 0
define i32 @wr_category(i8* %0, i64 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
%8 = load i64, i64* %6
%9 = icmp ne i64 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %3
store i32 0, i32* %4
br label %27
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = load i64, i64* %6
%14 = load i32*, i32** %7
%15 = call i32 @fwrite(i8* %12, i64 %13, i32 1, i32* %14)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %26
17: ; preds = %11
%18 = load i32*, i32** %7
%19 = call i32 @fclose(i32* %18)
%20 = call i32 (...) @category_file()
%21 = call i32 @unlink(i32 %20)
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 @errf(i32 %23)
%25 = load i32, i32* @EOF
store i32 %25, i32* %4
br label %27
26: ; preds = %11
store i32 0, i32* %4
br label %27
27: ; preds = %26, %17, %10
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@errno = global i32 0
@EOF = global i32 0
define i32 @wr_category(i8* nocapture %0, i64 %1, i32* nocapture %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %16, label %5
5: ; preds = %3
%6 = tail call i32 @fwrite(i8* %0, i64 %1, i32 1, i32* %2)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = tail call i32 @fclose(i32* %2)
%10 = tail call i32 (...) @category_file()
%11 = tail call i32 @unlink(i32 %10)
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 @errf(i32 %13)
%15 = load i32, i32* @EOF
br label %16
16: ; preds = %5, %3, %8
%17 = phi i32 [ %15, %8 ], [ 0, %3 ], [ 0, %5 ]
ret i32 %17
}