// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/binhex.c
// Function: do_decode_main
static int do_decode_main(deark *c, lctx *d, i64 pos)
{
	u8 b;
	u8 x;
	u8 pending_byte = 0;
	unsigned int pending_bits_used = 0;

	while(1) {
		if(pos >= c->infile->len) return 0; // unexpected end of file
		b = de_getbyte(pos);
		pos++;
		if(b==':') {
			break;
		}
		else if(b=='\x0a' || b=='\x0d' || b==' ' || b=='\t') {
			// Ignore whitespace
			continue;
		}

		x = get_char_value(d, b);
		if(x>=64) {
			de_err(c, "Invalid BinHex data at %d", (int)(pos-1));
			return 0;
		}

		// TODO: Simplify this code
		if(pending_bits_used==0) {
			pending_byte = x;
			pending_bits_used = 6;
		}
		else if(pending_bits_used==2) {
			pending_byte = (pending_byte<<(8-pending_bits_used))|x;
			dbuf_writebyte(d->decoded, pending_byte);
			pending_bits_used -= 2;
		}
		else if(pending_bits_used==4) {
			pending_byte = (pending_byte<<(8-pending_bits_used))|(x>>(pending_bits_used-2));
			dbuf_writebyte(d->decoded, pending_byte);
			pending_byte = x&0x03;
			pending_bits_used -= 2;
		}
		else if(pending_bits_used==6) {
			pending_byte = (pending_byte<<(8-pending_bits_used))|(x>>(pending_bits_used-2));
			dbuf_writebyte(d->decoded, pending_byte);
			pending_byte = x&0x0f;
			pending_bits_used -= 2;
		}
	}

	dbuf_flush(d->decoded);
	de_dbg(c, "size after decoding: %d", (int)d->decoded->len);
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movb $0x0, -0x23(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x3de3d
movl $0x0, -0x4(%rbp)
jmp 0x3dfff
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x165060
movb %al, -0x21(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x3a, %eax
jne 0x3de6b
jmp 0x3dfcb
movzbl -0x21(%rbp), %eax
cmpl $0xa, %eax
je 0x3de8f
movzbl -0x21(%rbp), %eax
cmpl $0xd, %eax
je 0x3de8f
movzbl -0x21(%rbp), %eax
cmpl $0x20, %eax
je 0x3de8f
movzbl -0x21(%rbp), %eax
cmpl $0x9, %eax
jne 0x3de91
jmp 0x3de1f
jmp 0x3de93
movq -0x18(%rbp), %rdi
movzbl -0x21(%rbp), %esi
callq 0x3e4c0
movb %al, -0x22(%rbp)
movzbl -0x22(%rbp), %eax
cmpl $0x40, %eax
jl 0x3ded4
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
subq $0x1, %rax
movl %eax, %edx
leaq 0x164db7(%rip), %rsi    # 0x1a2c78
movb $0x0, %al
callq 0x15bfb0
movl $0x0, -0x4(%rbp)
jmp 0x3dfff
cmpl $0x0, -0x28(%rbp)
jne 0x3deec
movb -0x22(%rbp), %al
movb %al, -0x23(%rbp)
movl $0x6, -0x28(%rbp)
jmp 0x3dfc6
cmpl $0x2, -0x28(%rbp)
jne 0x3df28
movzbl -0x23(%rbp), %eax
movl $0x8, %ecx
subl -0x28(%rbp), %ecx
shll %cl, %eax
movzbl -0x22(%rbp), %ecx
orl %ecx, %eax
movb %al, -0x23(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movzbl -0x23(%rbp), %esi
callq 0x168380
movl -0x28(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x28(%rbp)
jmp 0x3dfc4
cmpl $0x4, -0x28(%rbp)
jne 0x3df75
movzbl -0x23(%rbp), %eax
movl $0x8, %ecx
subl -0x28(%rbp), %ecx
shll %cl, %eax
movzbl -0x22(%rbp), %edx
movl -0x28(%rbp), %ecx
subl $0x2, %ecx
sarl %cl, %edx
movl %edx, %ecx
orl %ecx, %eax
movb %al, -0x23(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movzbl -0x23(%rbp), %esi
callq 0x168380
movzbl -0x22(%rbp), %eax
andl $0x3, %eax
movb %al, -0x23(%rbp)
movl -0x28(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x28(%rbp)
jmp 0x3dfc2
cmpl $0x6, -0x28(%rbp)
jne 0x3dfc0
movzbl -0x23(%rbp), %eax
movl $0x8, %ecx
subl -0x28(%rbp), %ecx
shll %cl, %eax
movzbl -0x22(%rbp), %edx
movl -0x28(%rbp), %ecx
subl $0x2, %ecx
sarl %cl, %edx
movl %edx, %ecx
orl %ecx, %eax
movb %al, -0x23(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movzbl -0x23(%rbp), %esi
callq 0x168380
movzbl -0x22(%rbp), %eax
andl $0xf, %eax
movb %al, -0x23(%rbp)
movl -0x28(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x28(%rbp)
jmp 0x3dfc2
jmp 0x3dfc4
jmp 0x3dfc6
jmp 0x3de1f
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x167c30
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x164ca1(%rip), %rsi    # 0x1a2c92
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)