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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_coff_header(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ write_dos_header_and_stub(int /*<<< orphan*/ *);
int /*<<< orphan*/ write_pe_signature(int /*<<< orphan*/ *);
void write_simple_dll(const char *name, uint16_t machine) {
  FILE *file = fopen(name, "w");
  if (file == NULL) {
    fprintf(stderr, "error: unable to open file for writing\n");
    exit(3);
  }
  write_dos_header_and_stub(file);
  write_pe_signature(file);
  write_coff_header(file, machine);
  fclose(file);
  file = NULL;
  printf("wrote %s\n", name);
}


x86 assembly (clang, -O0):
<write_simple_dll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <write_simple_dll+0x4d>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1060 <write_dos_header_and_stub@plt>
mov    -0x18(%rbp),%rdi
call   1050 <write_pe_signature@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <write_coff_header@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_simple_dll>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xe6f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d4 <write_simple_dll+0x54>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <write_dos_header_and_stub@plt>
mov    %rbp,%rdi
call   1050 <write_pe_signature@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1090 <write_coff_header@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe62(%rip),%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <printf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"error: unable to open file for writing\0A\00"
@.str.2 = [10 x i8] c"wrote %s\0A\00"
define void @write_simple_dll(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 3)
unreachable
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = call i32 @write_dos_header_and_stub(i32* %15)
%17 = load i32*, i32** %5
%18 = call i32 @write_pe_signature(i32* %17)
%19 = load i32*, i32** %5
%20 = load i32, i32* %4
%21 = call i32 @write_coff_header(i32* %19, i32 %20)
%22 = load i32*, i32** %5
%23 = call i32 @fclose(i32* %22)
store i32* null, i32** %5
%24 = load i8*, i8** %3
%25 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %24)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"error: unable to open file for writing\0A\00"
@.str.2 = [10 x i8] c"wrote %s\0A\00"
define void @write_simple_dll(i8* %0, i32 %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 3)
unreachable
9: ; preds = %2
%10 = tail call i32 @write_dos_header_and_stub(i32* %3)
%11 = tail call i32 @write_pe_signature(i32* %3)
%12 = tail call i32 @write_coff_header(i32* %3, i32 %1)
%13 = tail call i32 @fclose(i32* %3)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %0)
ret void
}