	.file	"extr_cartridge.c_InsertCartridge.c"
	.intel_syntax noprefix
	.text
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rb"
.LC1:
	.string	"Error reading cartridge.\n"
	.text
	.p2align 4
	.type	InsertCartridge, @function
InsertCartridge:
.LFB0:
	.cfi_startproc
	endbr64
	push	r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	mov	r13, rdi
	push	r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	mov	r12, rsi
	lea	rsi, .LC0[rip]
	push	rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	push	rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	sub	rsp, 40
	.cfi_def_cfa_offset 80
	mov	rax, QWORD PTR fs:40
	mov	QWORD PTR 24[rsp], rax
	xor	eax, eax
	call	fopen@PLT
	test	rax, rax
	je	.L20
	mov	rbp, rax
	mov	rdi, rax
	call	Util_flen@PLT
	mov	rdi, rbp
	mov	ebx, eax
	call	Util_rewind@PLT
	mov	rdi, QWORD PTR [r12]
	cmp	rdi, r13
	je	.L4
	mov	rsi, r13
	call	strcpy@PLT
.L4:
	mov	r13d, ebx
	and	r13d, 1023
	jne	.L5
	mov	edi, ebx
	call	Util_malloc@PLT
	mov	rcx, rbp
	mov	edx, ebx
	mov	esi, 1
	mov	QWORD PTR 8[r12], rax
	mov	rdi, rax
	call	fread@PLT
	cmp	eax, ebx
	jl	.L21
.L6:
	mov	rdi, rbp
	call	fclose@PLT
	mov	r8d, DWORD PTR CARTRIDGE_NONE[rip]
	mov	eax, ebx
	mov	ecx, DWORD PTR CARTRIDGE_LAST_SUPPORTED[rip]
	sar	eax, 10
	mov	DWORD PTR 16[r12], r8d
	mov	DWORD PTR 20[r12], eax
	test	ecx, ecx
	jle	.L7
	mov	rsi, QWORD PTR CARTRIDGE_kb[rip]
	mov	edi, r8d
	mov	edx, 1
	jmp	.L10
	.p2align 4,,10
	.p2align 3
.L8:
	add	rdx, 1
	cmp	ecx, edx
	jl	.L22
.L10:
	cmp	DWORD PTR [rsi+rdx*4], eax
	jne	.L8
	cmp	r8d, edi
	jne	.L9
	mov	DWORD PTR 16[r12], edx
	mov	edi, edx
	add	rdx, 1
	cmp	ecx, edx
	jge	.L10
.L22:
	cmp	r8d, edi
	je	.L7
.L14:
	mov	rdi, r12
	call	InitCartridge@PLT
	jmp	.L1
	.p2align 4,,10
	.p2align 3
.L5:
	mov	rdi, rsp
	mov	rcx, rbp
	mov	edx, 16
	mov	esi, 1
	call	fread@PLT
	cmp	eax, 15
	jle	.L23
.L11:
	cmp	BYTE PTR [rsp], 67
	jne	.L12
	cmp	BYTE PTR 1[rsp], 65
	jne	.L12
	cmp	BYTE PTR 2[rsp], 82
	je	.L24
	.p2align 4,,10
	.p2align 3
.L12:
	mov	rdi, rbp
	call	fclose@PLT
	mov	r13d, DWORD PTR CARTRIDGE_BAD_FORMAT[rip]
.L1:
	mov	rax, QWORD PTR 24[rsp]
	xor	rax, QWORD PTR fs:40
	jne	.L25
	add	rsp, 40
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	mov	eax, r13d
	pop	rbx
	.cfi_def_cfa_offset 32
	pop	rbp
	.cfi_def_cfa_offset 24
	pop	r12
	.cfi_def_cfa_offset 16
	pop	r13
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L20:
	.cfi_restore_state
	mov	r13d, DWORD PTR CARTRIDGE_CANT_OPEN[rip]
	jmp	.L1
	.p2align 4,,10
	.p2align 3
.L7:
	mov	rdi, QWORD PTR 8[r12]
	call	free@PLT
	mov	r13d, DWORD PTR CARTRIDGE_BAD_FORMAT[rip]
	mov	QWORD PTR 8[r12], 0
	jmp	.L1
	.p2align 4,,10
	.p2align 3
.L23:
	lea	rdi, .LC1[rip]
	call	Log_print@PLT
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L21:
	lea	rdi, .LC1[rip]
	call	Log_print@PLT
	jmp	.L6
	.p2align 4,,10
	.p2align 3
.L9:
	mov	edx, DWORD PTR CARTRIDGE_UNKNOWN[rip]
	mov	r13d, eax
	mov	DWORD PTR 16[r12], edx
	jmp	.L1
