// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pnm.c
// Function: read_pam_header
static int read_pam_header(deark *c, lctx *d, struct page_ctx *pg, i64 pos1)
{
	int ret;
	i64 pos = pos1;
	int retval = 0;
	int tupltype_line_count = 0;
	char linebuf[200];
	char token1buf[200];
	char token2buf[200];

	de_dbg(c, "header at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	pos += 3; // Skip "P7\n"

	while(1) {
		i64 content_len;
		i64 total_len;
		i64 curpos;

		ret = read_pam_header_line(c, d, pg, pos, &content_len, &total_len,
			linebuf, sizeof(linebuf));
		pos += total_len;

		if(!ret) {
			de_err(c, "Invalid PAM header");
			goto done;
		}

		if(content_len>0 && (de_getbyte(pos)=='#')) {
			// comment line
			pos += total_len;
			continue;
		}

		curpos = 0;
		if(!read_next_pam_token(c, d, pg, linebuf, sizeof(linebuf),
			token1buf, sizeof(token1buf), &curpos))
		{
			goto done;
		}

		if(!de_strcmp(token1buf, "ENDHDR")) {
			break;
		}

		// Other header lines have a param
		if(!read_next_pam_token(c, d, pg, linebuf, sizeof(linebuf),
			token2buf, sizeof(token2buf), &curpos))
		{
			goto done;
		}

		if(!de_strcmp(token1buf, "WIDTH")) {
			pg->width = de_atoi64(token2buf);
		}
		else if(!de_strcmp(token1buf, "HEIGHT")) {
			pg->height = de_atoi64(token2buf);
		}
		else if(!de_strcmp(token1buf, "DEPTH")) {
			pg->pam_depth = de_atoi(token2buf);
		}
		else if(!de_strcmp(token1buf, "MAXVAL")) {
			pg->maxval = de_atoi(token2buf);
		}
		else if(!de_strcmp(token1buf, "TUPLTYPE")) {
			// FIXME: The "TUPLTYPE" line(s) is wacky, and seems underspecified.
			// We do not support it correctly.
			// But I doubt any real PAM encoders are pathological enough to
			// require us to support its wackiness.
			if(tupltype_line_count>0) {
				de_err(c, "Multiple TUPLTYPE lines are not supported");
				goto done;
			}
			tupltype_line_count++;

			if(!de_strcmp(token2buf, "BLACKANDWHITE")) {
				pg->pam_subtype = PAMSUBTYPE_GRAY;
				pg->maxval = 1;
			}
			else if(!de_strcmp(token2buf, "BLACKANDWHITE_ALPHA")) {
				pg->pam_subtype = PAMSUBTYPE_GRAY;
				pg->has_alpha = 1;
				pg->maxval = 1;
			}
			else if(!de_strcmp(token2buf, "GRAYSCALE")) {
				pg->pam_subtype = PAMSUBTYPE_GRAY;
			}
			else if(!de_strcmp(token2buf, "GRAYSCALE_ALPHA")) {
				pg->pam_subtype = PAMSUBTYPE_GRAY;
				pg->has_alpha = 1;
			}
			else if(!de_strcmp(token2buf, "RGB")) {
				pg->pam_subtype = PAMSUBTYPE_RGB;
			}
			else if(!de_strcmp(token2buf, "RGB_ALPHA")) {
				pg->pam_subtype = PAMSUBTYPE_RGB;
				pg->has_alpha = 1;
			}
			else {
				de_err(c, "Unsupported color type");
				goto done;
			}
		}
	}

	if(tupltype_line_count==0) {
		// The TUPLTYPE field is technically optional, but the image is not
		// portable without it.
		switch(pg->pam_depth) {
		case 1:
			pg->pam_subtype = PAMSUBTYPE_GRAY;
			break;
		case 2:
			pg->pam_subtype = PAMSUBTYPE_GRAY;
			pg->has_alpha = 1;
			break;
		case 3:
			pg->pam_subtype = PAMSUBTYPE_RGB;
			break;
		case 4:
			pg->pam_subtype = PAMSUBTYPE_RGB;
			pg->has_alpha = 1;
			break;
		}

		if(pg->pam_subtype!=0) {
			de_warn(c, "Color type not specified. Attempting to guess.");
		}
	}

	pg->hdr_parse_pos = pos;
	retval = 1;
done:
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0xa0dd3(%rip), %rsi     # 0x1aed63
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x100(%rbp), %rax
leaq -0x2a8(%rbp), %r8
leaq -0x2b0(%rbp), %r9
movq %rax, (%rsp)
movq $0xc8, 0x8(%rsp)
callq 0x10e760
movl %eax, -0x24(%rbp)
movq -0x2b0(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x10e017
movq -0x8(%rbp), %rdi
leaq 0xb565f(%rip), %rsi     # 0x1c366a
movb $0x0, %al
callq 0x15bfb0
jmp 0x10e409
cmpq $0x0, -0x2a8(%rbp)
jle 0x10e04e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x23, %eax
jne 0x10e04e
movq -0x2b0(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x10dfb1
movq $0x0, -0x2b8(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x100(%rbp), %rcx
leaq -0x1d0(%rbp), %r9
movl $0xc8, %r8d
leaq -0x2b8(%rbp), %rax
movq $0xc8, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x10e830
cmpl $0x0, %eax
jne 0x10e09c
jmp 0x10e409
leaq -0x1d0(%rbp), %rdi
leaq 0xb55d3(%rip), %rsi     # 0x1c367d
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e0b9
jmp 0x10e358
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x100(%rbp), %rcx
leaq -0x2a0(%rbp), %r9
movl $0xc8, %r8d
leaq -0x2b8(%rbp), %rax
movq $0xc8, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x10e830
cmpl $0x0, %eax
jne 0x10e0fc
jmp 0x10e409
leaq -0x1d0(%rbp), %rdi
leaq 0xbe7da(%rip), %rsi     # 0x1cc8e4
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e130
leaq -0x2a0(%rbp), %rdi
callq 0x15ccc0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x10e353
leaq -0x1d0(%rbp), %rdi
leaq 0xbb8d9(%rip), %rsi     # 0x1c9a17
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e164
leaq -0x2a0(%rbp), %rdi
callq 0x15ccc0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x10e351
leaq -0x1d0(%rbp), %rdi
leaq 0xb5512(%rip), %rsi     # 0x1c3684
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e196
leaq -0x2a0(%rbp), %rdi
callq 0x15cca0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x24(%rax)
jmp 0x10e34f
leaq -0x1d0(%rbp), %rdi
leaq 0xb54e6(%rip), %rsi     # 0x1c368a
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e1c8
leaq -0x2a0(%rbp), %rdi
callq 0x15cca0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0x10e34d
leaq -0x1d0(%rbp), %rdi
leaq 0xb54bb(%rip), %rsi     # 0x1c3691
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e34b
cmpl $0x0, -0x38(%rbp)
jle 0x10e201
movq -0x8(%rbp), %rdi
leaq 0xb54a5(%rip), %rsi     # 0x1c369a
movb $0x0, %al
callq 0x15bfb0
jmp 0x10e409
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
leaq -0x2a0(%rbp), %rdi
leaq 0xb54ac(%rip), %rsi     # 0x1c36c4
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e23d
movq -0x18(%rbp), %rax
movl $0x1, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x20(%rax)
jmp 0x10e349
leaq -0x2a0(%rbp), %rdi
leaq 0xb5487(%rip), %rsi     # 0x1c36d2
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e27b
movq -0x18(%rbp), %rax
movl $0x1, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x2c(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x20(%rax)
jmp 0x10e347
leaq -0x2a0(%rbp), %rdi
leaq 0xb545d(%rip), %rsi     # 0x1c36e6
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e2a3
movq -0x18(%rbp), %rax
movl $0x1, 0x28(%rax)
jmp 0x10e345
leaq -0x2a0(%rbp), %rdi
leaq 0xb543f(%rip), %rsi     # 0x1c36f0
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e2d3
movq -0x18(%rbp), %rax
movl $0x1, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x2c(%rax)
jmp 0x10e343
leaq -0x2a0(%rbp), %rdi
leaq 0xbd334(%rip), %rsi     # 0x1cb615
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e2f8
movq -0x18(%rbp), %rax
movl $0x2, 0x28(%rax)
jmp 0x10e341
leaq -0x2a0(%rbp), %rdi
leaq 0xb53fa(%rip), %rsi     # 0x1c3700
callq 0x191a0
cmpl $0x0, %eax
jne 0x10e328
movq -0x18(%rbp), %rax
movl $0x2, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x2c(%rax)
jmp 0x10e33f
movq -0x8(%rbp), %rdi
leaq 0xb53d7(%rip), %rsi     # 0x1c370a
movb $0x0, %al
callq 0x15bfb0
jmp 0x10e409
jmp 0x10e341
jmp 0x10e343
jmp 0x10e345
jmp 0x10e347
jmp 0x10e349
jmp 0x10e34b
jmp 0x10e34d
jmp 0x10e34f
jmp 0x10e351
jmp 0x10e353
jmp 0x10dfb1
cmpl $0x0, -0x38(%rbp)
jne 0x10e3f6
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x2c0(%rbp)
subl $0x3, %eax
ja 0x10e3d8
movq -0x2c0(%rbp), %rax
leaq 0xb524d(%rip), %rcx     # 0x1c35d4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rbp), %rax
movl $0x1, 0x28(%rax)
jmp 0x10e3d8
movq -0x18(%rbp), %rax
movl $0x1, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x2c(%rax)
jmp 0x10e3d8
movq -0x18(%rbp), %rax
movl $0x2, 0x28(%rax)
jmp 0x10e3d8
movq -0x18(%rbp), %rax
movl $0x2, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x2c(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x10e3f4
movq -0x8(%rbp), %rdi
leaq 0xb5334(%rip), %rsi     # 0x1c3721
movb $0x0, %al
callq 0x15bc10
jmp 0x10e3f6
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movl $0x1, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x34(%rbp), %eax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
nopw %cs:(%rax,%rax)