// File: /workspace/github_c_makefile_star2_O1/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, %r15
leaq 0x7aadd(%rip), %rax     # 0x85370
movq (%rax,%r15,8), %rbx
movq %rbx, %rdi
callq 0x2110
leal 0x4(%rax), %esi
movq %rbx, %rdi
callq 0x2878
movq %rax, %r14
movq %rax, %rdi
movl $0x2e, %esi
callq 0x2160
testq %rax, %rax
jne 0xa8d5
movq %r14, %rdi
callq 0x2110
movl $0x3931532e, (%r14,%rax) # imm = 0x3931532E
movb $0x0, 0x4(%r14,%rax)
movq 0x1b134(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0xa91e
leaq 0x7a998(%rip), %rax     # 0x85280
movl (%rax,%r15,4), %edx
leaq 0x7a9dd(%rip), %rax     # 0x852d0
movl (%rax,%r15,4), %ecx
addl %edx, %ecx
leaq 0x2e0c(%rip), %rsi      # 0xd70c
movq %r14, %r8
xorl %eax, %eax
callq 0x2210
movq 0x1b0ff(%rip), %rdi     # 0x25a10
callq 0x20b0
testl %eax, %eax
jne 0xaa6a
leaq 0x2e05(%rip), %rsi      # 0xd72a
movq %r14, %rdi
callq 0x22b0
testq %rax, %rax
je 0xaa60
movq %rax, %r12
movq %r14, %rdi
callq 0x2040
movaps 0x3458(%rip), %xmm0     # 0xdda0
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
movl $0x72656c, 0x10(%r14)   # imm = 0x72656C
movq %r14, %rdi
callq 0x2110
leaq 0x2e14(%rip), %rsi      # 0xd77c
movq %r12, %rdi
movl %eax, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0xa7ab
leaq 0x7d6e2(%rip), %rbp     # 0x88060
movl $0x0, (%rbp,%r15,4)
leaq 0x7a942(%rip), %rax     # 0x852d0
movslq (%rax,%r15,4), %r13
cmpl $0x20, %r13d
movl $0x20, %ebx
cmovll %r13d, %ebx
testq %r13, %r13
movq %r15, (%rsp)
jle 0xaa05
leaq 0x7a8d1(%rip), %rax     # 0x85280
cmpq $0x20, %r13
movl $0x20, %ecx
cmovlq %r13, %rcx
movq %rcx, 0x8(%rsp)
movq (%rsp), %rcx
movslq (%rax,%rcx,4), %r14
leaq 0xa280(%rip), %r15      # 0x14c50
addq %r14, %r15
movq %r12, %rdi
leaq 0x2da2(%rip), %rsi      # 0xd77f
movl %ebx, %edx
movl %r14d, %ecx
movq %r15, %r8
callq 0xa7ab
movq (%rsp), %rax
incl (%rbp,%rax,4)
subl %ebx, %r13d
movq 0x8(%rsp), %rax
addl %eax, %r14d
addq %rax, %r15
testl %r13d, %r13d
jg 0xa9d3
movq (%rsp), %r14
movl (%rbp,%r14,4), %ecx
leaq 0x2d6d(%rip), %rsi      # 0xd782
movq %r12, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xa7ab
leaq 0x7a8f7(%rip), %rax     # 0x85320
movl (%rax,%r14,4), %ecx
testl %ecx, %ecx
js 0xaa45
leaq 0x2d4d(%rip), %rsi      # 0xd785
movq %r12, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0xa7ab
movq %r12, %rdi
callq 0x2100
testl %eax, %eax
jne 0xaa65
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2511
callq 0x24fc
callq 0x24e7