// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: screencapture_writeframe
static int screencapture_writeframe(BFILE *fil, char mode, void *v,
	void (*writeline)(unsigned char *, int, int, BFILE *, void *))
{
	int y, ystart, yend, yinc, j;
	unsigned char *ptr, *buf;
	char inverseit = 0, bottotop = 0;

	inverseit = (mode & 1);
	bottotop = (mode & 2);

#if USE_POLYMOST && USE_OPENGL
	if (rendmode >= 3 && qsetmode == 200) {
		char bgr = (mode & 4);

		// OpenGL returns bottom-to-top ordered lines.
		if (bottotop) {
			ystart = 0;
			yend = ydim;
			yinc = 1;
		} else {
			ystart = ydim-1;
			yend = -1;
			yinc = -1;
		}
		buf = kmalloc(xdim*ydim*3);
		if (buf) {
			glfunc.glReadPixels(0,0,xdim,ydim,GL_RGB,GL_UNSIGNED_BYTE,buf);
			if (bgr) {
				for (j=(xdim*ydim-1)*3; j>=0; j-=3) {
					swapchar(&buf[j+0], &buf[j+2]);
				}
			}
			for (y = ystart; y != yend; y += yinc) {
				ptr = buf + y*xdim*3;
				writeline(ptr, xdim, 3, fil, v);
			}
			kfree(buf);
		}
		return(0);
	}
#endif

	ptr = (unsigned char *)frameplace;
	if (bottotop) {
		ystart = ydim-1;
		yend = -1;
		yinc = -1;
	} else {
		ystart = 0;
		yend = ydim;
		yinc = 1;
	}

	if (inverseit && qsetmode != 200) {
		buf = kmalloc(bytesperline);
		if (buf) {
			for (y = ystart; y != yend; y += yinc) {
				copybuf(ptr + y*bytesperline, buf, xdim >> 2);
				for (j=(bytesperline>>2)-1; j>=0; j--) ((int *)buf)[j] ^= 0x0f0f0f0fL;
				writeline(buf, xdim, 1, fil, v);
			}
			kfree(buf);
		}
	} else {
		for (y = ystart; y != yend; y += yinc) {
			writeline(ptr + y*bytesperline, xdim, 1, fil, v);
		}
	}

	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x5b992c(%rip), %rax    # 0x5fa950
cmpl $0x3, (%rax)
setge %r9b
movl 0x1779f7(%rip), %eax    # 0x1b8a28
cmpl $0xc8, %eax
sete %dil
andb %r9b, %dil
cmpb $0x1, %dil
jne 0x41183
movl %esi, 0x18(%rsp)
movl %esi, %eax
andb $0x2, %al
movl 0x3c6f9a(%rip), %r13d   # 0x407ff0
leal -0x1(%r13), %esi
xorl %ebp, %ebp
testb %al, %al
setne 0x17(%rsp)
cmovnel %ebp, %esi
movq %rsi, 0x20(%rsp)
cmpb $0x1, %al
sbbl %ebp, %ebp
movl 0x3c6f77(%rip), %esi    # 0x407fec
movl %r13d, %eax
movl %esi, 0x28(%rsp)
imull %esi, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
movq %rdx, %r14
movq %rcx, %r15
callq 0x69a0
testq %rax, %rax
je 0x412e1
movq %rax, %r12
movq %rbx, 0x30(%rsp)
movq %r14, 0x40(%rsp)
movq %r15, 0x38(%rsp)
orl %r13d, %ebp
leaq 0x5b968b(%rip), %rax    # 0x5fa740
movq %r12, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movl 0x28(%rsp), %edx
movl %r13d, %ecx
movl $0x1907, %r8d           # imm = 0x1907
movl $0x1401, %r9d           # imm = 0x1401
callq *0xb0(%rax)
testb $0x4, 0x18(%rsp)
je 0x41116
movl 0x3c6f09(%rip), %eax    # 0x407fec
imull 0x3c6f06(%rip), %eax    # 0x407ff0
leal (%rax,%rax,2), %eax
addl $-0x3, %eax
js 0x41116
movl %eax, %eax
addq $0x3, %rax
movb -0x3(%r12,%rax), %cl
movb -0x1(%r12,%rax), %dl
movb %cl, -0x1(%r12,%rax)
movb %dl, -0x3(%r12,%rax)
addq $-0x3, %rax
cmpq $0x2, %rax
ja 0x410f8
movq 0x20(%rsp), %rax
movl %eax, %r13d
subl %ebp, %r13d
je 0x4127e
xorl %ecx, %ecx
movb 0x17(%rsp), %dl
movb %dl, %cl
leal -0x1(,%rcx,2), %ecx
leal (%rax,%rax,2), %ebx
movq %rcx, 0x18(%rsp)
leal (%rcx,%rcx,2), %eax
movl %eax, 0x20(%rsp)
movq 0x38(%rsp), %r15
movq 0x40(%rsp), %r14
movq 0x30(%rsp), %rbp
movl 0x3c6e92(%rip), %esi    # 0x407fec
movl %ebx, %eax
imull %esi, %eax
movslq %eax, %rdi
addq %r12, %rdi
movl $0x3, %edx
movq %rbp, %rcx
movq %r14, %r8
callq *%r15
addl 0x20(%rsp), %ebx
addl 0x18(%rsp), %r13d
jne 0x41154
jmp 0x4127e
movq %rbx, 0x30(%rsp)
movq %rdx, %rbx
movq %rcx, 0x38(%rsp)
leaq 0x13cf19(%rip), %rcx    # 0x17e0b0
movl %esi, %edx
andb $0x2, %dl
movl %esi, %r8d
shrb %r8b
movl 0x3c6e48(%rip), %edi    # 0x407ff0
leal -0x1(%rdi), %r13d
xorl %ebp, %ebp
testb %dl, %dl
cmovel %ebp, %r13d
movq (%rcx), %rcx
movq %rcx, 0x28(%rsp)
negb %dl
sbbl %ebp, %ebp
orl %edi, %ebp
movzbl %r8b, %ecx
andl $0x1, %ecx
negl %ecx
orl $0x1, %ecx
movl %ecx, 0x18(%rsp)
cmpl $0xc8, %eax
setne %al
testb %al, %sil
je 0x41294
leaq 0x13d09a(%rip), %rax    # 0x17e284
movslq (%rax), %rdi
callq 0x69a0
testq %rax, %rax
je 0x412e1
movq %rax, %r12
cmpl %ebp, %r13d
movl %ebp, 0x20(%rsp)
je 0x4127e
movq %rbx, %rbp
movq 0x28(%rsp), %rbx
movl 0x18(%rsp), %r14d
leaq 0x13d069(%rip), %r15    # 0x17e284
movslq (%r15), %rax
movslq %r13d, %rdi
imulq %rax, %rdi
addq %rbx, %rdi
movl 0x3c6dbe(%rip), %edx    # 0x407fec
sarl $0x2, %edx
movq %r12, %rsi
callq 0x4d3c2
movl (%r15), %eax
sarl $0x2, %eax
testl %eax, %eax
jle 0x4125a
movl %eax, %eax
incq %rax
xorl $0xf0f0f0f, -0x8(%r12,%rax,4) # imm = 0xF0F0F0F
decq %rax
cmpq $0x1, %rax
ja 0x41248
movl 0x3c6d8c(%rip), %esi    # 0x407fec
movq %r12, %rdi
movl $0x1, %edx
movq 0x30(%rsp), %rcx
movq %rbp, %r8
callq *0x38(%rsp)
addl %r14d, %r13d
cmpl 0x20(%rsp), %r13d
jne 0x41214
movq %r12, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x61c0
cmpl %ebp, %r13d
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r12
je 0x412e1
leaq 0x13cfd5(%rip), %rsi    # 0x17e284
movslq (%rsi), %rdi
movslq %r13d, %r13
imulq %r13, %rdi
addq %r12, %rdi
movl 0x3c6d2a(%rip), %esi    # 0x407fec
movl $0x1, %edx
movq %r14, %rcx
movq %rbx, %r8
movq %rax, %r15
callq *%rax
movl 0x18(%rsp), %esi
movq %r15, %rax
addl %esi, %r13d
cmpl %r13d, %ebp
jne 0x412a8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq