// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xa8, %rsp
movl %edi, 0xa4(%rsp)
leaq 0x2f51(%rip), %rax      # 0x116c6
movq %rax, 0x20(%rsp)
movslq 0xa4(%rsp), %rcx
leaq 0x79bc7(%rip), %rax     # 0x88350
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movslq 0xa4(%rsp), %rcx
leaq 0x79baf(%rip), %rax     # 0x88350
movq (%rax,%rcx,8), %rdi
callq 0x20f0
movq 0x18(%rsp), %rdi
addq $0x4, %rax
movl %eax, %esi
callq 0x26b0
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
movl $0x2e, %esi
callq 0x2140
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0xe815
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %rdi
callq 0x20f0
movq %rax, %rcx
movq 0x10(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movl 0x2ecb(%rip), %ecx      # 0x116d5
movb 0x2ec9(%rip), %dl       # 0x116d9
movb %dl, 0x4(%rax)
movl %ecx, (%rax)
cmpq $0x0, 0x1a1d3(%rip)     # 0x289f0
je 0xe890
movq 0x1a1ca(%rip), %rdi     # 0x289f0
movslq 0xa4(%rsp), %rcx
leaq 0x79a2b(%rip), %rax     # 0x88260
movl (%rax,%rcx,4), %edx
movslq 0xa4(%rsp), %rcx
leaq 0x79a19(%rip), %rax     # 0x88260
movl (%rax,%rcx,4), %ecx
movslq 0xa4(%rsp), %rsi
leaq 0x79a57(%rip), %rax     # 0x882b0
addl (%rax,%rsi,4), %ecx
movq 0x40(%rsp), %r8
leaq 0x2e02(%rip), %rsi      # 0x1166a
movb $0x0, %al
callq 0x21e0
movq 0x1a17a(%rip), %rdi     # 0x289f0
callq 0x2090
cmpl $0x0, %eax
je 0xe88e
movq 0x20(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x2670
jmp 0xe890
movq 0x40(%rsp), %rdi
leaq 0x2dec(%rip), %rsi      # 0x11688
callq 0x2270
movq %rax, %rdi
movq 0x20(%rsp), %rsi
callq 0x2670
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rdi
callq 0x2030
leaq 0x50(%rsp), %rax
movups 0x2e11(%rip), %xmm0     # 0x116da
movl 0x2e1b(%rip), %ecx      # 0x116ea
movl %ecx, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x48(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x20f0
movq 0x8(%rsp), %rdi
movl %eax, %edx
leaq 0x50(%rsp), %r8
leaq 0x2df2(%rip), %rsi      # 0x116ee
xorl %ecx, %ecx
callq 0xe610
movslq 0xa4(%rsp), %rcx
leaq 0x7c72e(%rip), %rax     # 0x8b040
movl $0x0, (%rax,%rcx,4)
movslq 0xa4(%rsp), %rcx
leaq 0x79988(%rip), %rax     # 0x882b0
movl (%rax,%rcx,4), %eax
movl %eax, 0x34(%rsp)
movslq 0xa4(%rsp), %rcx
leaq 0x79922(%rip), %rax     # 0x88260
movl (%rax,%rcx,4), %eax
movl %eax, 0x30(%rsp)
movl $0x20, 0x2c(%rsp)
cmpl $0x20, 0x34(%rsp)
jge 0xe95c
movl 0x34(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0xe95e
cmpl $0x0, 0x34(%rsp)
jle 0xe9c8
movq 0x48(%rsp), %rdi
movl 0x2c(%rsp), %edx
movl 0x30(%rsp), %ecx
movslq 0x30(%rsp), %rax
leaq 0x92b2(%rip), %r8       # 0x17c30
addq %rax, %r8
leaq 0x2d69(%rip), %rsi      # 0x116f1
callq 0xe610
movslq 0xa4(%rsp), %rcx
leaq 0x7c6a4(%rip), %rax     # 0x8b040
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x7c697(%rip), %rax     # 0x8b040
movl %edx, (%rax,%rcx,4)
movl 0x2c(%rsp), %eax
addl 0x30(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x2c(%rsp), %ecx
movl 0x34(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x34(%rsp)
jmp 0xe95e
movq 0x48(%rsp), %rdi
movslq 0xa4(%rsp), %rcx
leaq 0x7c664(%rip), %rax     # 0x8b040
movl (%rax,%rcx,4), %ecx
leaq 0x2d0e(%rip), %rsi      # 0x116f4
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
callq 0xe610
movslq 0xa4(%rsp), %rcx
leaq 0x798ff(%rip), %rax     # 0x88300
cmpl $-0x1, (%rax,%rcx,4)
jle 0xea31
movq 0x48(%rsp), %rdi
movslq 0xa4(%rsp), %rcx
leaq 0x798e5(%rip), %rax     # 0x88300
movl (%rax,%rcx,4), %ecx
leaq 0x2cd2(%rip), %rsi      # 0x116f7
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %r8d
callq 0xe610
movq 0x48(%rsp), %rdi
callq 0x20e0
cmpl $0x0, %eax
je 0xea4e
movq 0x20(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x2670
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)