// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ar.c
// Function: do_ar_item
static int do_ar_item(deark *c, lctx *d, i64 pos1, i64 *p_item_len)
{
	char name_orig[17];
	size_t name_orig_len;
	de_ucstring *rawname_ucstring = NULL;
	de_ucstring *filename_ucstring = NULL;
	char timestamp_buf[64];
	i64 mod_time;
	i64 file_mode;
	i64 file_offset;
	i64 file_size = 0;
	i64 name_offset;
	de_finfo *fi = NULL;
	i64 k;
	int retval = 0;
	int ret;
	i64 foundpos;
	i64 ext_name_len;

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

	fi = de_finfo_create(c);

	de_read((u8*)name_orig, pos1, 16);
	// Strip trailing spaces
	name_orig[16] = '\0';
	for(k=15; k>=0; k--) {
		if(name_orig[k]!=' ') break;
		name_orig[k]='\0';
	}
	name_orig_len = de_strlen(name_orig);

	rawname_ucstring = ucstring_create(c);
	ucstring_append_bytes(rawname_ucstring, (const u8*)name_orig, name_orig_len, 0,
		d->input_encoding);

	de_dbg(c, "member raw name: \"%s\"", ucstring_getpsz(rawname_ucstring));

	(void)dbuf_read_ascii_number(c->infile, pos1+16, 12, 10, &mod_time);
	de_unix_time_to_timestamp(mod_time, &fi->timestamp[DE_TIMESTAMPIDX_MODIFY], 0x1);
	de_timestamp_to_string(&fi->timestamp[DE_TIMESTAMPIDX_MODIFY], timestamp_buf, sizeof(timestamp_buf), 0);
	de_dbg(c, "mod time: %" I64_FMT " (%s)", mod_time, timestamp_buf);

	(void)dbuf_read_ascii_number(c->infile, pos1+40, 8, 8, &file_mode);
	de_dbg(c, "file mode: octal(%06o)", (int)file_mode);
	if((file_mode & 0111)!=0) {
		fi->mode_flags |= DE_MODEFLAG_EXE;
	}
	else {
		fi->mode_flags |= DE_MODEFLAG_NONEXE;
	}

	file_offset = pos1 + 60;
	(void)dbuf_read_ascii_number(c->infile, pos1+48, 10, 10, &file_size);
	de_dbg(c, "member data at %"I64_FMT", size: %"I64_FMT,
		file_offset, file_size);

	if(name_orig_len<1) {
		de_warn(c, "Missing filename");
		retval = 1;
		goto done;
	}
	else if(!de_strcmp(name_orig, "/")) {
		de_dbg(c, "symbol table (ignoring)");
		retval = 1;
		goto done;
	}
	else if(!de_strcmp(name_orig, "//")) {
		de_dbg(c, "extended name table");
		d->extended_name_table_pos = file_offset;
		d->extended_name_table_size = file_size;
		retval = 1;
		goto done;
	}
	else if(name_orig[0]=='/' && name_orig[1]>='0' && name_orig[1]<='9') {
		if(d->extended_name_table_pos==0) {
			de_err(c, "Missing extended name table");
			goto done;
		}

		(void)dbuf_read_ascii_number(c->infile, pos1+1, 15, 10, &name_offset);
		if(name_offset >= d->extended_name_table_size) {
			goto done;
		}

		ret = dbuf_search_byte(c->infile, '\x0a', d->extended_name_table_pos+name_offset,
			d->extended_name_table_size-name_offset, &foundpos);
		if(!ret) goto done;
		ext_name_len = foundpos - (d->extended_name_table_pos+name_offset);

		// TODO: Consolidate the filename extraction code.
		if(ext_name_len>0 && de_getbyte(d->extended_name_table_pos+name_offset+ext_name_len-1)=='/') {
			// Strip trailing slash.
			ext_name_len--;
		}

		filename_ucstring = ucstring_create(c);
		dbuf_read_to_ucstring(c->infile, d->extended_name_table_pos+name_offset,
			ext_name_len, filename_ucstring, 0, d->input_encoding);

		de_dbg(c, "extended filename: \"%s\"", ucstring_getpsz(filename_ucstring));

		de_finfo_set_name_from_ucstring(c, fi, filename_ucstring, 0);
		fi->original_filename_flag = 1;
	}
	else if(name_orig[0]=='/') {
		de_warn(c, "Unsupported extension: \"%s\"", ucstring_getpsz(rawname_ucstring));
		retval = 1;
		goto done;
	}
	else {
		i64 adjusted_len;

		filename_ucstring = ucstring_create(c);

		adjusted_len = name_orig_len;
		if(name_orig[name_orig_len-1]=='/') {
			// Filenames are often terminated with a '/', to allow for
			// trailing spaces. Strip off the '/'.
			adjusted_len--;
		}
		ucstring_append_bytes(filename_ucstring, (u8*)name_orig, adjusted_len,
			0, d->input_encoding);

		de_dbg(c, "filename: \"%s\"", ucstring_getpsz(filename_ucstring));
		de_finfo_set_name_from_ucstring(c, fi, filename_ucstring, 0);
		fi->original_filename_flag = 1;
	}

	dbuf_create_file_from_slice(c->infile, file_offset, file_size, NULL, fi, 0);

	retval = 1;
done:
	*p_item_len = 60 + file_size;
	if(*p_item_len % 2) (*p_item_len)++; // padding byte
	de_dbg_indent(c, -1);
	de_finfo_destroy(c, fi);
	ucstring_destroy(rawname_ucstring);
	ucstring_destroy(filename_ucstring);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xd0(%rbp)
movl $0x0, -0xdc(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x1a5130(%rip), %rsi    # 0x1c738a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0xd0(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl $0x10, %ecx
callq 0x164d10
movb $0x0, -0x30(%rbp)
movq $0xf, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jl 0x222e5
movq -0xd8(%rbp), %rax
movsbl -0x40(%rbp,%rax), %eax
cmpl $0x20, %eax
je 0x222c5
jmp 0x222e5
movq -0xd8(%rbp), %rax
movb $0x0, -0x40(%rbp,%rax)
movq -0xd8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x222a8
leaq -0x40(%rbp), %rdi
callq 0x190e0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x10(%rbp), %rax
movl (%rax), %r8d
xorl %ecx, %ecx
callq 0x171fa0
movq -0x8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x50(%rbp), %rdi
callq 0x172500
movq -0x100(%rbp), %rdi
movq %rax, %rdx
leaq 0x17bd78(%rip), %rsi    # 0x19e0b6
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x10, %rsi
movl $0xc, %edx
movl $0xa, %ecx
leaq -0xa8(%rbp), %r8
callq 0x165f70
movq -0xa8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
addq $0x18, %rsi
movl $0x1, %edx
callq 0x15d530
movq -0xd0(%rbp), %rdi
addq $0x18, %rdi
leaq -0xa0(%rbp), %rsi
movl $0x40, %edx
xorl %ecx, %ecx
callq 0x15e130
movq -0x8(%rbp), %rdi
movq -0xa8(%rbp), %rdx
leaq -0xa0(%rbp), %rcx
leaq 0x17bd0e(%rip), %rsi    # 0x19e0cc
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x28, %rsi
movl $0x8, %edx
movl $0x8, %ecx
leaq -0xb0(%rbp), %r8
callq 0x165f70
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rax
movl %eax, %edx
leaq 0x17bce0(%rip), %rsi    # 0x19e0df
movb $0x0, %al
callq 0x15b0d0
movq -0xb0(%rbp), %rax
andq $0x49, %rax
cmpq $0x0, %rax
je 0x22429
movq -0xd0(%rbp), %rax
movl 0x14(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x14(%rax)
jmp 0x22439
movq -0xd0(%rbp), %rax
movl 0x14(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
addq $0x3c, %rax
movq %rax, -0xb8(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x30, %rsi
movl $0xa, %edx
movl $0xa, %ecx
leaq -0xc0(%rbp), %r8
callq 0x165f70
movq -0x8(%rbp), %rdi
movq -0xb8(%rbp), %rdx
movq -0xc0(%rbp), %rcx
leaq 0x17bc6f(%rip), %rsi    # 0x19e0f6
movb $0x0, %al
callq 0x15b0d0
cmpq $0x1, -0x48(%rbp)
jae 0x224b6
movq -0x8(%rbp), %rdi
leaq 0x17bc74(%rip), %rsi    # 0x19e114
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0xdc(%rbp)
jmp 0x22830
leaq -0x40(%rbp), %rdi
leaq 0x17bc7d(%rip), %rsi    # 0x19e13e
callq 0x191a0
cmpl $0x0, %eax
jne 0x224ec
movq -0x8(%rbp), %rdi
leaq 0x17bc4f(%rip), %rsi    # 0x19e125
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0xdc(%rbp)
jmp 0x22830
leaq -0x40(%rbp), %rdi
leaq 0x17bc46(%rip), %rsi    # 0x19e13d
callq 0x191a0
cmpl $0x0, %eax
jne 0x22540
movq -0x8(%rbp), %rdi
leaq 0x17bc3c(%rip), %rsi    # 0x19e148
movb $0x0, %al
callq 0x15b0d0
movq -0xb8(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0xc0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x1, -0xdc(%rbp)
jmp 0x22830
movsbl -0x40(%rbp), %eax
cmpl $0x2f, %eax
jne 0x2270b
movsbl -0x3f(%rbp), %eax
cmpl $0x30, %eax
jl 0x2270b
movsbl -0x3f(%rbp), %eax
cmpl $0x39, %eax
jg 0x2270b
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x22589
movq -0x8(%rbp), %rdi
leaq 0x17bbc3(%rip), %rsi    # 0x19e140
movb $0x0, %al
callq 0x15bfb0
jmp 0x22830
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x1, %rsi
movl $0xf, %edx
movl $0xa, %ecx
leaq -0xc8(%rbp), %r8
callq 0x165f70
movq -0xc8(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0x225c5
jmp 0x22830
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
addq -0xc8(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
subq -0xc8(%rbp), %rcx
movl $0xa, %esi
leaq -0xe8(%rbp), %r8
callq 0x1667a0
movl %eax, -0xe0(%rbp)
cmpl $0x0, -0xe0(%rbp)
jne 0x22610
jmp 0x22830
movq -0xe8(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
addq -0xc8(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
jle 0x2267b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
addq -0xc8(%rbp), %rsi
addq -0xf0(%rbp), %rsi
subq $0x1, %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x2f, %eax
jne 0x2267b
movq -0xf0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xf0(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
addq -0xc8(%rbp), %rsi
movq -0xf0(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
xorl %r8d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x58(%rbp), %rdi
callq 0x172500
movq -0x108(%rbp), %rdi
movq %rax, %rdx
leaq 0x17ba7e(%rip), %rsi    # 0x19e15c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq -0x58(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0xd0(%rbp), %rax
movb $0x1, 0x8(%rax)
jmp 0x227f7
movsbl -0x40(%rbp), %eax
cmpl $0x2f, %eax
jne 0x2274f
movq -0x8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x50(%rbp), %rdi
callq 0x172500
movq -0x110(%rbp), %rdi
movq %rax, %rdx
leaq 0x17ba3b(%rip), %rsi    # 0x19e174
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0xdc(%rbp)
jmp 0x22830
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x48(%rbp), %rax
subq $0x1, %rax
movsbl -0x40(%rbp,%rax), %eax
cmpl $0x2f, %eax
jne 0x2278b
movq -0xf8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xf8(%rbp)
movq -0x58(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq -0xf8(%rbp), %rdx
movq -0x10(%rbp), %rax
movl (%rax), %r8d
xorl %ecx, %ecx
callq 0x171fa0
movq -0x8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x58(%rbp), %rdi
callq 0x172500
movq -0x118(%rbp), %rdi
movq %rax, %rdx
leaq 0x1a4db6(%rip), %rsi    # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq -0x58(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0xd0(%rbp), %rax
movb $0x1, 0x8(%rax)
jmp 0x227f7
jmp 0x227f9
jmp 0x227fb
jmp 0x227fd
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
movq -0xd0(%rbp), %r8
xorl %eax, %eax
movl %eax, %ecx
xorl %r9d, %r9d
callq 0x166be0
movl $0x1, -0xdc(%rbp)
movq -0xc0(%rbp), %rcx
addq $0x3c, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x22867
movq -0x20(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x15d260
movq -0x50(%rbp), %rdi
callq 0x171750
movq -0x58(%rbp), %rdi
callq 0x171750
movl -0xdc(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)