// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x420, %rsp            # imm = 0x420
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x0, -0x418(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x5de5(%rip), %rax      # 0x70bc
movl (%rax), %eax
shll $0x9, %eax
addl $0x400, %eax            # imm = 0x400
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10f0
cmpl $0x0, %eax
je 0x1302
leaq 0x2d0c(%rip), %rdi      # 0x4004
callq 0x1110
jmp 0x15b8
leaq -0x210(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1060
cmpq $0x200, %rax            # imm = 0x200
je 0x1341
movq 0x5ccd(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2cd5(%rip), %rsi      # 0x400a
movb $0x0, %al
callq 0x10c0
jmp 0x15b8
movzbl -0x210(%rbp), %esi
movzbl -0x20f(%rbp), %edx
leaq 0x2cc3(%rip), %rdi      # 0x4019
movb $0x0, %al
callq 0x1080
movzbl -0x210(%rbp), %eax
cmpl $0x48, %eax
jne 0x1375
movzbl -0x20f(%rbp), %eax
cmpl $0x2b, %eax
je 0x1388
leaq 0x2cad(%rip), %rdi      # 0x4029
movb $0x0, %al
callq 0x1080
jmp 0x15b8
movq -0x8(%rbp), %rdi
leaq 0x5d29(%rip), %rax      # 0x70bc
movl (%rax), %eax
leaq 0x5d24(%rip), %rcx      # 0x70c0
addl (%rcx), %eax
shll $0x9, %eax
subl $0x400, %eax            # imm = 0x400
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10f0
cmpl $0x0, %eax
je 0x13c7
leaq 0x2c47(%rip), %rdi      # 0x4004
callq 0x1110
jmp 0x15b8
leaq -0x410(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1060
cmpq $0x200, %rax            # imm = 0x200
je 0x1406
movq 0x5c08(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2c10(%rip), %rsi      # 0x400a
movb $0x0, %al
callq 0x10c0
jmp 0x15b8
leaq -0x210(%rbp), %rdi
leaq -0x410(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x10a0
cmpl $0x0, %eax
jne 0x142d
movl $0x1, -0x418(%rbp)
leaq -0x210(%rbp), %rdi
addq $0x58, %rdi
callq 0x1af0
movl %eax, -0x414(%rbp)
movl -0x414(%rbp), %esi
leaq 0x2be3(%rip), %rdi      # 0x4033
movb $0x0, %al
callq 0x1080
movl -0x414(%rbp), %eax
cmpl -0xc(%rbp), %eax
jne 0x1475
leaq 0x2bdd(%rip), %rdi      # 0x4046
movb $0x0, %al
callq 0x1080
jmp 0x15b8
leaq -0x210(%rbp), %rdi
addq $0x58, %rdi
movl -0xc(%rbp), %esi
callq 0x1250
leaq -0x210(%rbp), %rdi
addq $0x58, %rdi
callq 0x1af0
movl %eax, -0x414(%rbp)
movl -0x414(%rbp), %esi
leaq 0x2bad(%rip), %rdi      # 0x4058
movb $0x0, %al
callq 0x1080
movq -0x8(%rbp), %rdi
leaq 0x5bff(%rip), %rax      # 0x70bc
movl (%rax), %eax
shll $0x9, %eax
addl $0x400, %eax            # imm = 0x400
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10f0
cmpl $0x0, %eax
je 0x14e8
leaq 0x2b26(%rip), %rdi      # 0x4004
callq 0x1110
jmp 0x15b8
leaq -0x210(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1130
cmpq $0x200, %rax            # imm = 0x200
je 0x1527
movq 0x5ae7(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2b4d(%rip), %rsi      # 0x4068
movb $0x0, %al
callq 0x10c0
jmp 0x15b8
cmpl $0x0, -0x418(%rbp)
je 0x15aa
movq -0x8(%rbp), %rdi
leaq 0x5b81(%rip), %rax      # 0x70bc
movl (%rax), %eax
leaq 0x5b7c(%rip), %rcx      # 0x70c0
addl (%rcx), %eax
shll $0x9, %eax
subl $0x400, %eax            # imm = 0x400
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10f0
cmpl $0x0, %eax
je 0x156c
leaq 0x2a9f(%rip), %rdi      # 0x4004
callq 0x1110
jmp 0x15b8
leaq -0x210(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1130
cmpq $0x200, %rax            # imm = 0x200
je 0x15a8
movq 0x5a63(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2ac9(%rip), %rsi      # 0x4068
movb $0x0, %al
callq 0x10c0
jmp 0x15b8
jmp 0x15aa
leaq 0x2ac4(%rip), %rdi      # 0x4075
movb $0x0, %al
callq 0x1080
addq $0x420, %rsp            # imm = 0x420
popq %rbp
retq
nopw %cs:(%rax,%rax)