// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/rpm.c
// Function: de_run_rpm
static void de_run_rpm(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 pos;
	u8 buf[8];
	const char *ext;
	i64 section_size = 0;
	de_finfo *fi = NULL;
	char filename[128];

	d = de_malloc(c, sizeof(lctx));

	if(!do_lead_section(c, d)) {
		goto done;
	}

	pos = 96;

	if(!do_header_structure(c, d, 1, pos, &section_size)) {
		goto done;
	}
	pos += section_size;

	// Header structures are 8-byte aligned. The first one always starts at
	// offset 96, so we don't have to worry about it. But we need to make
	// sure the second one is aligned.
	pos = ((pos + 7)/8)*8;

	if(!do_header_structure(c, d, 0, pos, &section_size)) {
		goto done;
	}
	pos += section_size;

	de_dbg(c, "data pos: %d", (int)pos);
	if(pos > c->infile->len) goto done;

	// There is usually a tag that indicates the compression format, but we
	// primarily figure out the format by sniffing its magic number, on the
	// theory that that's more reliable.

	// TODO: I think it's also theoretically possible that it could use an archive
	// format other than cpio.

	de_read(buf, pos, 8);

	if(buf[0]==0x1f && buf[1]==0x8b) {
		ext = "cpio.gz";
	}
	else if(buf[0]==0x42 && buf[1]==0x5a && buf[2]==0x68) {
		ext = "cpio.bz2";
	}
	else if(buf[0]==0xfd && buf[1]==0x37 && buf[2]==0x7a) {
		ext = "cpio.xz";
	}
	else if(d->cmpr_type==DE_RPM_CMPR_LZMA || buf[0]==0x5d) {
		ext = "cpio.lzma";
	}
	else {
		de_warn(c, "Unidentified compression or archive format");
		ext = "cpio.bin";
	}

	if(d->name_srd && c->filenames_from_file) {
		const char *version2 = "x";
		const char *release2 = "x";

		if(d->version_srd) version2 = d->version_srd->sz;
		if(d->release_srd) release2 = d->release_srd->sz;

		fi = de_finfo_create(c);
		de_snprintf(filename, sizeof(filename), "%s-%s.%s",
			d->name_srd->sz, version2, release2);
		de_finfo_set_name_from_sz(c, fi, filename, 0, DE_ENCODING_ASCII);
	}

	dbuf_create_file_from_slice(c->infile, pos, c->infile->len - pos, ext, fi, 0);

done:
	de_finfo_destroy(c, fi);
	if(d) {
		de_destroy_stringreaderdata(c, d->name_srd);
		de_destroy_stringreaderdata(c, d->release_srd);
		de_destroy_stringreaderdata(c, d->version_srd);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0x20, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1224c0
cmpl $0x0, %eax
jne 0x122184
jmp 0x122409
movq $0x60, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
leaq -0x38(%rbp), %r8
callq 0x1225a0
cmpl $0x0, %eax
jne 0x1221b0
jmp 0x122409
movq -0x38(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
shlq $0x3, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rcx
xorl %edx, %edx
leaq -0x38(%rbp), %r8
callq 0x1225a0
cmpl $0x0, %eax
jne 0x1221f7
jmp 0x122409
movq -0x38(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x7d897(%rip), %rsi     # 0x19faab
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x122232
jmp 0x122409
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x8, %ecx
callq 0x164d10
movzbl -0x28(%rbp), %eax
cmpl $0x1f, %eax
jne 0x122270
movzbl -0x27(%rbp), %eax
cmpl $0x8b, %eax
jne 0x122270
leaq 0xa40d1(%rip), %rax     # 0x1c6338
movq %rax, -0x30(%rbp)
jmp 0x122305
movzbl -0x28(%rbp), %eax
cmpl $0x42, %eax
jne 0x122298
movzbl -0x27(%rbp), %eax
cmpl $0x5a, %eax
jne 0x122298
movzbl -0x26(%rbp), %eax
cmpl $0x68, %eax
jne 0x122298
leaq 0xa40ae(%rip), %rax     # 0x1c6340
movq %rax, -0x30(%rbp)
jmp 0x122303
movzbl -0x28(%rbp), %eax
cmpl $0xfd, %eax
jne 0x1222c2
movzbl -0x27(%rbp), %eax
cmpl $0x37, %eax
jne 0x1222c2
movzbl -0x26(%rbp), %eax
cmpl $0x7a, %eax
jne 0x1222c2
leaq 0xa408d(%rip), %rax     # 0x1c6349
movq %rax, -0x30(%rbp)
jmp 0x122301
movq -0x18(%rbp), %rax
cmpl $0x3, 0x4(%rax)
je 0x1222d5
movzbl -0x28(%rbp), %eax
cmpl $0x5d, %eax
jne 0x1222e2
leaq 0xa4075(%rip), %rax     # 0x1c6351
movq %rax, -0x30(%rbp)
jmp 0x1222ff
movq -0x8(%rbp), %rdi
leaq 0xa406e(%rip), %rsi     # 0x1c635b
movb $0x0, %al
callq 0x15bc10
leaq 0xa408b(%rip), %rax     # 0x1c6386
movq %rax, -0x30(%rbp)
jmp 0x122301
jmp 0x122303
jmp 0x122305
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1223dd
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %eax
cmpl $0x0, %eax
je 0x1223dd
leaq 0xaf85d(%rip), %rax     # 0x1d1b8c
movq %rax, -0xc8(%rbp)
leaq 0xaf84f(%rip), %rax     # 0x1d1b8c
movq %rax, -0xd0(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x122362
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x122380
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x40(%rbp)
leaq -0xc0(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rcx
movq -0xc8(%rbp), %r8
movq -0xd0(%rbp), %r9
movl $0x80, %esi
leaq 0xa3fd5(%rip), %rdx     # 0x1c638f
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x15d4c0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %r8
xorl %r9d, %r9d
callq 0x166be0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15d260
cmpq $0x0, -0x18(%rbp)
je 0x12245d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)