// File: /workspace/github_c_makefile_star2_O1/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 0x8635
movl %edi, %eax
leaq 0x9f8f4(%rip), %rcx     # 0xa7f20
movq (%rcx,%rax,8), %rbx
jmp 0x86e2
movq %rsi, %rbx
leaq 0xa3bc9(%rip), %rdi     # 0xac208
leaq 0x7ed59(%rip), %rsi     # 0x8739f
callq 0x939c
testl %eax, %eax
je 0x865b
leaq 0x7ed52(%rip), %rbx     # 0x873a8
jmp 0x86e2
movq 0x10(%rbx), %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x992d
movq (%r14), %rsi
testq %rsi, %rsi
je 0x868a
cmpq $0x101000, 0x48(%rsp)   # imm = 0x101000
jae 0x8693
leaq 0x7ed41(%rip), %rbx     # 0x873c9
jmp 0x86be
leaq 0x7ed26(%rip), %rbx     # 0x873b7
jmp 0x86d6
leaq 0x3a(%rsp), %rdi
callq 0x99d8
movq 0xa3b64(%rip), %rdi     # 0xac208
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x96d2
testl %eax, %eax
je 0x86f7
leaq 0x7ed19(%rip), %rbx     # 0x873d7
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99be
movq 0xa3b2b(%rip), %rdi     # 0xac208
callq 0x93c8
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
callq 0x82d0
movl %eax, %ebp
movq 0xa3aff(%rip), %rdi     # 0xac208
movl %eax, %esi
callq 0x9606
cmpl $-0x1, %eax
je 0x8747
movslq %eax, %r12
addq 0x40(%rsp), %r12
movq 0xa3ae4(%rip), %rdi     # 0xac208
leaq 0x7ee26(%rip), %rsi     # 0x87551
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x14a2f
testl %eax, %eax
je 0x8753
leaq 0x7ee82(%rip), %rbx     # 0x875c4
jmp 0x86be
leaq 0x7ed01(%rip), %rbx     # 0x8744f
jmp 0x86be
leaq 0x20(%rsp), %rcx
movq $0x0, (%rcx)
movq 0xa3aa2(%rip), %rdi     # 0xac208
leaq 0x7ee70(%rip), %rsi     # 0x875dd
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x14ef3
testl %eax, %eax
je 0x878a
leaq 0x7f2ff(%rip), %rbx     # 0x87a84
jmp 0x8b48
movq 0xa3a77(%rip), %rdi     # 0xac208
callq 0x93cd
testl %eax, %eax
je 0x87a6
leaq 0x7eead(%rip), %rbx     # 0x8764e
jmp 0x8b48
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movaps %xmm1, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movl 0x84(%rsp), %r14d
bswapl %r14d
andl $-0x4, %r14d
movl (%r12,%r14), %ebx
movl %ebx, %r13d
bswapl %r13d
leaq 0x7ee82(%rip), %rdi     # 0x87660
xorl %r15d, %r15d
movl %r13d, %esi
xorl %eax, %eax
callq 0x80e0
testl %ebx, %ebx
je 0x8a34
addq %r12, %r14
cmpl $0x1, %r13d
adcl $0x0, %r13d
movslq %ebp, %rax
movq %rax, 0x58(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r14, %rdx
movq %r14, 0x30(%rsp)
movq %r13, %rsi
movq %r13, 0x28(%rsp)
movq %r12, 0x60(%rsp)
movl 0x4(%rdx,%rbx,4), %r13d
bswapl %r13d
movl %r13d, %eax
andl $0x7f000000, %eax       # imm = 0x7F000000
addl $0xfe000000, %eax       # imm = 0xFE000000
shrl $0x18, %eax
cmpl $0x4, %eax
ja 0x88c2
movl %r13d, %ecx
andl $0xfffffc, %ecx         # imm = 0xFFFFFC
movl (%r12,%rcx), %r14d
bswapl %r14d
leaq 0x7e7ac(%rip), %rcx     # 0x87004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x7ee09(%rip), %rcx     # 0x87671
jmp 0x88cf
shll $0x10, %r14d
orl $0x80000000, %r14d      # imm = 0x80000000
leaq 0x7edfb(%rip), %rcx     # 0x87677
jmp 0x88cf
andl $0x3ffffff, %r14d       # imm = 0x3FFFFFF
leal -0x80000000(,%r14,4), %r14d
leaq 0x7ede1(%rip), %rcx     # 0x87675
jmp 0x88cf
movzwl %r14w, %r14d
testl %r13d, %r13d
js 0x88b9
leaq 0x70(%rsp), %rdi
callq 0x985a
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addl %r14d, %eax
movl %eax, %r14d
leaq 0x7edbb(%rip), %rcx     # 0x8767b
jmp 0x88cf
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
leaq 0x7edb2(%rip), %rcx     # 0x87681
movl %r14d, %eax
shrl $0x14, %eax
movzbl %al, %ebp
cmpl $0x10, %ebp
jb 0x8a28
movl %r13d, %r12d
andl $0xffffff, %r12d        # imm = 0xFFFFFF
addl $-0x10, %ebp
movq 0x20(%rsp), %rdi
movl $0x1, %esi
leaq 0x3a(%rsp), %rdx
movq %rcx, 0x68(%rsp)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq $-0x1, %r8
callq 0x1260f
testl %eax, %eax
leaq 0x7ed6d(%rip), %rax     # 0x87689
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
movl $0x2, %esi
movq 0x58(%rsp), %rdx
callq 0x124d9
testl %eax, %eax
leaq 0x7ed53(%rip), %rax     # 0x87690
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
shrl $0x1f, %r13d
movl $0x3, %esi
movq %r13, %rdx
callq 0x124d9
testl %eax, %eax
leaq 0x7ed37(%rip), %rax     # 0x87697
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
movl $0x4, %esi
movq 0x68(%rsp), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x1260f
testl %eax, %eax
leaq 0x7ed15(%rip), %rax     # 0x8769e
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
movl %r12d, %edx
movq 0x60(%rsp), %r12
movl $0x5, %esi
callq 0x124d9
testl %eax, %eax
leaq 0x7ecf8(%rip), %rax     # 0x876a5
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
movl %r14d, %edx
movl $0x6, %esi
callq 0x124d9
testl %eax, %eax
leaq 0x7ece0(%rip), %rax     # 0x876ac
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
movl $0x7, %esi
movq %rbp, %rdx
callq 0x124d9
testl %eax, %eax
leaq 0x7ecc8(%rip), %rax     # 0x876b3
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
callq 0x10e17
cmpl $0x65, %eax
leaq 0x7f073(%rip), %rax     # 0x87a76
cmovneq %rax, %r15
movq 0x20(%rsp), %rdi
callq 0x10253
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
testl %eax, %eax
leaq 0x7ec96(%rip), %rax     # 0x876ba
cmovneq %rax, %r15
incq %rbx
cmpq %rbx, %rsi
jne 0x8820
movq 0xa37cd(%rip), %rdi     # 0xac208
callq 0x93e0
movq 0x20(%rsp), %rdi
callq 0x100e6
testl %eax, %eax
je 0x8a5a
leaq 0x7ec6b(%rip), %rbx     # 0x876c0
jmp 0x8b48
movq 0xa37a7(%rip), %rdi     # 0xac208
leaq 0x7ec62(%rip), %rsi     # 0x876ca
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x156ff
testl %eax, %eax
je 0x8a8a
leaq 0x7eca5(%rip), %rbx     # 0x8772a
jmp 0x8b48
xorl %ebp, %ebp
leaq 0x7eca5(%rip), %r12     # 0x87738
leaq 0x7eca1(%rip), %r13     # 0x8773b
leaq 0x7ec92(%rip), %rbx     # 0x87733
movq 0x20(%rsp), %rdi
callq 0x10e17
cmpl $0x5, %eax
je 0x8aa1
cmpl $0x63, %eax
jle 0x8aea
cmpl $0x65, %eax
je 0x8b06
cmpl $0x64, %eax
jne 0x8afd
testb $0x1, %bpl
movq %r13, %r14
cmovneq %r12, %r14
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x11faa
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x80e0
movb $0x1, %bpl
jmp 0x8aa1
cmpl $0x1, %eax
je 0x8b15
cmpl $0x15, %eax
jne 0x8afd
leaq 0x7e803(%rip), %r15     # 0x872fe
jmp 0x8b2a
leaq 0x7e81e(%rip), %r15     # 0x87322
jmp 0x8b2a
testb $0x1, %bpl
je 0x8b1e
leaq 0x85044(%rip), %rdi     # 0x8db57
jmp 0x8b25
leaq 0x7e7bf(%rip), %r15     # 0x872db
jmp 0x8b2a
leaq 0x7ed27(%rip), %rdi     # 0x8784c
callq 0x8090
movq 0x20(%rsp), %rdi
callq 0x100e6
testl %eax, %eax
leaq 0x7ec0a(%rip), %rbx     # 0x87747
cmoveq %r15, %rbx
testq %r15, %r15
cmovneq %r15, %rbx
movq 0xa36b9(%rip), %rdi     # 0xac208
leaq 0x7ebfb(%rip), %rsi     # 0x87751
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x14a2f
jmp 0x86be