// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-mgs/zar-extract.c
// Function: main
int main( int argc, char **argv ) {
	
	char *name = NULL;
	unsigned char *comdata = NULL, *uncomdata = NULL;
	uint32_t *size;
	unsigned int memoff, len;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s _zar-file\n", argv[0]);
		return 1;
	}
	
	uint32_t numfiles, workfiles, uncomsize, filesize;
	unsigned long int havebuf;
	
	FILE *f, *o;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_END);
	filesize = ftell(f);
	fseek(f, 0, SEEK_SET);
	
	fread( &uncomsize, 4, 1, f );
	
	printf("Compressed size %08x (file is %08x)\n", filesize-4, filesize);
	printf("Uncompressed size: %08x\n", uncomsize );
	havebuf = uncomsize;
	
	comdata = malloc(filesize-4);
	uncomdata = malloc(uncomsize);
	
	fread(comdata, filesize-4, 1, f);
	
	if((workfiles = uncompress(uncomdata, &havebuf, comdata, filesize-4)) != Z_OK) {
		printf("=====================================couldnt decompress data\n");
		switch(workfiles) {
			case Z_MEM_ERROR: {
				printf("memory error\n");
				break;
			}
			case Z_NEED_DICT: {
				printf("need dictionary\n");
				break;
			}
			case Z_DATA_ERROR: {
				printf("input corrupted\n");
				//~ printf("%s\n", strm.msg);
				break;
			}
			case Z_STREAM_ERROR: {
				printf("broken stream\n");
				break;
			}
			case Z_BUF_ERROR: {
				printf("buffer error\n");
				break;
			}
			default: {
				printf("unknown error\n");
				break;
			}
		}
		printf("uncomsize %08x\n", uncomsize);
		return 1;
	}
	free(comdata);
	fclose(f);
	
	numfiles = *uncomdata;
	
	printf("Number of files: %u\n", numfiles);
	
	memoff = 4;
	
	for( workfiles = 0; workfiles < numfiles; workfiles++ ) {
		printf("offset %08x\n", memoff);
		name = (char *)uncomdata+memoff;
		len = strlen(name) + 1;
		if((memoff+len)%4) len+= 4-((memoff+len)%4);
		memoff += len;
		
		printf("offset %08x\n", memoff);
		size = (uint32_t *)(uncomdata+memoff);
		len = 4;
		if((memoff+len)%0x10) len += 0x10-((memoff+len)%0x10);
		memoff += len;
		
		printf("Current file: %s - Size %08x\n", name, *size);
		
		if( !(o = fopen( name, "wb" ))) {
			printf("Couldnt open output %s\n", name);
			return 1;
		}
		
		fwrite(uncomdata+memoff, *size, 1, o);
		
		fclose(o);
		
		memoff += *size + 1;
	}
	
	free(uncomdata);
	printf("Done.\n");
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1385
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xd00(%rip), %rdi       # 0x2072
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x16f1
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd02(%rip), %rsi       # 0x2096
callq 0x10c0
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x13c5
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xce7(%rip), %rdi       # 0x2099
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x16f1
movq -0x58(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10b0
movq -0x58(%rbp), %rdi
callq 0x1090
movl %eax, -0x48(%rbp)
movq -0x58(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
movq -0x58(%rbp), %rcx
leaq -0x44(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1040
movl -0x48(%rbp), %esi
subl $0x4, %esi
movl -0x48(%rbp), %edx
leaq 0xc96(%rip), %rdi       # 0x20af
movb $0x0, %al
callq 0x1080
movl -0x44(%rbp), %esi
leaq 0xcaa(%rip), %rdi       # 0x20d4
movb $0x0, %al
callq 0x1080
movl -0x44(%rbp), %eax
movq %rax, -0x50(%rbp)
movl -0x48(%rbp), %eax
subl $0x4, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x10a0
movq %rax, -0x20(%rbp)
movl -0x44(%rbp), %eax
movl %eax, %edi
callq 0x10a0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movl -0x48(%rbp), %eax
subl $0x4, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x58(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl -0x48(%rbp), %eax
subl $0x4, %eax
movl %eax, %eax
movl %eax, %ecx
leaq -0x50(%rbp), %rsi
callq 0x1060
movl %eax, -0x40(%rbp)
cmpl $0x0, %eax
je 0x154a
leaq 0xc4a(%rip), %rdi       # 0x20ed
xorl %eax, %eax
callq 0x1080
movl -0x40(%rbp), %eax
addl $0x5, %eax
movl %eax, %ecx
movq %rcx, -0x68(%rbp)
subl $0x7, %eax
ja 0x151f
movq -0x68(%rbp), %rax
leaq 0xb3e(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xc55(%rip), %rdi       # 0x212b
movb $0x0, %al
callq 0x1080
jmp 0x152d
leaq 0xc53(%rip), %rdi       # 0x2139
movb $0x0, %al
callq 0x1080
jmp 0x152d
leaq 0xc54(%rip), %rdi       # 0x214a
movb $0x0, %al
callq 0x1080
jmp 0x152d
leaq 0xc55(%rip), %rdi       # 0x215b
movb $0x0, %al
callq 0x1080
jmp 0x152d
leaq 0xc54(%rip), %rdi       # 0x216a
movb $0x0, %al
callq 0x1080
jmp 0x152d
leaq 0xc52(%rip), %rdi       # 0x2178
movb $0x0, %al
callq 0x1080
movl -0x44(%rbp), %esi
leaq 0xc50(%rip), %rdi       # 0x2187
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x16f1
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x58(%rbp), %rdi
callq 0x1050
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %esi
leaq 0xc27(%rip), %rdi       # 0x2197
movb $0x0, %al
callq 0x1080
movl $0x4, -0x34(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x16d3
movl -0x34(%rbp), %esi
leaq 0xc11(%rip), %rdi       # 0x21ac
movb $0x0, %al
callq 0x1080
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
addq $0x1, %rax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
addl -0x38(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x15e4
movl -0x34(%rbp), %ecx
addl -0x38(%rbp), %ecx
andl $0x3, %ecx
movl $0x4, %eax
subl %ecx, %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %esi
leaq 0xbb5(%rip), %rdi       # 0x21ac
movb $0x0, %al
callq 0x1080
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x4, -0x38(%rbp)
movl -0x34(%rbp), %eax
addl -0x38(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x1637
movl -0x34(%rbp), %ecx
addl -0x38(%rbp), %ecx
andl $0xf, %ecx
movl $0x10, %eax
subl %ecx, %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rax
movl (%rax), %edx
leaq 0xb68(%rip), %rdi       # 0x21b9
movb $0x0, %al
callq 0x1080
movq -0x18(%rbp), %rdi
leaq 0xb74(%rip), %rsi       # 0x21d7
callq 0x10c0
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
jne 0x168d
movq -0x18(%rbp), %rsi
leaq 0xb5d(%rip), %rdi       # 0x21da
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x16f1
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %eax
addq %rax, %rdi
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, %esi
movq -0x60(%rbp), %rcx
movl $0x1, %edx
callq 0x10e0
movq -0x60(%rbp), %rdi
callq 0x1050
movq -0x30(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1585
movq -0x28(%rbp), %rdi
callq 0x1030
leaq 0xb0f(%rip), %rdi       # 0x21f2
movb $0x0, %al
callq 0x1080
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq