// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/mksimtape/mksimtape.c
// Function: write_file
static BOOL write_file(PCHAR file, INT bs)
{	BOOL rc = TRUE;
	INT n;
	INT obs;
	INT le_bs;
	UCHAR sbuf[4];
	FILE *fp;
	PCHAR buf;

	obs = (bs+1) & ~1;/* Round up output block size to make even */
	le_bs = blksize(bs);	/* Little endian true block size */
	sbuf[0] = (le_bs & 0x000000ff);
	sbuf[1] = (le_bs & 0x0000ff00) >> 8;
	sbuf[2] = (le_bs & 0x00ff0000) >> 16;
	sbuf[3] = (le_bs & 0xff000000) >> 24;
	fprintf(stderr, "Writing file %s with block size %d %s\n",
		file, obs, bs == obs ? "": "(rounded up)");

	fp = fopen(file, "r");
	if(fp == (FILE *) NULL) {
		error("cannot open file '%s'", file);
		return(FALSE);
	}

	buf = (PCHAR) malloc(obs);
	if(buf == (PCHAR) NULL) {
		error("cannot allocate memory for buffer");
		return(FALSE);
	}

	for(;;) {
		n = fread(buf, 1, bs, fp);
		if(n < bs) {
			if(!feof(fp)) {
				error("error reading file '%s'", file);
				rc = FALSE;
				break;
			}
			if(n == 0) break;	/* No partial buffer */
			memset(&buf[n], '\0', bs-n);	/* Zero fill */
		}
		if(bs != obs) buf[obs-1] = '\0';	/* Pad */
		n = fwrite(sbuf, 1, 4, stdout);	/* Record header */
		if(n != 4) {
			error("Error writing to tape image");
			rc = FALSE;
			break;
		}
		n = fwrite(buf, 1, obs, stdout);/* Record data */
		if(n < obs) {
			error("Error writing to tape image");
			rc = FALSE;
			break;
		}
		n = fwrite(sbuf, 1, 4, stdout);	/* Record trailer */
		if(n != 4) {
			error("Error writing to tape image");
			rc = FALSE;
			break;
		}
	}

	free((PCHAR) buf);
	fclose(fp);
	return(rc);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x1, -0x18(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
andl $-0x2, %eax
movl %eax, -0x20(%rbp)
movl -0x14(%rbp), %edi
callq 0x17f0
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
andl $0xff, %eax
movb %al, -0x28(%rbp)
movl -0x24(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, -0x27(%rbp)
movl -0x24(%rbp), %eax
andl $0xff0000, %eax         # imm = 0xFF0000
sarl $0x10, %eax
movb %al, -0x26(%rbp)
movl -0x24(%rbp), %eax
andl $0xff000000, %eax       # imm = 0xFF000000
shrl $0x18, %eax
movb %al, -0x25(%rbp)
movq 0x2a87(%rip), %rax      # 0x3ff0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
movl -0x20(%rbp), %ecx
movl -0x14(%rbp), %esi
movl -0x20(%rbp), %r9d
leaq 0xaf2(%rip), %r8        # 0x2073
leaq 0xb80(%rip), %rax       # 0x2108
cmpl %r9d, %esi
cmoveq %rax, %r8
leaq 0xab6(%rip), %rsi       # 0x204c
movb $0x0, %al
callq 0x10a0
movq -0x10(%rbp), %rdi
leaq 0xb0e(%rip), %rsi       # 0x20b6
callq 0x10e0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x15d6
movq -0x10(%rbp), %rsi
leaq 0xabd(%rip), %rdi       # 0x2080
movb $0x0, %al
callq 0x1380
movl $0x0, -0x4(%rbp)
jmp 0x176a
movslq -0x20(%rbp), %rdi
callq 0x10d0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x1604
leaq 0xaa5(%rip), %rdi       # 0x2096
movb $0x0, %al
callq 0x1380
movl $0x0, -0x4(%rbp)
jmp 0x176a
jmp 0x1606
movq -0x38(%rbp), %rdi
movslq -0x14(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl $0x1, %esi
callq 0x1040
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1679
movq -0x30(%rbp), %rdi
callq 0x10b0
cmpl $0x0, %eax
jne 0x1653
movq -0x10(%rbp), %rsi
leaq 0xa78(%rip), %rdi       # 0x20b8
movb $0x0, %al
callq 0x1380
movl $0x0, -0x18(%rbp)
jmp 0x1752
cmpl $0x0, -0x1c(%rbp)
jne 0x165e
jmp 0x1752
movq -0x38(%rbp), %rdi
movslq -0x1c(%rbp), %rax
addq %rax, %rdi
movl -0x14(%rbp), %eax
subl -0x1c(%rbp), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x1080
movl -0x14(%rbp), %eax
cmpl -0x20(%rbp), %eax
je 0x1692
movq -0x38(%rbp), %rax
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
leaq -0x28(%rbp), %rdi
movq 0x2933(%rip), %rax      # 0x3fd0
movq (%rax), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x1110
movl %eax, -0x1c(%rbp)
cmpl $0x4, -0x1c(%rbp)
je 0x16d2
leaq 0xa11(%rip), %rdi       # 0x20d0
movb $0x0, %al
callq 0x1380
movl $0x0, -0x18(%rbp)
jmp 0x1752
movq -0x38(%rbp), %rdi
movslq -0x20(%rbp), %rdx
movq 0x28ef(%rip), %rax      # 0x3fd0
movq (%rax), %rcx
movl $0x1, %esi
callq 0x1110
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1710
leaq 0x9d0(%rip), %rdi       # 0x20d0
movb $0x0, %al
callq 0x1380
movl $0x0, -0x18(%rbp)
jmp 0x1752
leaq -0x28(%rbp), %rdi
movq 0x28b5(%rip), %rax      # 0x3fd0
movq (%rax), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x1110
movl %eax, -0x1c(%rbp)
cmpl $0x4, -0x1c(%rbp)
je 0x174d
leaq 0x993(%rip), %rdi       # 0x20d0
movb $0x0, %al
callq 0x1380
movl $0x0, -0x18(%rbp)
jmp 0x1752
jmp 0x1606
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1050
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)