.L24:
	cmp	BYTE PTR 3[rsp], 84
	jne	.L12
	movsx	ebx, BYTE PTR 4[rsp]
	movsx	eax, BYTE PTR 5[rsp]
	sal	ebx, 24
	sal	eax, 16
	or	ebx, eax
	movsx	eax, BYTE PTR 7[rsp]
	or	ebx, eax
	movsx	eax, BYTE PTR 6[rsp]
	sal	eax, 8
	or	ebx, eax
	jle	.L12
	cmp	DWORD PTR CARTRIDGE_LAST_SUPPORTED[rip], ebx
	jl	.L12
	mov	rdx, QWORD PTR CARTRIDGE_kb[rip]
	movsx	rax, ebx
	mov	eax, DWORD PTR [rdx+rax*4]
	mov	r13d, eax
	mov	DWORD PTR 20[r12], eax
	sal	r13d, 10
	mov	edi, r13d
	call	Util_malloc@PLT
	mov	rcx, rbp
	mov	edx, r13d
	mov	esi, 1
	mov	QWORD PTR 8[r12], rax
	mov	rdi, rax
	call	fread@PLT
	cmp	eax, r13d
	jl	.L26
.L13:
	mov	rdi, rbp
	call	fclose@PLT
	movsx	ebp, BYTE PTR 8[rsp]
	movsx	eax, BYTE PTR 9[rsp]
	mov	esi, r13d
	mov	DWORD PTR 16[r12], ebx
	mov	rdi, QWORD PTR 8[r12]
	sal	eax, 16
	sal	ebp, 24
	or	ebp, eax
	movsx	eax, BYTE PTR 11[rsp]
	or	ebp, eax
	movsx	eax, BYTE PTR 10[rsp]
	sal	eax, 8
	or	ebp, eax
	call	CARTRIDGE_Checksum@PLT
	cmp	eax, ebp
	je	.L16
	mov	r13d, DWORD PTR CARTRIDGE_BAD_CHECKSUM[rip]
	jmp	.L14
.L25:
	call	__stack_chk_fail@PLT
.L16:
	xor	r13d, r13d
	jmp	.L14
.L26:
	lea	rdi, .LC1[rip]
	call	Log_print@PLT
	jmp	.L13
	.cfi_endproc
.LFE0:
	.size	InsertCartridge, .-InsertCartridge
	.comm	CARTRIDGE_kb,8,8
	.comm	CARTRIDGE_UNKNOWN,4,4
	.comm	CARTRIDGE_NONE,4,4
	.comm	CARTRIDGE_LAST_SUPPORTED,4,4
	.comm	CARTRIDGE_CANT_OPEN,4,4
	.comm	CARTRIDGE_BAD_FORMAT,4,4
	.comm	CARTRIDGE_BAD_CHECKSUM,4,4
	.ident	"GCC: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	 1f - 0f
	.long	 4f - 1f
	.long	 5
0:
	.string	 "GNU"
1:
	.align 8
	.long	 0xc0000002
	.long	 3f - 2f
2:
	.long	 0x3
3:
	.align 8
4:

	.arch armv5t
	.eabi_attribute 20, 1
	.eabi_attribute 21, 1
	.eabi_attribute 23, 3
	.eabi_attribute 24, 1
	.eabi_attribute 25, 1
	.eabi_attribute 26, 2
	.eabi_attribute 30, 2
	.eabi_attribute 34, 0
	.eabi_attribute 18, 4
	.file	"extr_cartridge.c_InsertCartridge.c"
	.text
	.section	.rodata.str1.4,"aMS",%progbits,1
	.align	2
.LC0:
	.ascii	"rb\000"
	.align	2
.LC1:
	.ascii	"Error reading cartridge.\012\000"
	.section	.rodata.cst4,"aM",%progbits,4
	.align	2
.LC2:
	.word	__stack_chk_guard
	.text
	.align	2
	.syntax unified
	.arm
	.fpu softvfp
	.type	InsertCartridge, %function
InsertCartridge:
	@ args = 0, pretend = 0, frame = 24
	@ frame_needed = 0, uses_anonymous_args = 0
	push	{r4, r5, r6, r7, lr}
	ldr	r3, .L27
	sub	sp, sp, #28
	mov	r5, r1
	ldr	r1, .L27+4
	ldr	r3, [r3]
	str	r3, [sp, #20]
	mov	r3,#0
	mov	r6, r0
	bl	fopen
	subs	r7, r0, #0
	beq	.L20
	bl	Util_flen
	mov	r4, r0
	mov	r0, r7
	bl	Util_rewind
	ldr	r0, [r5]
	cmp	r0, r6
	beq	.L4
	mov	r1, r6
	bl	strcpy
.L4:
	lsl	r6, r4, #22
	lsr	r6, r6, #22
	cmp	r6, #0
	bne	.L5
	mov	r0, r4
	bl	Util_malloc
	mov	r3, r7
	mov	r2, r4
	mov	r1, #1
	str	r0, [r5, #4]
	bl	fread
	cmp	r0, r4
	blt	.L21
.L6:
	mov	r0, r7
	bl	fclose
	ldr	r2, .L27+8
	ldr	r3, .L27+12
	ldr	r1, [r2]
	ldr	r7, [r3]
	asr	ip, r4, #10
	cmp	r1, #0
	str	ip, [r5, #12]
	str	r7, [r5, #8]
	ble	.L7
	ldr	r2, .L27+16
	mov	lr, r7
	mov	r3, #1
	ldr	r2, [r2]
	b	.L10
.L8:
	add	r3, r3, #1
	cmp	r3, r1
	bgt	.L22
.L10:
	ldr	r0, [r2, #4]!
	cmp	r0, ip
	bne	.L8
	cmp	r7, lr
	bne	.L9
	mov	lr, r3
	str	r3, [r5, #8]
	add	r3, r3, #1
	cmp	r3, r1
	ble	.L10
.L22:
	cmp	r7, lr
	beq	.L7
.L14:
	mov	r0, r5
	bl	InitCartridge
	b	.L1
.L5:
	mov	r3, r7
	mov	r2, #16
	mov	r1, #1
	add	r0, sp, #4
	bl	fread
	cmp	r0, #15
	ble	.L23
.L11:
	ldrb	r3, [sp, #4]	@ zero_extendqisi2
	cmp	r3, #67
	bne	.L12
	ldrb	r3, [sp, #5]	@ zero_extendqisi2
	cmp	r3, #65
	bne	.L12
	ldrb	r3, [sp, #6]	@ zero_extendqisi2
	cmp	r3, #82
	beq	.L24
.L12:
	mov	r0, r7
	bl	fclose
	ldr	r3, .L27+20
	ldr	r6, [r3]
.L1:
	ldr	r3, .L27
	ldr	r2, [r3]
	ldr	r3, [sp, #20]
	eors	r2, r3, r2
	mov	r3, #0
	bne	.L25
	mov	r0, r6
	add	sp, sp, #28
	@ sp needed
	pop	{r4, r5, r6, r7, pc}
.L20:
	ldr	r3, .L27+24
	ldr	r6, [r3]
	b	.L1
.L7:
	ldr	r0, [r5, #4]
	bl	free
	mov	r2, #0
	ldr	r3, .L27+20
	str	r2, [r5, #4]
	ldr	r6, [r3]
	b	.L1
.L23:
	ldr	r0, .L27+28
	bl	Log_print
	b	.L11
.L21:
	ldr	r0, .L27+28
	bl	Log_print
	b	.L6
.L9:
	ldr	r3, .L27+32
	mov	r6, ip
	ldr	r3, [r3]
	str	r3, [r5, #8]
	b	.L1
.L24:
	ldrb	r3, [sp, #7]	@ zero_extendqisi2
	cmp	r3, #84
	bne	.L12
	ldr	r4, [sp, #8]
	eor	r3, r4, r4, ror #16
	lsr	r3, r3, #8
	bic	r3, r3, #65280
	eor	r4, r3, r4, ror #8
	cmp	r4, #0
	ble	.L12
	ldr	r3, .L27+8
	ldr	r3, [r3]
	cmp	r3, r4
	blt	.L12
	ldr	r3, .L27+16
	ldr	r3, [r3]
	ldr	r3, [r3, r4, lsl #2]
	lsl	r6, r3, #10
	str	r3, [r5, #12]
	mov	r0, r6
	bl	Util_malloc
	mov	r2, r6
	mov	r3, r7
	mov	r1, #1
	str	r0, [r5, #4]
	bl	fread
	cmp	r0, r6
	blt	.L26
.L13:
	mov	r0, r7
	bl	fclose
	ldr	r2, [sp, #12]
	mov	r1, r6
	eor	r3, r2, r2, ror #16
	lsr	r3, r3, #8
	bic	r3, r3, #65280
	str	r4, [r5, #8]
	ldr	r0, [r5, #4]
	eor	r4, r3, r2, ror #8
	bl	CARTRIDGE_Checksum
	cmp	r0, r4
	ldrne	r3, .L27+36
	moveq	r6, #0
	ldrne	r6, [r3]
	b	.L14
.L25:
	bl	__stack_chk_fail
.L26:
	ldr	r0, .L27+28
	bl	Log_print
	b	.L13
.L28:
	.align	2
.L27:
	.word	.LC2
	.word	.LC0
	.word	CARTRIDGE_LAST_SUPPORTED
	.word	CARTRIDGE_NONE
	.word	CARTRIDGE_kb
	.word	CARTRIDGE_BAD_FORMAT
	.word	CARTRIDGE_CANT_OPEN
	.word	.LC1
	.word	CARTRIDGE_UNKNOWN
	.word	CARTRIDGE_BAD_CHECKSUM
	.size	InsertCartridge, .-InsertCartridge
	.comm	CARTRIDGE_kb,4,4
	.comm	CARTRIDGE_UNKNOWN,4,4
	.comm	CARTRIDGE_NONE,4,4
	.comm	CARTRIDGE_LAST_SUPPORTED,4,4
	.comm	CARTRIDGE_CANT_OPEN,4,4
	.comm	CARTRIDGE_BAD_FORMAT,4,4
	.comm	CARTRIDGE_BAD_CHECKSUM,4,4
	.ident	"GCC: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0"
	.section	.note.GNU-stack,"",%progbits
