#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 */
unsigned int _LEN(unsigned char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
unsigned int fwrite(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ stderr;
int wrt_24section(unsigned char *section, char *file_name) {
  unsigned int size;
  FILE *output;
  if (section == NULL) {
    remove(file_name);
    return 0;
  }
  size = _LEN(section);
  if ((output = fopen(file_name, "wb")) == NULL) {
    fprintf(stderr, "could not open %s for output\n", file_name);
    exit(8);
  }
  if (fwrite(section, sizeof(unsigned char), size, output) != size) {
    fprintf(stderr, "error writing %s\n", file_name);
    fclose(output);
    exit(8);
  }
  fclose(output);
  return 0;
}


x86 assembly (clang, -O0):
<wrt_24section>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a0 <wrt_24section+0x30>
mov    -0x18(%rbp),%rdi
call   1030 <remove@plt>
movl   $0x0,-0x4(%rbp)
jmp    1247 <wrt_24section+0xd7>
mov    -0x10(%rbp),%rdi
call   1050 <_LEN@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ed <wrt_24section+0x7d>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe25(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x8,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x1c(%rbp),%eax
je     1237 <wrt_24section+0xc7>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe02(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x8,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wrt_24section>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     11c0 <wrt_24section+0x50>
mov    %rdi,%rbx
call   1050 <_LEN@plt>
mov    %eax,%ebp
lea    0xe70(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d5 <wrt_24section+0x65>
mov    %rax,%r15
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
cmp    %ebp,%eax
jne    11f7 <wrt_24section+0x87>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    11c8 <wrt_24section+0x58>
mov    %r14,%rdi
call   1030 <remove@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x8,%edi
call   1080 <exit@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0x8,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"could not open %s for output\0A\00"
@.str.2 = [18 x i8] c"error writing %s\0A\00"
define i32 @wrt_24section(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = call i32 @remove(i8* %11)
store i32 0, i32* %3
br label %41
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @_LEN(i8* %14)
store i32 %15, i32* %6
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 8)
unreachable
24: ; preds = %13
%25 = load i8*, i8** %4
%26 = load i32, i32* %6
%27 = load i32*, i32** %7
%28 = call i32 @fwrite(i8* %25, i32 1, i32 %26, i32* %27)
%29 = load i32, i32* %6
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %38
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %5
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32*, i32** %7
%36 = call i32 @fclose(i32* %35)
%37 = call i32 @exit(i32 8)
unreachable
38: ; preds = %24
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
store i32 0, i32* %3
br label %41
41: ; preds = %38, %10
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"could not open %s for output\0A\00"
@.str.2 = [18 x i8] c"error writing %s\0A\00"
define i32 @wrt_24section(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @remove(i8* %1)
br label %24
6: ; preds = %2
%7 = tail call i32 @_LEN(i8* %0)
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%13 = tail call i32 @exit(i32 8)
unreachable
14: ; preds = %6
%15 = tail call i32 @fwrite(i8* %0, i32 1, i32 %7, i32* %8)
%16 = icmp eq i32 %15, %7
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%20 = tail call i32 @fclose(i32* %8)
%21 = tail call i32 @exit(i32 8)
unreachable
22: ; preds = %14
%23 = tail call i32 @fclose(i32* %8)
br label %24
24: ; preds = %22, %4
ret i32 0
}