// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-fax.c
// Function: do_decompress_fax34
static void do_decompress_fax34(deark *c, struct fax_ctx *fc,
	struct fax34_huffman_tree *f34ht)
{
	char errmsg[100];
	static const char errmsg_UNEXPECTEDEOD[] = "Unexpected end of compressed data";
	static const char errmsg_HUFFDECODEERR[] = "Huffman decode error";
	static const char errmsg_NOEOL[] = "Failed to find EOL mark";
	static const char errmsg_UNSUPPEXT[] = "Decoding error or unsupported Fax extension";

	errmsg[0] = '\0';
	init_fax34_bitreader(c, fc);

	if(fc->has_eol_codes) {
		if(!fax34_full_sync(c, fc, 1024)) {
			if(fc->fax34params->tiff_cmpr_meth==3 && fc->dcmpri->len>0) {
				de_dbg(c, "[no sync mark found, trying to compensate]");
				fc->has_eol_codes = 0;
				fc->rows_padded_to_next_byte = 0;
				init_fax34_bitreader(c, fc);
			}
			else {
				de_strlcpy(errmsg, "Failed to find sync mark", sizeof(errmsg));
				goto done;
			}
		}
	}

	fc->pending_run_len = 0;
	fc->f2d_h_codes_remaining = 0;
	fc->ypos = 0;
	fc->a0 = -1;
	fc->a0_color = 0;
	fc->have_read_tag_bit = 0;

