// File: /workspace/github_c_makefile_star2_O2/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, %rcx
leaq 0x7abf8(%rip), %rax     # 0x84370
movq %rcx, (%rsp)
movq (%rax,%rcx,8), %rbx
movq %rbx, %rdi
callq 0x2110
leal 0x4(%rax), %esi
movq %rbx, %rdi
callq 0x258f
movq %rax, %r14
pushq $0x2e
popq %rsi
movq %rax, %rdi
callq 0x2160
testq %rax, %rax
jne 0x97bc
movq %r14, %rdi
callq 0x2110
movl $0x3931532e, (%r14,%rax) # imm = 0x3931532E
movb $0x0, 0x4(%r14,%rax)
movq 0x1b24d(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x9811
leaq 0x7aab1(%rip), %rax     # 0x84280
movq (%rsp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x7aaf3(%rip), %rax     # 0x842d0
movl (%rax,%rcx,4), %ecx
addl %edx, %ecx
leaq 0x2e99(%rip), %rsi      # 0xc682
movq %r14, %r8
xorl %eax, %eax
callq 0x2210
movq 0x1b216(%rip), %rdi     # 0x24a10
callq 0x20b0
testl %eax, %eax
je 0x9811
leaq 0x2ed4(%rip), %rsi      # 0xc6de
xorl %edi, %edi
callq 0x2575
leaq 0x2e88(%rip), %rsi      # 0xc6a0
movq %r14, %rdi
callq 0x22b0
movq %rax, %rbx
leaq 0x2eb4(%rip), %rsi      # 0xc6de
movq %rax, %rdi
callq 0x2575
movq %r14, %rdi
callq 0x2040
movaps 0x34af(%rip), %xmm0     # 0xccf0
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
movl $0x72656c, 0x10(%r14)   # imm = 0x72656C
movq %r14, %rdi
callq 0x2110
leaq 0x2e91(%rip), %rsi      # 0xc6f2
xorl %ebp, %ebp
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movl %eax, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x965a
leaq 0x7d7e2(%rip), %rbx     # 0x87060
movq (%rsp), %rcx
andl $0x0, (%rbx,%rcx,4)
leaq 0x7aa43(%rip), %rax     # 0x842d0
movl (%rax,%rcx,4), %r13d
leaq 0x7a9e8(%rip), %rax     # 0x84280
movslq (%rax,%rcx,4), %r14
cmpl $0x20, %r13d
pushq $0x20
popq %rax
cmovll %r13d, %eax
movslq %eax, %r15
leaq 0xa39f(%rip), %r12      # 0x13c50
addq %r14, %r12
testl %r13d, %r13d
jle 0x98ea
movq 0x8(%rsp), %rdi
leaq 0x2e30(%rip), %rsi      # 0xc6f5
movl %r15d, %edx
movl %r14d, %ecx
movq %r12, %r8
callq 0x965a
movq (%rsp), %rax
movl (%rbx,%rax,4), %ebp
incl %ebp
movl %ebp, (%rbx,%rax,4)
subl %r15d, %r13d
addq %r15, %r12
addl %r15d, %r14d
jmp 0x98b4
leaq 0x2e07(%rip), %rsi      # 0xc6f8
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x965a
leaq 0x7aa14(%rip), %rax     # 0x84320
movq (%rsp), %rcx
movl (%rax,%rcx,4), %ecx
testl %ecx, %ecx
js 0x992b
leaq 0x2ddd(%rip), %rsi      # 0xc6fb
movq %rbx, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x965a
movq %rbx, %rdi
callq 0x2100
testl %eax, %eax
je 0x9953
leaq 0x2da0(%rip), %rsi      # 0xc6de
xorl %edi, %edi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2575
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq