// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pnm.c
// Function: do_image_pgm_ppm_ascii
static int do_image_pgm_ppm_ascii(deark *c, lctx *d, struct page_ctx *pg, i64 pos1)
{
	int nsamples_per_pixel;
	i64 nsamples_per_image;
	i64 intermed_nbytes_per_image;
	i64 pos = pos1;
	int retval = 0;
	struct pgm_ppm_ascii_ctx actx;

	de_zeromem(&actx, sizeof(struct pgm_ppm_ascii_ctx));
	if(fmt_is_ppm(pg->fmt)) nsamples_per_pixel = 3;
	else nsamples_per_pixel = 1;

	nsamples_per_image = (i64)nsamples_per_pixel * pg->height * pg->width;
	actx.intermed_nbytes_per_sample = (pg->maxval>255) ? 2 : 1;
	intermed_nbytes_per_image = nsamples_per_image * (i64)actx.intermed_nbytes_per_sample;

	actx.intermed_img = dbuf_create_membuf(c, intermed_nbytes_per_image, 0x1);
	actx.samplebuf_used=0;

	actx.sample_count = 0;

	while(1) {
		u8 b;

		if(actx.sample_count >= nsamples_per_image) break;
		if(pos >= c->infile->len) { // end of file
			if(actx.samplebuf_used>0) {
				pgm_ppm_ascii_handle_sample(&actx);
			}
			break;
		}

		b = de_getbyte_p(&pos);
		if(is_pnm_whitespace(b)) {
			if(actx.samplebuf_used>0) {
				// Completed a sample
				pgm_ppm_ascii_handle_sample(&actx);
			}
			else { // Skip extra whitespace
				continue;
			}
		}
		else {
			// Non-whitespace. Save for later.
			if(actx.samplebuf_used < sizeof(actx.samplebuf_used)-1) {
				actx.samplebuf[actx.samplebuf_used++] = b;
			}
		}
	}

	retval = do_image_pgm_ppm_pam_binary(c, d, pg, actx.intermed_img, 0);
	dbuf_close(actx.intermed_img);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x19140
movq -0x18(%rbp), %rax
movl (%rax), %edi
callq 0x10f370
cmpl $0x0, %eax
je 0x10ec96
movl $0x3, -0x24(%rbp)
jmp 0x10ec9d
movl $0x1, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq -0x18(%rbp), %rcx
imulq 0x18(%rcx), %rax
movq -0x18(%rbp), %rcx
imulq 0x10(%rcx), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl 0x20(%rax), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0xff, %edx
cmovgl %ecx, %eax
movb %al, -0x88(%rbp)
movq -0x30(%rbp), %rax
movzbl -0x88(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0x10ed1f
jmp 0x10edae
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x10ed46
cmpq $0x0, -0x70(%rbp)
jbe 0x10ed44
leaq -0x88(%rbp), %rdi
callq 0x10f3a0
jmp 0x10edae
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x89(%rbp)
movzbl -0x89(%rbp), %edi
callq 0x10e920
cmpl $0x0, %eax
je 0x10ed87
cmpq $0x0, -0x70(%rbp)
jbe 0x10ed83
leaq -0x88(%rbp), %rdi
callq 0x10f3a0
jmp 0x10ed85
jmp 0x10ed10
jmp 0x10eda9
cmpq $0x7, -0x70(%rbp)
jae 0x10eda7
movb -0x89(%rbp), %cl
movq -0x70(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x70(%rbp)
movb %cl, -0x68(%rbp,%rax)
jmp 0x10eda9
jmp 0x10ed10
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x78(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x10ee80
movl %eax, -0x44(%rbp)
movq -0x78(%rbp), %rdi
callq 0x1676a0
movl -0x44(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq