// File: /workspace/github_c_makefile_star2_O2/miniupnp[P]hfsplustools/src/hfspatchnoautofolder.c
// Function: set_auto_folder
void set_auto_folder(FILE * f, uint32_t folderid)
{
	unsigned char buffer[512];
	unsigned char a_buffer[512];
	uint32_t dir_id;
	int write_alternate = 0;

	if(fseek(f, hfs_part_sector * SECTOR_SIZE + HFS_VOLUME_HEADER_OFFSET, SEEK_SET) != 0) {
		perror("fseek");
		return;
	}
	if(fread(buffer, 1, sizeof(buffer), f) != sizeof(buffer)) {
		fprintf(stderr, "reading error\n");
		return;
	}
	printf("SIGNATURE %c%c\n", buffer[0], buffer[1]);
	if(buffer[0] != 'H' || buffer[1] != '+') {
		printf("Not HFS+\n");
		return;
	}
	if(fseek(f, (hfs_part_sector + hfs_part_sector_count) * SECTOR_SIZE - HFS_VOLUME_HEADER_OFFSET, SEEK_SET) != 0) {
		perror("fseek");
		return;
	}
	if(fread(a_buffer, 1, sizeof(a_buffer), f) != sizeof(a_buffer)) {
		fprintf(stderr, "reading error\n");
		return;
	}
	if(memcmp(buffer, a_buffer, sizeof(buffer)) == 0) {
		write_alternate = 1;
	}
	dir_id = readu32(buffer + AUTO_OPEN_DIR_ID_OFFSET);
	printf("Former value = %u\n", dir_id);
	if(dir_id == folderid) {
		printf("No need to patch\n");
		return;
	}
	writeu32(buffer + AUTO_OPEN_DIR_ID_OFFSET, folderid);
	dir_id = readu32(buffer + AUTO_OPEN_DIR_ID_OFFSET);
	printf("New value = %u\n", dir_id);

	/* Main Volume Header */
	if(fseek(f, hfs_part_sector * SECTOR_SIZE + HFS_VOLUME_HEADER_OFFSET, SEEK_SET) != 0) {
		perror("fseek");
		return;
	}
	if(fwrite(buffer, 1, sizeof(buffer), f) != sizeof(buffer)) {
		fprintf(stderr, "write error\n");
		return;
	}

	/* Alternate Volume Header */
	if(write_alternate) {
		if(fseek(f, (hfs_part_sector + hfs_part_sector_count) * SECTOR_SIZE - HFS_VOLUME_HEADER_OFFSET, SEEK_SET) != 0) {
			perror("fseek");
			return;
		}
		if(fwrite(buffer, 1, sizeof(buffer), f) != sizeof(buffer)) {
			fprintf(stderr, "write error\n");
			return;
		}
	}

	printf("successfully patched !\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x4e46(%rip), %r13      # 0x60c4
movl (%r13), %esi
shll $0x9, %esi
addl $0x400, %esi            # imm = 0x400
xorl %edx, %edx
callq 0x1100
testl %eax, %eax
jne 0x1459
movq %rsp, %rdi
pushq $0x1
popq %rsi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rcx
callq 0x1070
cmpq $0x200, %rax            # imm = 0x200
jne 0x1476
movzbl (%rsp), %r14d
movzbl 0x1(%rsp), %r15d
leaq 0x1d4e(%rip), %rdi      # 0x3019
movl %r14d, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1090
xorb $0x48, %r14b
xorb $0x2b, %r15b
orb %r14b, %r15b
je 0x1302
leaq 0x1f23(%rip), %rdi      # 0x320f
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1060
leaq 0x4dbf(%rip), %rax      # 0x60c8
movl (%rax), %esi
addl (%r13), %esi
shll $0x9, %esi
addl $0xfffffc00, %esi       # imm = 0xFFFFFC00
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1100
testl %eax, %eax
jne 0x1459
leaq 0x200(%rsp), %rdi
pushq $0x1
popq %rsi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rcx
callq 0x1070
cmpq $0x200, %rax            # imm = 0x200
jne 0x1476
movq %rsp, %rdi
leaq 0x200(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x10f0
movl %eax, %r14d
leaq 0x58(%rsp), %r15
movq %r15, %rdi
callq 0x188f
movl %eax, %r12d
leaq 0x1cac(%rip), %rdi      # 0x3029
movl %eax, %esi
xorl %eax, %eax
callq 0x1090
cmpl %ebp, %r12d
jne 0x1397
leaq 0x1e6c(%rip), %rdi      # 0x31fe
jmp 0x1442
bswapl %ebp
movl %ebp, 0x58(%rsp)
movq %r15, %rdi
callq 0x188f
leaq 0x1c90(%rip), %rdi      # 0x303c
movl %eax, %esi
xorl %eax, %eax
callq 0x1090
movl (%r13), %esi
shll $0x9, %esi
addl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1100
testl %eax, %eax
jne 0x14a3
movq %rsp, %rdi
pushq $0x1
popq %rsi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rcx
callq 0x1140
cmpq $0x200, %rax            # imm = 0x200
jne 0x14b1
testl %r14d, %r14d
jne 0x143b
leaq 0x4cc9(%rip), %rax      # 0x60c8
movl (%rax), %esi
addl (%r13), %esi
shll $0x9, %esi
addl $0xfffffc00, %esi       # imm = 0xFFFFFC00
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1100
testl %eax, %eax
jne 0x14a3
movq %rsp, %rdi
pushq $0x1
popq %rsi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rcx
callq 0x1140
cmpq $0x200, %rax            # imm = 0x200
jne 0x14b1
leaq 0x1da5(%rip), %rdi      # 0x31e7
callq 0x1060
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ba4(%rip), %rdi      # 0x3004
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1120
movq 0x4b7b(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x1b83(%rip), %rdi      # 0x300a
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1140
leaq 0x1b5a(%rip), %rdi      # 0x3004
callq 0x1120
jmp 0x1447
movq 0x4b40(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x1b8a(%rip), %rdi      # 0x304c
pushq $0xc
popq %rsi
pushq $0x1
popq %rdx
callq 0x1140
jmp 0x1447