// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/grasp.c
// Function: do_extract_file
static int do_extract_file(deark *c, lctx *d, i64 fnum)
{
	i64 index_entry_pos;
	i64 data_block_pos;
	i64 dpos;
	i64 dlen;
	de_finfo *fi = NULL;
	de_ucstring *fname = NULL;
	int saved_indent_level;
	int need_errmsg = 0;
	int retval = 0;

	de_dbg_indent_save(c, &saved_indent_level);
	index_entry_pos = d->index_pos+17*fnum;
	data_block_pos = dbuf_getu32x(c->infile, index_entry_pos, d->is_le);

	// The last "file" is usually not a file, but a "NULL terminator" with an
	// offset of 0. Not very useful, since we already know how long the list is.
	if(data_block_pos==0) {
		de_dbg(c, "end-of-file-list marker found");
		goto done;
	}

	de_dbg(c, "file #%d", (int)fnum);
	de_dbg_indent(c, 1);
	de_dbg(c, "index entry pos: %"I64_FMT, index_entry_pos);
	de_dbg(c, "data block pos: %"I64_FMT, data_block_pos);

	if(data_block_pos < d->index_size) {
		need_errmsg = 1;
		goto done;
	}

	if(de_getbyte(index_entry_pos+4)==0x00) {
		need_errmsg = 1; // missing file name?
		goto done;
	}

	fi = de_finfo_create(c);
	fname = ucstring_create(c);

	// Filenames are 13 bytes, NUL-padded.
	dbuf_read_to_ucstring(c->infile, index_entry_pos+4, 13, fname, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_finfo_set_name_from_ucstring(c, fi, fname, 0);
	fi->original_filename_flag = 1;
	de_dbg(c, "file name: \"%s\"", ucstring_getpsz_d(fname));

	dlen = dbuf_getu32x(c->infile, data_block_pos, d->is_le);
	dpos = data_block_pos+4;
	de_dbg(c, "file data at %"I64_FMT", len=%"I64_FMT, dpos, dlen);
	if(dpos+dlen > c->infile->len) {
		need_errmsg = 1;
		goto done;
	}

	dbuf_create_file_from_slice(c->infile, dpos, dlen, NULL, fi, 0);
	retval = 1;

done:
	if(need_errmsg) {
		de_err(c, "Bad file entry (#%d)", (int)fnum);
	}
	de_finfo_destroy(c, fi);
	ucstring_destroy(fname);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x15b580
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x11, -0x18(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x1658c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x75e7d
movq -0x8(%rbp), %rdi
leaq 0x135f58(%rip), %rsi    # 0x1abdc9
movb $0x0, %al
callq 0x15b0d0
jmp 0x76020
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x135f59(%rip), %rsi    # 0x1abde7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x135f3e(%rip), %rsi    # 0x1abdf0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x135f3d(%rip), %rsi    # 0x1abe05
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x75ee9
movl $0x1, -0x50(%rbp)
jmp 0x76020
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x4, %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x75f12
movl $0x1, -0x50(%rbp)
jmp 0x76020
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x4, %rsi
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0xd, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x40(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rdi
callq 0x172650
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0x149d7c(%rip), %rsi    # 0x1bfd0d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x1658c0
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq 0x14d390(%rip), %rsi    # 0x1c3363
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x75ff9
movl $0x1, -0x50(%rbp)
jmp 0x76020
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %r8
xorl %eax, %eax
movl %eax, %ecx
xorl %r9d, %r9d
callq 0x166be0
movl $0x1, -0x54(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x7603e
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x135de2(%rip), %rsi    # 0x1abe19
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15d260
movq -0x48(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x15b5a0
movl -0x54(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)