	while(1) {
		int ret;
		int in_2d_mode;
		fmtutil_huffman_valtype val = 0;

		if(fc->ypos >= fc->image_height ||
			((fc->ypos == fc->image_height-1) && (fc->a0 >= fc->image_width)))
		{
			goto done; // Normal completion
		}
		if(fc->dcmpro->len_known && fc->nbytes_written>fc->dcmpro->expected_len) {
			goto done; // Sufficient output
		}

		if(fc->bitrd.eof_flag) {
			de_strlcpy(errmsg, errmsg_UNEXPECTEDEOD, sizeof(errmsg));
			goto done;
		}

		if(!fc->has_eol_codes && (fc->a0 >= fc->image_width) && fc->f2d_h_codes_remaining==0) {
			if(fc->rows_padded_to_next_byte) {
				de_bitbuf_lowlevel_empty(&fc->bitrd.bbll);
			}
			fax34_on_eol(c, fc, 0);
		}

		if(fc->is_2d && fc->fax34params->tiff_cmpr_meth==3 && !fc->have_read_tag_bit) {
			fc->tag_bit = (u8)de_bitreader_getbits(&fc->bitrd, 1);
			fc->have_read_tag_bit = 1;
		}

		in_2d_mode = 0;
		if(fc->is_2d) {
			if(fc->f2d_h_codes_remaining == 0) {
				if(fc->fax34params->tiff_cmpr_meth==3) {
					if(fc->tag_bit==0) {
						in_2d_mode = 1;
					}
				}
				else { // (Fax4)
					in_2d_mode = 1;
				}
			}
		}

		if(in_2d_mode) {
			ret = fmtutil_huffman_read_next_value(f34ht->_2d_codes->bk, &fc->bitrd, &val, NULL);
			if(!ret) {
				if(fc->bitrd.eof_flag) {
					de_strlcpy(errmsg, errmsg_UNEXPECTEDEOD, sizeof(errmsg));
				}
				else {
					de_strlcpy(errmsg, errmsg_HUFFDECODEERR, sizeof(errmsg));
				}
				goto done;
			}
			de_dbg3(c, "val: %d", (int)val);

			if(val>=FAX2D_V_BIAS-3 && val<=FAX2D_V_BIAS+3) { // VL(3), ..., V(0), ..., VR(3)
				i64 run_len;

				find_b1(fc);
				de_dbg3(c, "at %d b1=%d", (int)fc->a0, (int)fc->b1);
				run_len = fc->b1 - fc->a0 + ((i64)val-FAX2D_V_BIAS);

				fax34_record_run(c, fc, run_len, 1);
				fc->a0_color = fc->a0_color?0:1;
			}
			else if(val==FAX2D_P) {
				find_b1_and_b2(fc);
				fax34_record_run(c, fc, fc->b2 - fc->a0, 1);
			}
			else if(val==FAX2D_H) {
				fc->f2d_h_codes_remaining = 2;
			}
			else if(val==FAX2D_7ZEROES) {
				if(fc->has_eol_codes) {
					if(!fax34_finish_sync(c, fc, 64, NULL)) {
						de_strlcpy(errmsg, errmsg_NOEOL, sizeof(errmsg));
						goto done;
					}
					fax34_on_eol(c, fc, 1);
				}
				else {
					// Full EOFB should be 000000000001000000000001
					de_dbg3(c, "EOFB");
					goto done;
				}
			}
			else if(val==FAX2D_EXTENSION) {
				UI extnum;

				extnum = (UI)de_bitreader_getbits(&fc->bitrd, 3);
				// TODO?: Support uncompressed mode
				de_snprintf(errmsg, sizeof(errmsg), "%s (%u)", errmsg_UNSUPPEXT, extnum);
				goto done;
			}
			else {
				goto done; // Should be impossible
			}
		}
		else {
			ret = fmtutil_huffman_read_next_value(f34ht->htwb[(UI)fc->a0_color]->bk, &fc->bitrd, &val, NULL);
			if(!ret) {
				if(fc->bitrd.eof_flag) {
					de_strlcpy(errmsg, errmsg_UNEXPECTEDEOD, sizeof(errmsg));
				}
				else {
					de_strlcpy(errmsg, errmsg_HUFFDECODEERR, sizeof(errmsg));
				}
				goto done;
			}

			if(val==FAX1D_8ZEROES) {
				i64 nbits_read;

				if(!fc->has_eol_codes || fc->f2d_h_codes_remaining!=0) {
					de_strlcpy(errmsg, errmsg_HUFFDECODEERR, sizeof(errmsg));
					goto done;
				}

				if(!fax34_finish_sync(c, fc, 64, &nbits_read)) {
					de_strlcpy(errmsg, errmsg_NOEOL, sizeof(errmsg));
					goto done;
				}
				if(nbits_read<4) {
					de_strlcpy(errmsg, errmsg_UNSUPPEXT, sizeof(errmsg));
					// TODO: Attempt error recovery?
					goto done;
				}
				// TODO: Check for premature EOL?
				fax34_on_eol(c, fc, 1);
			}
			else if(val<64) {
				fc->pending_run_len += (i64)val;
				fax34_record_run(c, fc, fc->pending_run_len, 0);
				fc->pending_run_len = 0;
				fc->a0_color = fc->a0_color?0:1;
				if(fc->f2d_h_codes_remaining>0) {
					fc->f2d_h_codes_remaining--;
				}
			}
			else { // make-up code
				fc->pending_run_len += (i64)val;
			}
		}
	}

done:
	if(fc->a0>0) {
		fax34_on_eol(c, fc, 0); // Make sure we emit the last row
	}

