// File: /workspace/github_c_makefile_star2_O3/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 0x4b16(%rip), %r13      # 0x60c4
movl (%r13), %esi
shll $0x9, %esi
addl $0x400, %esi            # imm = 0x400
xorl %edx, %edx
callq 0x1100
testl %eax, %eax
jne 0x178d
movq %rsp, %rdi
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rcx
callq 0x1070
cmpq $0x200, %rax            # imm = 0x200
jne 0x17aa
movzbl (%rsp), %r14d
movzbl 0x1(%rsp), %r15d
leaq 0x1a1c(%rip), %rdi      # 0x3019
movl %r14d, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1090
xorb $0x48, %r14b
xorb $0x2b, %r15b
orb %r14b, %r15b
je 0x1634
leaq 0x1bf1(%rip), %rdi      # 0x320f
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1060
leaq 0x4a8d(%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 0x178d
leaq 0x200(%rsp), %rdi
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rcx
callq 0x1070
cmpq $0x200, %rax            # imm = 0x200
jne 0x17b1
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 0x1b00
movl %eax, %r12d
leaq 0x1978(%rip), %rdi      # 0x3029
movl %eax, %esi
xorl %eax, %eax
callq 0x1090
cmpl %ebp, %r12d
jne 0x16cb
leaq 0x1b38(%rip), %rdi      # 0x31fe
jmp 0x1776
bswapl %ebp
movl %ebp, 0x58(%rsp)
movq %r15, %rdi
callq 0x1b00
leaq 0x195c(%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 0x17b8
movq %rsp, %rdi
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rcx
callq 0x1140
cmpq $0x200, %rax            # imm = 0x200
jne 0x17c6
testl %r14d, %r14d
jne 0x176f
leaq 0x4993(%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 0x17b8
movq %rsp, %rdi
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rcx
callq 0x1140
cmpq $0x200, %rax            # imm = 0x200
jne 0x17cd
leaq 0x1a71(%rip), %rdi      # 0x31e7
callq 0x1060
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1870(%rip), %rdi      # 0x3004
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1120
callq 0x1205
jmp 0x177b
callq 0x1221
jmp 0x177b
leaq 0x1845(%rip), %rdi      # 0x3004
callq 0x1120
jmp 0x177b
callq 0x123d
jmp 0x177b
callq 0x125f
jmp 0x177b