// File: /workspace/github_c_makefile_star2_O3/Edilbert[P]BS9/bs9.c
// Function: WriteS19Format
void WriteS19Format(int i)
{
    unsigned char buf[80];
    FILE *bf;
    char *filename, *ExtPtr;
    int UnwrittenBytes,Addr,BytesInThisLine;
    const char *msg = "Write S19 file";

    filename = (char *)StrNDup(SFF[i],strlen(SFF[i]) + 4);
    ExtPtr = strrchr(filename, '.');
    if (!ExtPtr)
    {
       ExtPtr = filename + strlen(filename);
       memmove(ExtPtr, ".S19",5);
    }
    if (df)
    {
       fprintf(df,"Storing $%4.4x - $%4.4x <%s>\n",
                    SFA[i],SFA[i]+SFL[i],filename);
       if (ferror(df)) AssertFileOp(NULL, msg);
    }
    bf = AssertFileOp(fopen(filename, "wb"), msg);
    free(filename);

    // Write a S0 header which the TTL pseudo op should define
    memmove((char *)buf,"Bit Shift Assembler",20);
    WriteS19Line(bf, "S0", strlen((char *)buf), 0, buf);

    SFR[i] = 0;
    UnwrittenBytes = SFL[i];
    Addr = SFA[i];
    BytesInThisLine = 32;
    if (UnwrittenBytes < 32) BytesInThisLine = UnwrittenBytes;
    while (UnwrittenBytes > 0)
    {
       WriteS19Line(bf, "S1", BytesInThisLine, Addr, &ROM[Addr]);
       ++SFR[i];
       Addr += BytesInThisLine;
       UnwrittenBytes -= BytesInThisLine;
    }

    WriteS19Line(bf, "S5", 0, SFR[i], NULL);

    if (SFE[i] > -1) WriteS19Line(bf, "S9", 0, SFE[i], NULL);
    if (fclose(bf)) AssertFileOp(NULL, msg);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movslq %edi, %rbx
leaq 0x7aa33(%rip), %rax     # 0x85370
movq (%rax,%rbx,8), %r14
movq %r14, %rdi
callq 0x2110
leal 0x4(%rax), %esi
movq %r14, %rdi
callq 0x2878
movq %rax, %r14
movq %rax, %rdi
movl $0x2e, %esi
callq 0x2160
testq %rax, %rax
jne 0xa97f
movq %r14, %rdi
callq 0x2110
movl $0x3931532e, (%r14,%rax) # imm = 0x3931532E
movb $0x0, 0x4(%r14,%rax)
movq 0x1b08a(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0xa9c6
leaq 0x7a8ee(%rip), %rax     # 0x85280
movl (%rax,%rbx,4), %edx
leaq 0x7a934(%rip), %rax     # 0x852d0
movl (%rax,%rbx,4), %ecx
addl %edx, %ecx
leaq 0x2d4a(%rip), %rsi      # 0xd6f2
movq %r14, %r8
xorl %eax, %eax
callq 0x2210
movq 0x1b057(%rip), %rdi     # 0x25a10
callq 0x20b0
testl %eax, %eax
jne 0xab29
leaq 0x2d43(%rip), %rsi      # 0xd710
movq %r14, %rdi
callq 0x22b0
testq %rax, %rax
je 0xab1f
movq %rax, %r12
movq %r14, %rdi
callq 0x2040
movaps 0x3370(%rip), %xmm0     # 0xdd60
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movl $0x72656c, 0x10(%r15)   # imm = 0x72656C
movq %r15, %rdi
callq 0x2110
leaq 0x2d52(%rip), %rsi      # 0xd762
xorl %r14d, %r14d
movq %r12, %rdi
movl %eax, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0xa855
leaq 0x7d637(%rip), %r13     # 0x88060
movl $0x0, (%r13,%rbx,4)
leaq 0x7a897(%rip), %rax     # 0x852d0
movq %rbx, 0x8(%rsp)
movl (%rax,%rbx,4), %ebx
cmpl $0x20, %ebx
movl $0x20, %eax
movq %rax, (%rsp)
movl $0x20, %ebp
cmovll %ebx, %ebp
testl %ebx, %ebx
movq %r12, %rdx
jle 0xaac3
leaq 0x7a81d(%rip), %rax     # 0x85280
cmpl $0x20, %ebx
movq (%rsp), %rcx
cmovbl %ebx, %ecx
movq %rcx, (%rsp)
movq 0x8(%rsp), %rcx
movslq (%rax,%rcx,4), %r15
leaq 0xa1cf(%rip), %r12      # 0x14c50
addq %r15, %r12
movq %rdx, %r14
movq %rdx, %rdi
leaq 0x2cd4(%rip), %rsi      # 0xd765
movl %ebp, %edx
movl %r15d, %ecx
movq %r12, %r8
callq 0xa855
movq %r14, %rdx
movq 0x8(%rsp), %rax
movl (%r13,%rax,4), %r14d
incl %r14d
movl %r14d, (%r13,%rax,4)
subl %ebp, %ebx
movq (%rsp), %rax
addl %eax, %r15d
addq %rax, %r12
testl %ebx, %ebx
jg 0xaa84
leaq 0x2c9e(%rip), %rsi      # 0xd768
movq %rdx, %rbx
movq %rdx, %rdi
xorl %edx, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0xa855
leaq 0x7a83c(%rip), %rax     # 0x85320
movq 0x8(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
testl %ecx, %ecx
js 0xab04
leaq 0x2c74(%rip), %rsi      # 0xd76b
movq %rbx, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xa855
movq %rbx, %rdi
callq 0x2100
testl %eax, %eax
jne 0xab24
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2511
callq 0x24fc
callq 0x24e7