// File: /workspace/github_c_makefile_star2_O2/jkbenaim[P]psfrag/psfrag.c
// Function: cmd_depends
char *cmd_depends(int argc, char **argv)
{
	__label__ out_return, out_dbclose, out_unmap, out_droptable;
	struct MappedFile_s m;
	char *msg = NULL;
	int rc;
	int fragnum;
	char pcode[6];

	switch (argc) {
	case 0 ... 2:
		msg = "must specify a Pokemon Stadium rom";
		goto out_return;
		break;
	case 3:
		msg = "must specify a fragment number";
		goto out_return;
		break;
	default:
		break;
	}

	rc = DB_Init(&db, ":memory:");
	if (rc != SQLITE_OK) {
		msg = "DB_Init oopsed";
		goto out_return;
	}

	m = MappedFile_Open(argv[2], false);
	if (m.data == NULL) {
		msg = "couldn't open rom";
		goto out_dbclose;
	}

	if (m.size < (1048576 + 4096)) {
		msg = "rom too small";
		goto out_unmap;
	}

	get_pcode(pcode, m.data);

	rc = DB_FragSearch(db, m.data, m.size);
	if (rc != SQLITE_OK) {
		msg = "DB_FragSearch oopsed";
		goto out_unmap;
	}

	fragnum = atoi(argv[3]);
	int fragaddr = DB_GetAddrForNum(db, fragnum);
	if (fragaddr == -1) {
		msg = "no fragment by that number";
		goto out_unmap;
	}
	//parse_relocs(m.data + fragaddr);
	uint32_t *fragbytes = m.data + fragaddr;

	// start

	rc = sqlite3_exec(
		db,
		"create temp table relocs(reloc_id integer primary key, pcode, fragnum, far, type, addr, target_addr, target_frag);",
		NULL, NULL, NULL
	);
	if (rc != SQLITE_OK) {
		msg = "create temp table failed";
		goto out_unmap;
	};

	sqlite3_stmt *stmt = NULL;
	rc = sqlite3_prepare_v2(
		db,
		"insert into temp.relocs(pcode, fragnum, far, type, addr, target_addr, target_frag) values (?, ?, ?, ?, ?, ?, ?);",
		-1,
		&stmt,
		NULL
	);
	if (rc != SQLITE_OK) {
		msg = "prepare";
		goto out_droptable;
	}

	rc = DB_Begin(db);
	if (rc != SQLITE_OK) {
		msg = "begin transaction";
		goto out_droptable;
	}
	struct fragment_s frag;
	memcpy(&frag, fragbytes, sizeof(struct fragment_s));
	uint32_t *words = (uint32_t *)fragbytes;
	uint32_t *reloc_words = words;
	reloc_words += htonl(frag.offset_relocs) / sizeof(uint32_t);
	uint32_t num_relocs = htonl(reloc_words[0]);
	reloc_words++;
	printf("%d relocations.\n", num_relocs);
	for(int i = 0; i<num_relocs; i++) {
		uint32_t reloc = htonl(reloc_words[i]);
		bool foreign = reloc & 0x80000000;
		uint32_t type = reloc & 0x7F000000;
		uint32_t addr = reloc & 0x00FFFFFF;
		uint32_t target = htonl(words[addr>>2]);
		uint32_t loc = -1;
		uint32_t n;
		int loc_fragnum = -2;

		char *zType = NULL;

		switch (type) {
		case 0x02000000:
			zType = "ptr";
			loc = target;
			break;
		case 0x04000000:
			zType = "j";
			loc = (target & 0x03FFFFFF) << 2;
			loc |= 0x80000000;
			break;
		case 0x05000000:
			zType = "lui";
			loc = (target & 0x0000FFFF) << 16;
			loc |= 0x80000000;
			break;
		case 0x06000000:
			zType = "addiu";
			loc = (target & 0x0000FFFF);
			if (!foreign)
				loc += get_vma(&frag);
			break;
		default:
			zType = "unknown";
			loc = -1;
			break;
		}

		n = (loc & 0x0FF00000)>>20;
		loc_fragnum = n - 16;
// 		printf("%5d %08x %8x %08x\t%s\t%d\n",
// 			i,
// 			reloc,
// 			addr,
// 			loc,
// 			zType,
// 			loc_fragnum
// 		);
		if (loc_fragnum < 0) continue;
// "insert into temp.relocs(pcode, fragnum, far, type, addr, target_addr, target_frag) values (?, ?, ?, ?, ?, ?, ?);"
		rc = sqlite3_bind_text(stmt, 1, pcode, -1, SQLITE_TRANSIENT);
		if (rc != SQLITE_OK) {msg = "bind 1";}
		rc = sqlite3_bind_int64(stmt, 2, fragnum);
		if (rc != SQLITE_OK) {msg = "bind 2";}
		rc = sqlite3_bind_int64(stmt, 3, foreign);
		if (rc != SQLITE_OK) {msg = "bind 3";}
		rc = sqlite3_bind_text(stmt, 4, zType, -1, SQLITE_STATIC);
		if (rc != SQLITE_OK) {msg = "bind 4";}
		rc = sqlite3_bind_int64(stmt, 5, addr);
		if (rc != SQLITE_OK) {msg = "bind 5";}
		rc = sqlite3_bind_int64(stmt, 6, loc);
		if (rc != SQLITE_OK) {msg = "bind 6";}
		rc = sqlite3_bind_int64(stmt, 7, loc_fragnum);
		if (rc != SQLITE_OK) {msg = "bind 7";}

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {msg = "step";}

		rc = sqlite3_reset(stmt);
		if (rc != SQLITE_OK) {msg = "reset";}
	}

	DB_End(db);

	rc = sqlite3_finalize(stmt);
	if (rc != SQLITE_OK) {msg = "finalize1"; goto out_droptable;}

	rc = sqlite3_prepare(
		db,
		"select distinct target_frag from temp.relocs where target_frag != fragnum order by target_frag;",
		-1,
		&stmt,
		NULL
	);
	if (rc != SQLITE_OK) { msg = "prepare2"; goto out_droptable;}

	bool did_print_first = false;
	while (SQLITE_DONE != (rc=sqlite3_step(stmt))) switch (rc) {
	case SQLITE_BUSY:
		break;
	case SQLITE_ERROR:
		msg = "sqlite3_step returned SQLITE_ERROR";
		goto out_finalize;
		break;
	case SQLITE_MISUSE:
		msg = "sqlite3_step returned SQLITE_MISUSE";
		goto out_finalize;
		break;
	default:
		msg = "sqlite3_step returned some other error";
		goto out_finalize;
		break;
	case SQLITE_ROW:
		printf("%s%d", did_print_first?", ":"Depends on ",sqlite3_column_int(stmt, 0));
		did_print_first = true;
		break;
	}

	if (did_print_first) {
		printf(".\n");
	} else {
		printf("No dependencies.\n");
	}

out_finalize:
	rc = sqlite3_finalize(stmt);
	if (!msg)
		if (rc != SQLITE_OK) {msg = "finalize2"; goto out_droptable;}
out_droptable:
	rc = sqlite3_exec(
		db,
		"drop table temp.relocs;",
		NULL, NULL, NULL
	);
out_unmap:
	MappedFile_Close(m);
out_dbclose:
	DB_Close(db);
out_return:
	if (msg) {
		return msg;
	} else {
		return NULL;
	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpl $0x4, %edi
jae 0x8575
movl %edi, %eax
leaq 0x929d4(%rip), %rcx     # 0x9af40
movq (%rcx,%rax,8), %rbx
jmp 0x8624
movq %rsi, %r14
leaq 0x96c89(%rip), %rdi     # 0x9f208
leaq 0x6be19(%rip), %rsi     # 0x7439f
callq 0x92a8
testl %eax, %eax
je 0x859b
leaq 0x6be12(%rip), %rbx     # 0x743a8
jmp 0x8624
movq 0x10(%r14), %rsi
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x97bb
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x85cd
movq 0x60(%rsp), %r15
cmpq $0x101000, %r15         # imm = 0x101000
jae 0x85d6
leaq 0x6bdfe(%rip), %rbx     # 0x743c9
jmp 0x8600
leaq 0x6bde3(%rip), %rbx     # 0x743b7
jmp 0x8618
leaq 0x32(%rsp), %rdi
movq %rbx, %rsi
callq 0x9860
movq 0x96c1e(%rip), %rdi     # 0x9f208
movq %rbx, %rsi
movq %r15, %rdx
callq 0x959b
testl %eax, %eax
je 0x8639
leaq 0x6bdd7(%rip), %rbx     # 0x743d7
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x58(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x9845
movq 0x96be9(%rip), %rdi     # 0x9f208
callq 0x92d4
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r14), %rdi
callq 0x82d0
movl %eax, %r14d
movq 0x96bbc(%rip), %rdi     # 0x9f208
movl %eax, %esi
callq 0x94e2
cmpl $-0x1, %eax
je 0x8685
movl %eax, %r15d
movq 0x96ba6(%rip), %rdi     # 0x9f208
leaq 0x6bee8(%rip), %rsi     # 0x74551
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x131ca
testl %eax, %eax
je 0x8691
leaq 0x6bf44(%rip), %rbx     # 0x745c4
jmp 0x8600
leaq 0x6bdc3(%rip), %rbx     # 0x7444f
jmp 0x8600
leaq 0x20(%rsp), %rcx
andq $0x0, (%rcx)
movq 0x96b67(%rip), %rdi     # 0x9f208
leaq 0x6bf35(%rip), %rsi     # 0x745dd
pushq $-0x1
popq %rdx
xorl %r8d, %r8d
callq 0x13571
testl %eax, %eax
je 0x86c3
leaq 0x6c3c6(%rip), %rbx     # 0x74a84
jmp 0x8a48
movq 0x96b3e(%rip), %rdi     # 0x9f208
callq 0x92d9
testl %eax, %eax
je 0x86df
leaq 0x6bf74(%rip), %rbx     # 0x7464e
jmp 0x8a48
movslq %r15d, %rax
addq %rax, %rbx
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
leaq 0x70(%rsp), %rax
movaps %xmm1, 0x10(%rax)
movaps %xmm0, (%rax)
movl 0x14(%rax), %eax
bswapl %eax
andl $-0x4, %eax
leaq (%rbx,%rax), %rcx
movq %rcx, 0x40(%rsp)
movl (%rbx,%rax), %esi
bswapl %esi
leaq 0x6bf4b(%rip), %rdi     # 0x74660
movq %rsi, 0x48(%rsp)
xorl %eax, %eax
callq 0x80e0
xorl %esi, %esi
movslq %r14d, %rax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
pushq $-0x1
popq %rcx
cmpq %rsi, 0x48(%rsp)
je 0x893b
movq 0x40(%rsp), %rax
movl 0x4(%rax,%rsi,4), %r14d
bswapl %r14d
movl %r14d, %eax
andl $0x7f000000, %eax       # imm = 0x7F000000
addl $0xfe000000, %eax       # imm = 0xFE000000
shrl $0x18, %eax
movl $0xff, %r13d
cmpl $0x4, %eax
ja 0x892d
movl %r14d, %edx
andl $0xfffffc, %edx         # imm = 0xFFFFFC
movl (%rbx,%rdx), %ebp
bswapl %ebp
leaq 0x6b887(%rip), %rdx     # 0x74004
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
leaq 0x6bef6(%rip), %r12     # 0x74681
movl %ecx, %edx
jmpq *%rax
leaq 0x6bedb(%rip), %r12     # 0x74671
jmp 0x87e6
shll $0x10, %ebp
orl $0x80000000, %ebp       # imm = 0x80000000
leaq 0x6becf(%rip), %r12     # 0x74677
jmp 0x87e6
andl $0x3ffffff, %ebp        # imm = 0x3FFFFFF
leal -0x80000000(,%rbp,4), %ebp
leaq 0x6beb7(%rip), %r12     # 0x74675
jmp 0x87e6
movzwl %bp, %ebp
testl %r14d, %r14d
js 0x87df
leaq 0x70(%rsp), %rdi
movq %rsi, %r12
callq 0x96f0
movq %r12, %rsi
pushq $-0x1
popq %rcx
addl %ebp, %eax
movl %eax, %ebp
leaq 0x6be95(%rip), %r12     # 0x7467b
movl %ebp, %eax
shrl $0x14, %eax
movzbl %al, %r13d
movl %ebp, %edx
cmpl $0x10, %r13d
jb 0x8925
movl %edx, 0x2c(%rsp)
movq %rsi, 0x50(%rsp)
movl %r14d, %ebp
andl $0xffffff, %ebp         # imm = 0xFFFFFF
addl $-0x10, %r13d
movq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
leaq 0x32(%rsp), %rdx
pushq $-0x1
popq %r8
callq 0x1123b
testl %eax, %eax
leaq 0x6be59(%rip), %rax     # 0x74689
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
pushq $0x2
popq %rsi
movq 0x38(%rsp), %rdx
callq 0x1111b
testl %eax, %eax
leaq 0x6be41(%rip), %rax     # 0x74690
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
shrl $0x1f, %r14d
pushq $0x3
popq %rsi
movq %r14, %rdx
callq 0x1111b
testl %eax, %eax
leaq 0x6be27(%rip), %rax     # 0x74697
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r12, %rdx
pushq $-0x1
popq %rcx
xorl %r8d, %r8d
callq 0x1123b
testl %eax, %eax
leaq 0x6be0b(%rip), %rax     # 0x7469e
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
movl %ebp, %edx
pushq $0x5
popq %rsi
callq 0x1111b
testl %eax, %eax
leaq 0x6bdf6(%rip), %rax     # 0x746a5
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %edx
pushq $0x6
popq %rsi
callq 0x1111b
testl %eax, %eax
leaq 0x6bddf(%rip), %rax     # 0x746ac
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
pushq $0x7
popq %rsi
movq %r13, %rdx
callq 0x1111b
testl %eax, %eax
leaq 0x6bdc9(%rip), %rax     # 0x746b3
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
callq 0xfe1e
cmpl $0x65, %eax
leaq 0x6c174(%rip), %rax     # 0x74a76
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
callq 0xf3b8
movq 0x50(%rsp), %rsi
pushq $-0x1
popq %rcx
testl %eax, %eax
leaq 0x6bd99(%rip), %rax     # 0x746ba
cmovneq %rax, %r15
incq %rsi
jmp 0x8731
leaq 0x6bd4d(%rip), %r12     # 0x74681
movl %ecx, %edx
jmp 0x87fb
movq 0x968c6(%rip), %rdi     # 0x9f208
callq 0x92ec
movq 0x20(%rsp), %rdi
callq 0xf24e
testl %eax, %eax
je 0x8961
leaq 0x6bd64(%rip), %rbx     # 0x746c0
jmp 0x8a48
movq 0x968a0(%rip), %rdi     # 0x9f208
leaq 0x6bd5b(%rip), %rsi     # 0x746ca
pushq $-0x1
popq %rdx
leaq 0x20(%rsp), %rcx
xorl %r8d, %r8d
callq 0x13c24
testl %eax, %eax
je 0x898f
leaq 0x6bda0(%rip), %rbx     # 0x7472a
jmp 0x8a48
xorl %ebp, %ebp
leaq 0x6bda0(%rip), %r12     # 0x74738
leaq 0x6bd9c(%rip), %r13     # 0x7473b
leaq 0x6bd8d(%rip), %rbx     # 0x74733
movq 0x20(%rsp), %rdi
callq 0xfe1e
cmpl $0x5, %eax
je 0x89a6
cmpl $0x1, %eax
je 0x8a0c
cmpl $0x15, %eax
je 0x89f4
cmpl $0x65, %eax
je 0x89fd
cmpl $0x64, %eax
jne 0x8a15
testb $0x1, %bpl
movq %r13, %r14
cmovneq %r12, %r14
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x10cc5
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x80e0
movb $0x1, %bpl
jmp 0x89a6
leaq 0x6b903(%rip), %r15     # 0x742fe
jmp 0x8a2a
testb $0x1, %bpl
je 0x8a1e
leaq 0x72171(%rip), %rdi     # 0x7ab7b
jmp 0x8a25
leaq 0x6b8c8(%rip), %r15     # 0x742db
jmp 0x8a2a
leaq 0x6b906(%rip), %r15     # 0x74322
jmp 0x8a2a
leaq 0x6be27(%rip), %rdi     # 0x7484c
callq 0x8090
movq 0x20(%rsp), %rdi
callq 0xf24e
testl %eax, %eax
leaq 0x6bd0a(%rip), %rbx     # 0x74747
cmoveq %r15, %rbx
testq %r15, %r15
cmovneq %r15, %rbx
movq 0x967b9(%rip), %rdi     # 0x9f208
leaq 0x6bcfb(%rip), %rsi     # 0x74751
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x131ca
jmp 0x8600