	if(errmsg[0]) {
		if(fc->ypos>0) {
			de_warn(c, "[%s] Failed to decode entire strip: %s", fc->modname, errmsg);
		}
		else {
			de_dfilter_set_errorf(c, fc->dres, fc->modname, "%s", errmsg);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x17e640
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x17ddb8
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x17e6c0
cmpl $0x0, %eax
jne 0x17ddb6
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
cmpl $0x3, 0x18(%rax)
jne 0x17dd9a
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpq $0x0, 0x10(%rax)
jle 0x17dd9a
movq -0x8(%rbp), %rdi
leaq 0x58cb7(%rip), %rsi     # 0x1d6a2b
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movb $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x29(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x17e640
jmp 0x17ddb4
leaq -0x80(%rbp), %rdi
leaq 0x58cb1(%rip), %rsi     # 0x1d6a56
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e40b
jmp 0x17ddb6
jmp 0x17ddb8
movq -0x10(%rbp), %rax
movq $0x0, 0x98(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0xa8(%rax)
movq -0x10(%rbp), %rax
movq $-0x1, 0xb0(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0xb8(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0xb9(%rax)
movl $0x0, -0x8c(%rbp)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jge 0x17de59
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x17de5e
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jl 0x17de5e
jmp 0x17e40b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
je 0x17de8a
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpq 0x10(%rcx), %rax
jle 0x17de8a
jmp 0x17e40b
movq -0x10(%rbp), %rax
cmpb $0x0, 0x60(%rax)
je 0x17deae
leaq -0x80(%rbp), %rdi
leaq 0x58db1(%rip), %rsi     # 0x1d6c50
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e40b
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
jne 0x17df04
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jl 0x17df04
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa0(%rax)
jne 0x17df04
movq -0x10(%rbp), %rax
cmpb $0x0, 0x29(%rax)
je 0x17def5
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
addq $0x20, %rdi
callq 0x16a360
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x17e770
movq -0x10(%rbp), %rax
movzbl 0x2a(%rax), %eax
cmpl $0x0, %eax
je 0x17df55
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
cmpl $0x3, 0x18(%rax)
jne 0x17df55
movq -0x10(%rbp), %rax
cmpb $0x0, 0xb9(%rax)
jne 0x17df55
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
movl $0x1, %esi
callq 0x16a390
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0xba(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0xb9(%rax)
movl $0x0, -0x88(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2a(%rax)
je 0x17dfae
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa0(%rax)
jne 0x17dfac
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
cmpl $0x3, 0x18(%rax)
jne 0x17dfa0
movq -0x10(%rbp), %rax
movzbl 0xba(%rax), %eax
cmpl $0x0, %eax
jne 0x17df9e
movl $0x1, -0x88(%rbp)
jmp 0x17dfaa
movl $0x1, -0x88(%rbp)
jmp 0x17dfac
jmp 0x17dfae
cmpl $0x0, -0x88(%rbp)
je 0x17e228
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x48, %rsi
leaq -0x8c(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x187bc0
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jne 0x17e029
movq -0x10(%rbp), %rax
cmpb $0x0, 0x60(%rax)
je 0x17e00f
leaq -0x80(%rbp), %rdi
leaq 0x58c4d(%rip), %rsi     # 0x1d6c50
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e024
leaq -0x80(%rbp), %rdi
leaq 0x58c66(%rip), %rsi     # 0x1d6c80
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e40b
movq -0x8(%rbp), %rdi
movl -0x8c(%rbp), %edx
leaq 0x387e4(%rip), %rsi     # 0x1b681e
movb $0x0, %al
callq 0x15b400
cmpl $0x61, -0x8c(%rbp)
jl 0x17e0fd
cmpl $0x67, -0x8c(%rbp)
jg 0x17e0fd
movq -0x10(%rbp), %rdi
callq 0x17e950
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rax
movl %eax, %ecx
leaq 0x589e6(%rip), %rsi     # 0x1d6a6f
movb $0x0, %al
callq 0x15b400
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x10(%rbp), %rcx
subq 0xb0(%rcx), %rax
movslq -0x8c(%rbp), %rcx
subq $0x64, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x98(%rbp), %rdx
movl $0x1, %ecx
callq 0x17ea40
movq -0x10(%rbp), %rax
movzbl 0xb8(%rax), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0xb8(%rax)
jmp 0x17e223
cmpl $0x1, -0x8c(%rbp)
jne 0x17e13c
movq -0x10(%rbp), %rdi
callq 0x17eb80
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdx
movq -0x10(%rbp), %rax
subq 0xb0(%rax), %rdx
movl $0x1, %ecx
callq 0x17ea40
jmp 0x17e221
cmpl $0x2, -0x8c(%rbp)
jne 0x17e158
movq -0x10(%rbp), %rax
movl $0x2, 0xa0(%rax)
jmp 0x17e21f
cmpl $0x0, -0x8c(%rbp)
jne 0x17e1cd
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x17e1b4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x40, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x17ec20
cmpl $0x0, %eax
jne 0x17e1a0
leaq -0x80(%rbp), %rdi
leaq 0x58b0f(%rip), %rsi     # 0x1d6ca0
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e40b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x1, %edx
callq 0x17e770
jmp 0x17e1cb
movq -0x8(%rbp), %rdi
leaq 0x588bc(%rip), %rsi     # 0x1d6a7b
movb $0x0, %al
callq 0x15b400
jmp 0x17e40b
jmp 0x17e21d
cmpl $0x3, -0x8c(%rbp)
jne 0x17e218
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
movl $0x3, %esi
callq 0x16a390
movl %eax, -0x9c(%rbp)
leaq -0x80(%rbp), %rdi
movl -0x9c(%rbp), %r8d
movl $0x64, %esi
leaq 0x5887b(%rip), %rdx     # 0x1d6a80
leaq 0x58ab4(%rip), %rcx     # 0x1d6cc0
movb $0x0, %al
callq 0x15a980
jmp 0x17e40b
jmp 0x17e40b
jmp 0x17e21f
jmp 0x17e221
jmp 0x17e223
jmp 0x17e406
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movzbl 0xb8(%rcx), %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x48, %rsi
leaq -0x8c(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x187bc0
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jne 0x17e2a3
movq -0x10(%rbp), %rax
cmpb $0x0, 0x60(%rax)
je 0x17e289
leaq -0x80(%rbp), %rdi
leaq 0x589d3(%rip), %rsi     # 0x1d6c50
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e29e
leaq -0x80(%rbp), %rdi
leaq 0x589ec(%rip), %rsi     # 0x1d6c80
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e40b
cmpl $-0x1, -0x8c(%rbp)
jne 0x17e354
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x17e2c7
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa0(%rax)
je 0x17e2e1
leaq -0x80(%rbp), %rdi
leaq 0x589ae(%rip), %rsi     # 0x1d6c80
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e40b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x40, %edx
leaq -0xa8(%rbp), %rcx
callq 0x17ec20
cmpl $0x0, %eax
jne 0x17e319
leaq -0x80(%rbp), %rdi
leaq 0x58996(%rip), %rsi     # 0x1d6ca0
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e40b
cmpq $0x4, -0xa8(%rbp)
jge 0x17e33d
leaq -0x80(%rbp), %rdi
leaq 0x58992(%rip), %rsi     # 0x1d6cc0
movl $0x64, %edx
callq 0x15aa30
jmp 0x17e40b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x1, %edx
callq 0x17e770
jmp 0x17e404
cmpl $0x40, -0x8c(%rbp)
jge 0x17e3e9
movslq -0x8c(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x98(%rax), %rcx
movq %rcx, 0x98(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rdx
xorl %ecx, %ecx
callq 0x17ea40
movq -0x10(%rbp), %rax
movq $0x0, 0x98(%rax)
movq -0x10(%rbp), %rax
movzbl 0xb8(%rax), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0xb8(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa0(%rax)
jbe 0x17e3e7
movq -0x10(%rbp), %rax
movl 0xa0(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xa0(%rax)
jmp 0x17e402
movslq -0x8c(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x98(%rax), %rcx
movq %rcx, 0x98(%rax)
jmp 0x17e404
jmp 0x17e406
jmp 0x17de09
movq -0x10(%rbp), %rax
cmpq $0x0, 0xb0(%rax)
jle 0x17e428
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x17e770
cmpb $0x0, -0x80(%rbp)
je 0x17e484
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa8(%rax)
jle 0x17e45c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq -0x80(%rbp), %rcx
leaq 0x58635(%rip), %rsi     # 0x1d6a88
movb $0x0, %al
callq 0x15bc10
jmp 0x17e482
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq -0x80(%rbp), %r8
leaq 0x226b4(%rip), %rcx     # 0x1a0b2f
movb $0x0, %al
callq 0x177450
jmp 0x17e484
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)