LLM4Binary/decompile-bench-makefile huggingface dataset:
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_write_flash_cmd(WdtDevice*, unsigned char*, int, int)
int wh_w8790_dev_write_flash_cmd(WDT_DEV* pdev, BYTE* buf, int start, int size)
{
	if (size > W8790_USB_MAX_PAYLOAD_SIZE - 2) {
		wh_printf("%s: payload data overrun\n", __FUNCTION__);
		return 0;
	}

	BYTE cmd[W8790_USB_MAX_PAYLOAD_SIZE + 1] = { 0 };

	cmd[0] = W8790_COMMAND63;
	cmd[1] = (BYTE)W8790_WRITE_FLASH;
	cmd[2] = (BYTE)size;
	memcpy(&cmd[3], &buf[start], size);

	if (wh_w8790_dev_command_write(pdev, cmd, 0, size + 3) > 0)
		return wh_w8790_dev_wait_cmd_end(pdev, 0, 0);
	wh_printf("w8790_write_cmd fail \n)");
	return 0;
}

// Assembly:
pushq %rbp
pushq %rbx
subq $0x48, %rsp
cmpl $0x3e, %ecx
jl 0x56e6
leaq 0x5250(%rip), %rdi      # 0xa924
leaq 0x54aa(%rip), %rsi      # 0xab85
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x24bf
jmp 0x5749
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x3(%rsp), %rdi
movaps %xmm0, -0x3(%rdi)
movaps %xmm0, 0x2d(%rdi)
movaps %xmm0, 0x1d(%rdi)
movaps %xmm0, 0xd(%rdi)
movw $0xd307, -0x3(%rdi)     # imm = 0xD307
movb %cl, -0x1(%rdi)
movslq %edx, %rax
addq %rax, %rsi
movslq %ecx, %rdx
callq 0x2150
movq %rsp, %rsi
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %edx, %edx
callq 0x4d06
testl %eax, %eax
je 0x573b
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5556
movl %eax, %ebp
jmp 0x5749
leaq 0x5460(%rip), %rdi      # 0xaba2
xorl %eax, %eax
callq 0x24bf
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_write_flash(WdtDevice*, int, unsigned char*, int, int)
int wh_w8790_dev_write_flash(WDT_DEV* pdev, int addr, BYTE* buf, int start, int size)
{
	int byte_count = size;
	int offset = start;
	int max_payload_size = W8790_USB_MAX_PAYLOAD_SIZE - 2;
	int cur_addr = 0xFFFFFFFF;

	while (byte_count >= max_payload_size) {
		if ((addr & 0xfff) == 0)
			printf("base addr: 0x%x\n", addr);
		if (!check_is_all_ff(&buf[offset], max_payload_size)) {
			if (cur_addr != addr) {
				if (!wh_w8790_dev_set_flash_address(pdev, addr))
					return 0;
			}
			if (wh_w8790_dev_write_flash_cmd(pdev, buf, offset, max_payload_size) <= 0)
				return 0;
			cur_addr = addr + max_payload_size;
		}
		else
			wh_printf("Already ff, no need to set: 0x%x\n", addr);
		offset += max_payload_size;
		byte_count -= max_payload_size;
		addr += max_payload_size;
	}

	if (cur_addr != addr) {
		if (!wh_w8790_dev_set_flash_address(pdev, addr))
			return 0;
	}
	if (byte_count > 0) {
		if ((addr & 0xfff) == 0)
			wh_printf("base addr: 0x%x\n", addr);
		return wh_w8790_dev_write_flash_cmd(pdev, buf, offset, byte_count);

	}

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %ebx
movl %ecx, %ebp
movl %esi, %r12d
movq %rdi, 0x8(%rsp)
movslq %ecx, %r13
movq %rdx, 0x10(%rsp)
addq %rdx, %r13
pushq $-0x1
popq %r15
pushq $0x3d
popq %r14
cmpl $0x3d, %ebx
jl 0x5810
testw $0xfff, %r12w           # imm = 0xFFF
jne 0x57a2
leaq 0x4d21(%rip), %rdi      # 0xa4b9
movl %r12d, %esi
xorl %eax, %eax
callq 0x2050
movq %r13, %rdi
movl %r14d, %esi
callq 0x6691
testl %eax, %eax
je 0x57c8
leaq 0x51a5(%rip), %rdi      # 0xa95d
movl %r12d, %esi
xorl %eax, %eax
callq 0x24bf
addl $0x3d, %r12d
jmp 0x5801
cmpl %r12d, %r15d
je 0x57e2
movq 0x8(%rsp), %rdi
movl %r12d, %esi
callq 0x506e
testl %eax, %eax
je 0x5864
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x56c2
testl %eax, %eax
je 0x5864
addl $0x3d, %r12d
movl %r12d, %r15d
addl $-0x3d, %ebx
addl $0x3d, %ebp
addq $0x3d, %r13
jmp 0x5780
cmpl %r12d, %r15d
je 0x5826
movq 0x8(%rsp), %rdi
movl %r12d, %esi
callq 0x506e
testl %eax, %eax
je 0x5864
testl %ebx, %ebx
jle 0x5868
testw $0xfff, %r12w           # imm = 0xFFF
jne 0x5843
leaq 0x4c80(%rip), %rdi      # 0xa4b9
movl %r12d, %esi
xorl %eax, %eax
callq 0x24bf
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %ebp, %edx
movl %ebx, %ecx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x56c2
xorl %eax, %eax
jmp 0x586b
pushq $0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_write_flash_page(WdtDevice*, unsigned char*, int, int)
int wh_w8790_dev_write_flash_page(WDT_DEV* pdev, BYTE* buf, int addr, int size)
{
	int retval = 0;
	unsigned int	write_base;
	unsigned int	write_size;

	write_base = W8790_FLASH_PAGE_SIZE;

	if (addr & 0xFF) {
		unsigned int 	size_partial;
		size_partial = write_base - (addr & 0xFF);

		if (size > (int)size_partial) {
			write_size = size_partial;
			size = size - size_partial;
		}
		else {
			write_size = size;
			size = 0;
		}
		retval = wh_w8790_dev_write_flash(pdev, addr, (BYTE*)buf, 0/*offset*/, write_size);
		if (!retval)
			return retval;
		buf = buf + size_partial;
		addr = addr + size_partial;
	}

	while (size)
	{
		if (size > (int)write_base) {
			write_size = write_base;
			size = size - write_base;
		}
		else {
			write_size = size;
			size = 0;
		}
		retval = wh_w8790_dev_write_flash(pdev, addr, (BYTE*)buf, 0/*offset*/, write_size);
		if (!retval)
			return retval;
		buf = buf + write_base;
		addr = addr + write_base;
	}

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %r12d
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movl %edx, %eax
andl $0xff, %eax
je 0x58d6
movl $0x100, %r13d           # imm = 0x100
subl %eax, %r13d
movl %r13d, %eax
subl %r12d, %eax
movl %r12d, %r8d
cmovll %r13d, %r8d
negl %eax
xorl %ebp, %ebp
cmpl %r13d, %r12d
movl %eax, %r12d
cmovlel %ebp, %r12d
movq %r15, %rdi
movl %ebx, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x5752
testl %eax, %eax
je 0x592f
addq %r13, %r14
addl %r13d, %ebx
movl $0x100, %r13d           # imm = 0x100
xorl %ebp, %ebp
testl %r12d, %r12d
je 0x592c
cmpl %r13d, %r12d
movl $0x100, %r8d            # imm = 0x100
cmovll %r12d, %r8d
movq %r15, %rdi
movl %ebx, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x5752
testl %eax, %eax
je 0x592f
addq $0x100, %r14            # imm = 0x100
addl $0x100, %ebx            # imm = 0x100
cmpl $0x101, %r12d           # imm = 0x101
leal -0x100(%r12), %eax
movl $0x0, %ecx
cmovll %ecx, %eax
movl %eax, %r12d
jmp 0x58de
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_protect_flash(WdtDevice*, unsigned short)
int wh_w8790_dev_protect_flash(WDT_DEV* pdev, UINT16 protect_mask)
{
	UINT16 mask = protect_mask;

	BYTE cmd[10] = { W8790_COMMAND9, (BYTE)W8790_PROTECT_FLASH, 0, 0, 0, 0 };
	put_unaligned_le16(mask, &cmd[2]);
	put_unaligned_le16((UINT16)~mask, &cmd[4]);

	return wh_w8790_dev_command_write(pdev, cmd, 0, sizeof(cmd));
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebx
movq %rdi, %r14
leaq 0xa(%rsp), %rsi
andw $0x0, 0x6(%rsi)
movq $0xd406, -0x2(%rsi)     # imm = 0xD406
movl %ebx, %edi
callq 0x668a
leaq 0xc(%rsp), %rsi
xorl $0xffff, %ebx           # imm = 0xFFFF
movl %ebx, %edi
callq 0x668a
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x4d06
addq $0x18, %rsp
popq %rbx
popq %r14
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_flash_get_checksum(WdtDevice*, unsigned int*, unsigned int, int, unsigned int)
int wh_w8790_dev_flash_get_checksum(WDT_DEV* pdev, UINT32* pchksum, UINT32 flash_address,
	int size, UINT32 init_sum)
{
	BYTE cmd[10] = { 0 };
	BYTE buf[2] = { 0 };

	*pchksum = 0;

	cmd[0] = W8790_COMMAND9;
	cmd[1] = (BYTE)W8790_CALCULATE_FLASH_CHECKSUM;
	put_unaligned_le32(flash_address, &cmd[2]);
	put_unaligned_le32(size, &cmd[5]);
	put_unaligned_le16(init_sum, &cmd[8]);

	if (wh_w8790_dev_command_write(pdev, cmd, 0, sizeof(cmd)) <= 0)
		return 0;

	if (wh_w8790_dev_wait_cmd_end(pdev, 0, 0) <= 0)
		return 0;

	if (wh_w8790_dev_read_buf_response(pdev, buf, 2) <= 0)
		return 0;

	*pchksum = get_unaligned_le16(buf);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl %r8d, %ebp
movl %ecx, %r15d
movq %rsi, %rbx
leaq 0x10(%rsp), %rsi
andq $0x0, (%rsi)
movq %rdi, %r14
andw $0x0, 0xc(%rsp)
andl $0x0, (%rbx)
movw $0xd506, -0x2(%rsi)     # imm = 0xD506
movl %edx, %edi
callq 0x668e
leaq 0x13(%rsp), %rsi
movl %r15d, %edi
callq 0x668e
leaq 0x16(%rsp), %rsi
movzwl %bp, %edi
callq 0x668a
leaq 0xe(%rsp), %rsi
xorl %ebp, %ebp
movq %r14, %rdi
xorl %edx, %edx
callq 0x4d06
testl %eax, %eax
je 0x5a2a
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5556
testl %eax, %eax
je 0x5a2a
leaq 0xc(%rsp), %rsi
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0x564d
testl %eax, %eax
je 0x5a2a
leaq 0xc(%rsp), %rdi
callq 0x6683
movzwl %ax, %eax
movl %eax, (%rbx)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_set_block_access(WdtDevice*, unsigned char, unsigned int, unsigned int*)
int wh_w8790_dev_set_block_access(WDT_DEV* pdev, BYTE type, UINT32 offset, UINT32* pblk_size)
{
	BYTE response[4] = { 0 };
	BYTE cmd[10] = { 0 };
	int block_size = 0;

	*pblk_size = 0;

	cmd[0] = W8790_COMMAND9;
	cmd[1] = (BYTE)W8790_SET_BLOCK_ACCESS;
	cmd[2] = type;
	cmd[3] = (BYTE)(offset >> 0);
	cmd[4] = (BYTE)(offset >> 8);
	cmd[5] = (BYTE)(offset >> 16);

	if (wh_w8790_dev_command_read(pdev, cmd, sizeof(cmd), response, 0, 4) <= 0)
	{
		wh_printf("%s: Failed !\n", __FUNCTION__);
		return 0;
	}
		

	block_size = response[1] | (response[2] << 8) | (response[3] << 16);

	if (response[0] == (BYTE)type && block_size != 0x00FFFFFF)
		*pblk_size = block_size;

	return 1;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rcx, %rbx
movq %rsp, %rcx
andl $0x0, (%rcx)
leaq 0x6(%rsp), %rax
andl $0x0, 0x6(%rax)
andl $0x0, (%rbx)
movl %esi, %r14d
movw $0xe006, (%rax)         # imm = 0xE006
movb %r14b, 0x2(%rax)
movb %dl, 0x3(%rax)
movb %dh, 0xa(%rsp)
shrl $0x10, %edx
movb %dl, 0x5(%rax)
xorl %ebp, %ebp
pushq $0x4
popq %r9
movq %rax, %rsi
xorl %r8d, %r8d
callq 0x4ea5
testl %eax, %eax
je 0x5aa5
pushq $0x1
popq %rbp
cmpb %r14b, (%rsp)
jne 0x5aba
movzwl 0x1(%rsp), %ecx
movzbl 0x3(%rsp), %eax
shll $0x10, %eax
orl %ecx, %eax
cmpl $0xffffff, %eax         # imm = 0xFFFFFF
je 0x5aba
movl %eax, (%rbx)
jmp 0x5aba
leaq 0x510e(%rip), %rdi      # 0xabba
leaq 0x5115(%rip), %rsi      # 0xabc8
xorl %eax, %eax
callq 0x24bf
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_block_checksum(WdtDevice*, unsigned int*, unsigned int, unsigned int)
int wh_w8790_dev_block_checksum(WDT_DEV* pdev, UINT32* pchksum, UINT32 size, UINT32 init_value)
{
	BYTE cmd[10] = { 0 };
	BYTE buf[8] = { 0 };

	cmd[0] = W8790_COMMAND9;
	cmd[1] = W8790_BLOCK_CHECKSUM;
	put_unaligned_le32(size, &cmd[2]);
	put_unaligned_le16(init_value, &cmd[5]);

	if (wh_w8790_dev_command_write(pdev, cmd, 0, sizeof(cmd)) <= 0)
		return 0;

	if (wh_w8790_dev_wait_cmd_end(pdev, 0, 0) <= 0)
		return 0;

	if (wh_w8790_dev_read_buf_response(pdev, buf, 2) <= 0)
		return 0;

	*pchksum = get_unaligned_le16(buf);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x20, %rsp
movl %ecx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x18(%rsp), %rsi
andq $0x0, (%rsi)
andq $0x0, 0x8(%rsp)
movw $0xe306, -0x2(%rsi)     # imm = 0xE306
movl %edx, %edi
callq 0x668e
leaq 0x1b(%rsp), %rsi
movzwl %bp, %edi
callq 0x668a
leaq 0x16(%rsp), %rsi
xorl %ebp, %ebp
movq %r14, %rdi
xorl %edx, %edx
callq 0x4d06
testl %eax, %eax
je 0x5b55
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5556
testl %eax, %eax
je 0x5b55
leaq 0x8(%rsp), %rsi
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0x564d
testl %eax, %eax
je 0x5b55
leaq 0x8(%rsp), %rdi
callq 0x6683
movzwl %ax, %eax
movl %eax, (%rbx)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_flash_read_data(WdtDevice*, unsigned char*, unsigned int, int)
int wh_w8790_dev_flash_read_data(WDT_DEV* pdev, BYTE* data, UINT32 address, int length)
{
	if (!pdev || !data)
		return 0;

	// address and length should be align to 4
	if ((address & 0x3) != 0 || (length & 0x3) != 0)
		return 0;


	if (!wh_w8790_dev_set_flash_address(pdev, address))
		return 0;

	return wh_w8790_dev_read_flash(pdev, data, length);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movl %ecx, %ebx
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x5b7f
movl %ebx, %eax
orl %edx, %eax
testb $0x3, %al
je 0x5b87
xorl %eax, %eax
popq %rbx
popq %r14
popq %r15
retq
movq %rsi, %r14
movq %rdi, %r15
movl %edx, %esi
callq 0x506e
testl %eax, %eax
je 0x5b7f
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
popq %rbx
popq %r14
popq %r15
jmp 0x4ff2
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: w8790_dev_flash_write_4k(WdtDevice*, unsigned char*, unsigned int, int)
int w8790_dev_flash_write_4k(WDT_DEV* pdev, BYTE* data, UINT32 address, int size) {
	int retval = 0;
	int page_size;
	int retry_count = 0;
	int start_addr = address;
	UINT32 calc_checksum, read_checksum;

	BYTE* pdata = data;


	while (size) {
		if (size > (int)WDT_PAGE_SIZE) {
			page_size = WDT_PAGE_SIZE;
			size = size - WDT_PAGE_SIZE;
		} else {

			page_size = size;
			size = 0;
		}

		for (retry_count = 0; retry_count < RETRY_COUNT; retry_count++) {

			if(retry_count > 0)
				wh_w8790_dev_send_commands(pdev, WH_CMD_FLASH_ERASE4K, start_addr);


			retval = wh_w8790_dev_write_flash_page(pdev, pdata, start_addr, page_size);
			if (!retval)
				continue;

			calc_checksum = misr_for_bytes(0, pdata, 0, page_size);

			retval = wh_w8790_dev_flash_get_checksum(pdev, &read_checksum, start_addr, page_size, 0);
			if (read_checksum == calc_checksum)
				break;
			else
				printf("checksum failed (%d): %d <> %d\n", retry_count, read_checksum, calc_checksum);

		}


		if (retry_count == RETRY_COUNT) {
			printf("***** retry failed *****\n");
			break;
		}
		start_addr = start_addr + page_size;
		pdata = pdata + page_size;
	}

	if (retry_count == RETRY_COUNT) {
		printf("stop 4k chunk program : fail \n");
		return 0;
	}
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %r12d
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
testl %r12d, %r12d
je 0x5c9d
leal -0x1000(%r12), %eax
xorl %ebx, %ebx
cmpl $0x1001, %r12d          # imm = 0x1001
cmovll %ebx, %eax
movl %eax, (%rsp)
movl $0x1000, %eax           # imm = 0x1000
cmpl %eax, %r12d
cmovgel %eax, %r12d
testl %ebx, %ebx
je 0x5c04
cmpl $0x3, %ebx
je 0x5c74
movq %r15, %rdi
movl %ebp, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x5e14
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %r12d, %ecx
callq 0x587a
testl %eax, %eax
je 0x5c5c
xorl %edi, %edi
movq %r14, %rsi
xorl %edx, %edx
movl %r12d, %ecx
callq 0x7ddb
movzwl %ax, %r13d
movq %r15, %rdi
leaq 0x4(%rsp), %rsi
movl %ebp, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x598e
movl 0x4(%rsp), %edx
cmpl %r13d, %edx
je 0x5c60
leaq 0x4a1e(%rip), %rdi      # 0xa66e
movl %ebx, %esi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2050
incl %ebx
jmp 0x5bec
addl %r12d, %ebp
movslq %r12d, %rax
addq %rax, %r14
movl (%rsp), %eax
movl %eax, %r12d
jmp 0x5bc0
leaq 0x4b36(%rip), %rdi      # 0xa7b1
callq 0x2270
leaq 0x4b43(%rip), %rdi      # 0xa7ca
callq 0x2270
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rax
jmp 0x5c8e
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_get_device_info(WdtDevice*, unsigned char*, int, int)
int wh_w8790_dev_get_device_info(WDT_DEV* pdev, BYTE* buf, int offset, int size)
{
	BYTE cmd[10] = { 0 };
	cmd[0] = (BYTE)W8790_COMMAND9;// , type, (BYTE)offset, (BYTE)size
	cmd[1] = W8790_GET_DEVICE_INFO;
	cmd[2] = (BYTE)offset;
	cmd[3] = (BYTE)size;

	return wh_w8790_dev_command_read(pdev, cmd, sizeof(cmd), buf, 0, size);

}

// Assembly:
subq $0x18, %rsp
movl %ecx, %r9d
leaq 0xe(%rsp), %rax
andw $0x0, 0x8(%rax)
movq %rsi, %rcx
andl $0x0, 0x4(%rax)
movw $0xc806, (%rax)         # imm = 0xC806
movb %dl, 0x2(%rax)
movb %r9b, 0x3(%rax)
movq %rax, %rsi
xorl %r8d, %r8d
callq 0x4ea5
addq $0x18, %rsp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_flash_erase(WdtDevice*, unsigned int, int)
int wh_w8790_dev_flash_erase(WDT_DEV* pdev, UINT32 address, int size)
{
	int ret = wh_w8790_dev_flash_erase_cmd(pdev, address, size);

	if (ret <= 0)
		printf("%s: addr: %x, size: %x\n", __FUNCTION__, address, size);

	return ret;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %edx, %ebx
movl %esi, %ebp
callq 0x5663
movl %eax, %r14d
testl %eax, %eax
jne 0x5e41
leaq 0x4ba7(%rip), %rdi      # 0xa9d6
leaq 0x4db0(%rip), %rsi      # 0xabe6
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2050
movl %r14d, %eax
popq %rbx
popq %r14
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_write_register(WdtDevice*, unsigned int, unsigned int)
int wh_w8790_dev_write_register(WDT_DEV* pdev, UINT32 address, UINT32 reg_value)
{
    BYTE cmd[10] = { 0 };
    
    cmd[0] = W8790_COMMAND9;
    cmd[1] = (BYTE)W8790_WRITE_REGISTER;
    put_unaligned_le32(address, &cmd[2]);
    put_unaligned_le32(reg_value, &cmd[6]);

    return wh_w8790_dev_command_write(pdev, cmd, 0, sizeof(cmd));
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebx
movl %esi, %eax
movq %rdi, %r14
leaq 0x10(%rsp), %rsi
andq $0x0, (%rsi)
movw $0xdf06, -0x2(%rsi)     # imm = 0xDF06
movl %eax, %edi
callq 0x668e
leaq 0x14(%rsp), %rsi
movl %ebx, %edi
callq 0x668e
leaq 0xe(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x4d06
addq $0x18, %rsp
popq %rbx
popq %r14
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_flash_section_validate(unsigned char*, unsigned int, int)
int wh_w8790_flash_section_validate(BYTE* data, UINT32 datalength, int start = 0)
{
	int ret = 0;
	W8790_FLASH_SECTION_HEADER header;

	header.Param1 = get_unaligned_le32(&data[12]);
	header.Param0 = get_unaligned_le32(&data[8]);
	header.PayloadSize = get_unaligned_le32(&data[4]);
	header.HeaderChecksum = get_unaligned_le16(&data[2]);
	header.Checksum = get_unaligned_le16(&data[0]);
	if (header.HeaderChecksum != wh_w8790_flash_section_header_checksum(header))
	{
		wh_printf("Invalid section header. \n");
		return 0;
	}
	if (start + sizeof(header) + header.PayloadSize > datalength)
	{
		wh_printf("Invalid section length. \n");
		return 0;
	}
	BYTE* payload;
	payload = (BYTE*)calloc(header.PayloadSize, sizeof(BYTE));
	if (!payload) {
		wh_printf("calloc fail !\n");
		return 0;
	}
	memcpy(payload, &data[16], header.PayloadSize);

	if (header.Checksum != misr_for_bytes(header.HeaderChecksum, payload, 0, header.PayloadSize / sizeof(BYTE))) {
		ret = 0;
		wh_printf("Payload checksum error.\n");
	}
	else 
	{
		ret = 1;
	}
		
	if (payload)
		free(payload);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, (%rsp)
movl %esi, 0x4(%rsp)
movq %rdi, %rbx
addq $0xc, %rdi
callq 0x6687
movl %eax, %r14d
shlq $0x20, %r14
leaq 0x8(%rbx), %rdi
callq 0x6687
movl %eax, %ebp
orq %r14, %rbp
leaq 0x4(%rbx), %rdi
callq 0x6687
movl %eax, %r14d
movq %r14, %r13
shlq $0x20, %r13
leaq 0x2(%rbx), %rdi
callq 0x6683
movl %eax, %r12d
movl %r12d, %r15d
shll $0x10, %r15d
orq %r13, %r15
movq %rbx, %rdi
callq 0x6683
movl %eax, %r13d
movzwl %ax, %edi
orq %r15, %rdi
movq %rbp, %rsi
callq 0x5e90
cmpw %ax, %r12w
jne 0x5f60
movslq (%rsp), %rax
addq %r14, %rax
addq $0x10, %rax
movl 0x4(%rsp), %ecx
cmpq %rcx, %rax
jbe 0x5f81
leaq 0x4cbb(%rip), %rdi      # 0xac19
jmp 0x5f67
leaq 0x4c98(%rip), %rdi      # 0xabff
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x24bf
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x2130
testq %rax, %rax
je 0x5fc4
movq %rax, %r15
addq $0x10, %rbx
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2150
movzwl %r12w, %edi
xorl %ebx, %ebx
movq %r15, %rsi
xorl %edx, %edx
movl %r14d, %ecx
callq 0x7ddb
cmpw %ax, %r13w
jne 0x5fcd
pushq $0x1
popq %rbx
jmp 0x5fdb
leaq 0x4c68(%rip), %rdi      # 0xac33
jmp 0x5f67
leaq 0x4c6e(%rip), %rdi      # 0xac42
xorl %eax, %eax
callq 0x24bf
movq %r15, %rdi
callq 0x21e0
jmp 0x5f70
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_prepare_data(WdtDevice*, BoardInfo*)
int wh_w8790_prepare_data(WDT_DEV* pdev, BOARD_INFO* p_out_board_info)
{
	if (!pdev || !p_out_board_info)
		return 0;
	wh_w8790_dev_set_basic_op(pdev);



	if (!wh_w8790_dev_identify_platform(pdev)) {
		wh_printf("can't get platform identify!\n");
		return 0;
	}
	//I:0x49 S:0x53 P:0x50 
	if (pdev->board_info.dev_info.w8790_feature_devinfo.program_name_fourcc[0] == 'I' &&
		pdev->board_info.dev_info.w8790_feature_devinfo.program_name_fourcc[1] == 'S' &&
		pdev->board_info.dev_info.w8790_feature_devinfo.program_name_fourcc[2] == 'P')
	{
		p_out_board_info->dev_type = FW_WDT8790_ISP;
		wh_printf("It is ISP mode!\n");


		return 1;

	}


	W8790_PCT pct_data;


	if (wh_w8790_dev_get_context(pdev, &pct_data)) {
		// set the default values
		p_out_board_info->sys_param.Phy_Frmbuf_W = pct_data.n_cs;
		p_out_board_info->sys_param.Phy_X0 = pct_data.x1;
		p_out_board_info->sys_param.Phy_X1 = pct_data.xn;

		p_out_board_info->sys_param.Phy_Frmbuf_H = pct_data.n_cd;
		p_out_board_info->sys_param.Phy_Y0 = pct_data.y1;
		p_out_board_info->sys_param.Phy_Y1 = pct_data.yn;

		p_out_board_info->sys_param.xmls_id2 = 0;

		W8790_PARAMETER_INFO parameter_info;
		wh_w8790_dev_read_parameter_table_info(pdev, &parameter_info);
		UINT32 cksum;
		wh_w8790_dev_read_parameters_get_checksum(pdev, parameter_info, &cksum);
		p_out_board_info->sys_param.xmls_id1 = cksum;

		return 1;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x6417
movq %rsi, %rbx
movq %rdi, %r14
callq 0x4db5
testl %eax, %eax
je 0x6403
cmpb $0x49, 0x220(%r14)
jne 0x6394
cmpb $0x53, 0x221(%r14)
jne 0x6394
cmpb $0x50, 0x222(%r14)
jne 0x6394
movl $0x201, (%rbx)          # imm = 0x201
leaq 0x4a09(%rip), %rdi      # 0xad94
xorl %eax, %eax
callq 0x24bf
jmp 0x63fe
leaq 0xa(%rsp), %rsi
movq %r14, %rdi
callq 0x4e14
testl %eax, %eax
je 0x6415
movzbl 0xc(%rsp), %eax
movw %ax, 0x24(%rbx)
movzbl 0xb(%rsp), %eax
movw %ax, 0x26(%rbx)
movd 0xe(%rsp), %xmm0
pxor %xmm1, %xmm1
punpcklbw %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3],xmm0[4],xmm1[4],xmm0[5],xmm1[5],xmm0[6],xmm1[6],xmm0[7],xmm1[7]
movq %xmm0, 0x28(%rbx)
andw $0x0, 0x22(%rbx)
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x5276
movq (%r15), %rsi
movl 0x8(%r15), %edx
leaq 0x14(%rsp), %r15
movq %r14, %rdi
movq %r15, %rcx
callq 0x5fe5
movzwl (%r15), %eax
movw %ax, 0x20(%rbx)
pushq $0x1
popq %rbp
jmp 0x6417
leaq 0x496c(%rip), %rdi      # 0xad76
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x24bf
jmp 0x6417
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: wh_get_device_access_func(int, FuncPtrStructDevAccess*)
int wh_get_device_access_func(int interfaceIndex, FUNC_PTR_STRUCT_DEV_ACCESS*  pFuncs)
{
	if (!pFuncs)
		return 0;

	if (interfaceIndex == INTERFACE_I2C) {
		pFuncs->p_wh_scan_device = (LPFUNC_wh_scan_device) wh_i2c_scan_device;
		pFuncs->p_wh_get_device = (LPFUNC_wh_get_device) wh_i2c_get_device;
		pFuncs->p_wh_open_device = (LPFUNC_wh_open_device) wh_i2c_open_device;
		pFuncs->p_wh_close_device = (LPFUNC_wh_close_device) wh_i2c_close_device;
		pFuncs->p_wh_prepare_data = (LPFUNC_wh_prepare_data) wh_i2c_prepare_data;			
		return 1;
	}

	return 0;
}

// Assembly:
testq %rsi, %rsi
sete %cl
cmpl $0x2, %edi
setne %dl
xorl %eax, %eax
orb %cl, %dl
jne 0x646f
leaq 0x1dd7(%rip), %rax      # 0x8214
movq %rax, (%rsi)
leaq 0x1ecb(%rip), %rax      # 0x8312
movq %rax, 0x8(%rsi)
leaq 0x1ef1(%rip), %rax      # 0x8343
movq %rax, 0x10(%rsi)
leaq 0x1fb1(%rip), %rax      # 0x840e
movq %rax, 0x18(%rsi)
leaq 0x2118(%rip), %rax      # 0x8580
movq %rax, 0x20(%rsi)
pushq $0x1
popq %rax
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: wh_get_device_private_access_func(WdtDevice*, FuncPtrStructDevOperation*)
int wh_get_device_private_access_func(WDT_DEV* pdev,  FUNC_PTR_STRUCT_DEV_OPERATION*  pFuncs)
{
	if (!pFuncs || !pdev)
		return 0;

	if (pdev->board_info.dev_type & FW_WDT8755) {
		pFuncs->p_wh_program_chunk = (LPFUNC_wh_program_chunk) wh_w8755_dev_program_chunk;
		pFuncs->p_wh_verify_chunk	= (LPFUNC_wh_verify_chunk) wh_w8755_dev_verify_chunk;
		pFuncs->p_wh_flash_write_data = (LPFUNC_wh_flash_write_data) wh_w8755_dev_flash_write_data;
		pFuncs->p_wh_flash_get_checksum = (LPFUNC_wh_flash_get_checksum) wh_w8755_dev_flash_get_checksum;
 		pFuncs->p_wh_send_commands = (LPFUNC_wh_send_commands) wh_w8755_dev_send_commands;

		return wh_w8755_dev_set_basic_op(pdev);
	}

	if (pdev->board_info.dev_type & FW_WDT8760_2) {
		pFuncs->p_wh_program_chunk = (LPFUNC_wh_program_chunk) wh_w8760_dev_program_chunk;
		pFuncs->p_wh_verify_chunk	= (LPFUNC_wh_verify_chunk) wh_w8760_dev_verify_chunk;
		pFuncs->p_wh_flash_write_data = (LPFUNC_wh_flash_write_data) wh_w8760_dev_flash_write_data;
		pFuncs->p_wh_flash_get_checksum = (LPFUNC_wh_flash_get_checksum) wh_w8760_dev_flash_get_checksum;
 		pFuncs->p_wh_send_commands = (LPFUNC_wh_send_commands) wh_w8760_dev_send_commands;

		return wh_w8760_dev_set_basic_op(pdev);
	}
	if (pdev->board_info.dev_type & FW_WDT8790) {
		pFuncs->p_wh_flash_write_data = (LPFUNC_wh_flash_write_data)wh_w8790_dev_flash_write_data;
		pFuncs->p_wh_flash_get_checksum = (LPFUNC_wh_flash_get_checksum)wh_w8790_dev_flash_get_checksum;
		pFuncs->p_wh_send_commands = (LPFUNC_wh_send_commands)wh_w8790_dev_send_commands;
		pFuncs->p_wh_flash_erase = (LPFUNC_wh_flash_erase)wh_w8790_dev_flash_erase;


		return wh_w8790_dev_set_basic_op(pdev);
	}


	return 0;

}

// Assembly:
testq %rsi, %rsi
sete %al
testq %rdi, %rdi
sete %cl
orb %al, %cl
jne 0x6510
movl 0x188(%rdi), %eax
testb $0x20, %al
jne 0x64cf
testw $0x2800, %ax            # imm = 0x2800
je 0x650a
leaq -0x18e6(%rip), %rax     # 0x4bb5
movq %rax, (%rsi)
leaq -0x1d7a(%rip), %rax     # 0x472b
movq %rax, 0x8(%rsi)
leaq -0x1e85(%rip), %rax     # 0x462b
movq %rax, 0x18(%rsi)
leaq -0x1e35(%rip), %rax     # 0x4686
movq %rax, 0x20(%rsi)
leaq -0x201a(%rip), %rax     # 0x44ac
movq %rax, 0x28(%rsi)
jmp 0x3c46
leaq -0x2daa(%rip), %rax     # 0x372c
movq %rax, (%rsi)
leaq -0x2dc0(%rip), %rax     # 0x3720
movq %rax, 0x8(%rsi)
leaq -0x398e(%rip), %rax     # 0x2b5d
movq %rax, 0x18(%rsi)
leaq -0x330b(%rip), %rax     # 0x31eb
movq %rax, 0x20(%rsi)
leaq -0x38cd(%rip), %rax     # 0x2c34
movq %rax, 0x28(%rsi)
jmp 0x377c
btl $0x9, %eax
jb 0x6513
xorl %eax, %eax
retq
leaq -0x73f(%rip), %rax      # 0x5ddb
movq %rax, 0x18(%rsi)
leaq -0xb97(%rip), %rax      # 0x598e
movq %rax, 0x20(%rsi)
leaq -0x88e(%rip), %rax      # 0x5ca2
movq %rax, 0x28(%rsi)
leaq -0x727(%rip), %rax      # 0x5e14
movq %rax, 0x30(%rsi)
jmp 0x4cfd
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: check_firmware_id(WdtDevice*, unsigned int)
int check_firmware_id(WDT_DEV *pdev, UINT32 fwid)
{
	if ((fwid & 0xF0000000) == 0x30000000) {
		if (pdev->pparam->argus & OPTION_INFO)		
			printf("It is WDT8755 or WDT8752 !\n");	
		return FW_WDT8755;
	}

	if ((fwid & 0xFFFF0000) == 0xFFFF0000) {
		if (pdev->pparam->argus & OPTION_INFO)		
			printf("It is WDT8752 recovery fw !\n");	
		return FW_WDT8755;
	}	

	if ((fwid & 0xF0000000) == 0x40000000) {
		if (pdev->pparam->argus & OPTION_INFO)	
			printf("It is WDT8760 alike !\n");	
		return FW_WDT8760;
	}

	if ((fwid & 0xFF000000) == 0x51000000) {
                if(pdev->pparam->argus & OPTION_INFO)
                        printf("It is WDT8730 !\n");
                return FW_NOT_SUPPORT;

        }

	//TODO: We will add new devices here and should before FW_WDT8970 for compatibility.

	if ((fwid & 0xFF000000) == 0x50000000) {
		if(pdev->pparam->argus & OPTION_INFO)
			printf("It is WDT8790 !\n");
		return FW_WDT8790;

	}
	
	return FW_WITH_CMD;
}

// Assembly:
pushq %rbx
movl %esi, %eax
andl $0xf0000000, %eax       # imm = 0xF0000000
cmpl $0x30000000, %eax       # imm = 0x30000000
jne 0x65ee
movq 0x280(%rdi), %rax
pushq $0x20
popq %rbx
testb $0x8, (%rax)
je 0x667a
leaq 0x5018(%rip), %rdi      # 0xb601
jmp 0x6675
cmpl $0xffff0000, %esi       # imm = 0xFFFF0000
jb 0x660e
movq 0x280(%rdi), %rax
pushq $0x20
popq %rbx
testb $0x8, (%rax)
je 0x667a
leaq 0x4fd9(%rip), %rdi      # 0xb5e5
jmp 0x6675
cmpl $0x40000000, %eax       # imm = 0x40000000
jne 0x662f
movq 0x280(%rdi), %rax
movl $0x800, %ebx            # imm = 0x800
testb $0x8, (%rax)
je 0x667a
leaq 0x4fa2(%rip), %rdi      # 0xb5cf
jmp 0x6675
andl $0xff000000, %esi       # imm = 0xFF000000
cmpl $0x50000000, %esi       # imm = 0x50000000
je 0x665d
cmpl $0x51000000, %esi       # imm = 0x51000000
jne 0x667e
movq 0x280(%rdi), %rax
pushq $0x4
popq %rbx
testb $0x8, (%rax)
je 0x667a
leaq 0x4f64(%rip), %rdi      # 0xb5bf
jmp 0x6675
movq 0x280(%rdi), %rax
movl $0x200, %ebx            # imm = 0x200
testb $0x8, (%rax)
je 0x667a
leaq 0x4f3a(%rip), %rdi      # 0xb5af
callq 0x2270
movl %ebx, %eax
popq %rbx
retq
pushq $0x2
popq %rbx
jmp 0x667a
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: check_is_all_ff(unsigned char*, int)
int check_is_all_ff(BYTE* data, int length)
{
	if (data == 0)
		return 0;

	int idx;

	for (idx = 0; idx < length; idx++)
		if (data[idx] != 0xFF)
			return 0;
	return 1;
}

// Assembly:
testq %rdi, %rdi
je 0x66af
xorl %ecx, %ecx
testl %esi, %esi
cmovlel %ecx, %esi
pushq $0x1
popq %rax
cmpq %rcx, %rsi
je 0x66b1
cmpb $-0x1, (%rdi,%rcx)
leaq 0x1(%rcx), %rcx
je 0x66a0
xorl %eax, %eax
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: count_ff_bytes(unsigned char*, int, int)
int count_ff_bytes(BYTE* data, int start, int size)
{
	int count = 0;
	for (int i = 0; i < size; i++)
	{
		if (data[start + i] == 0xFF)
			count++;
	}
	return count;
}

// Assembly:
movslq %esi, %rcx
xorl %esi, %esi
testl %edx, %edx
cmovlel %esi, %edx
addq %rdi, %rcx
xorl %eax, %eax
cmpq %rsi, %rdx
je 0x66d7
xorl %edi, %edi
cmpb $-0x1, (%rcx,%rsi)
sete %dil
addl %edi, %eax
incq %rsi
jmp 0x66c1
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: wh_open_whiff_file(char*)
WH_HANDLE wh_open_whiff_file(char* path)
{
	FILE*			pfile = NULL;
	WIF_FILE 		*pcur_wif = NULL;

	pfile = fopen(path, "rb");
	if (!pfile)
		return NULL;

	pcur_wif = (WIF_FILE*) malloc(sizeof(WIF_FILE));
	if (!pcur_wif) {
		fclose(pfile);
		return NULL;
	}

	/* set file ptr to the end */
	fseek(pfile, 0, SEEK_END);
	pcur_wif->data_len = ftell(pfile);

	/* set the file ptr the beginning */
	rewind(pfile);		

	pcur_wif->pdata = (BYTE*) malloc(pcur_wif->data_len + 32);

	if (!pcur_wif->pdata)
		goto failed;

	if (fread(pcur_wif->pdata, 1, pcur_wif->data_len, pfile) == pcur_wif->data_len)	{	
		process_whiff_file(pcur_wif);
		fclose(pfile);

		return (WH_HANDLE) pcur_wif;
	}

failed:
	wh_close_whiff_file((WH_HANDLE) pcur_wif);
		
	return NULL;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
leaq 0x4e17(%rip), %rsi      # 0xb55b
callq 0x21d0
testq %rax, %rax
je 0x67d7
movq %rax, %r14
pushq $0x18
popq %rdi
callq 0x21f0
testq %rax, %rax
je 0x67cf
movq %rax, %rbx
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x21c0
movq %r14, %rdi
callq 0x2030
movq %rax, %r15
movl %r15d, 0x8(%rbx)
movq %r14, %rdi
callq 0x2070
leal 0x20(%r15), %edi
callq 0x21f0
movq %rax, (%rbx)
testq %rax, %rax
je 0x67c5
movl %r15d, %edx
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r14, %rcx
callq 0x2240
movl 0x8(%rbx), %ecx
cmpq %rcx, %rax
jne 0x67c5
movq %rbx, %rdi
callq 0x7d0f
movq %r14, %rdi
callq 0x21a0
jmp 0x67d9
movq %rbx, %rdi
callq 0x67e2
jmp 0x67d7
movq %r14, %rdi
callq 0x21a0
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r14
popq %r15
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: wh_close_whiff_file(void*)
int wh_close_whiff_file(WH_HANDLE handle)
{
	WIF_FILE	*pcur_wif = (WIF_FILE*) handle;

	if (pcur_wif) {
		if (pcur_wif->pdata)
			free(pcur_wif->pdata);

		free(pcur_wif);
		return 1;
	}

	return 0;
}

// Assembly:
testq %rdi, %rdi
je 0x6800
pushq %rbx
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x21e0
movq %rbx, %rdi
callq 0x21e0
pushq $0x1
popq %rax
popq %rbx
retq
xorl %eax, %eax
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: wh_get_chunk_info(void*, unsigned int, ChunkInfoEx*)
int wh_get_chunk_info(WH_HANDLE handle, UINT32 chunk_index, CHUNK_INFO_EX* pchunk_info_ex)
{
	WIF_FILE	*pcur_wif = (WIF_FILE*) handle;

	if (!pcur_wif)
		return 0;

	UINT32	chunk_four_cc = get_chunk_fourcc (chunk_index);

	if (!chunk_four_cc)
		return 0;

	if (!pcur_wif->pformat_chunk)
		return 0;

	/* check if the chunk is existed */
	if (pcur_wif->pformat_chunk->enableFlag | chunk_index) {
		
		/* 12 is the header size */
		UINT32	chunk_start_pos = 12 + sizeof (FORMAT_CHUNK);
		CHUNK_DATA*	pchunk_data = NULL;

		while (chunk_start_pos < pcur_wif->data_len) {
			pchunk_data = (CHUNK_DATA*) & pcur_wif->pdata[chunk_start_pos];

			/* we got it */
			if (pchunk_data->ckID == chunk_four_cc)	{
				memcpy((void*) &pchunk_info_ex->chuckInfo, (void*) &pchunk_data->chunkInfo, sizeof(CHUNK_INFO));
				pchunk_info_ex->length = pchunk_data->chunkInfo.length;
				pchunk_info_ex->pData = (BYTE*) &pcur_wif->pdata[chunk_start_pos + 8 + sizeof(CHUNK_INFO)];
				
				return 1;
			} else
				/* 8 is the header size */
				chunk_start_pos = chunk_start_pos + pchunk_data->ckSize + 8;
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
xorl %ebx, %ebx
testq %rdi, %rdi
je 0x6882
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
movl %esi, %edi
callq 0x7c8c
testl %eax, %eax
je 0x6882
movq 0x10(%r15), %rcx
testq %rcx, %rcx
je 0x6882
orl 0xc(%rcx), %ebp
je 0x6882
movl 0x8(%r15), %edx
pushq $0x28
popq %rcx
cmpl %edx, %ecx
jae 0x6882
movq (%r15), %rdi
movl %ecx, %r8d
leaq (%rdi,%r8), %rsi
cmpl %eax, (%rdi,%r8)
je 0x6857
movl 0x4(%rsi), %esi
addl %esi, %ecx
addl $0x8, %ecx
jmp 0x6839
movq 0x18(%rsi), %rax
movq %rax, 0x10(%r14)
movups 0x8(%rsi), %xmm0
movups %xmm0, (%r14)
movl 0xc(%rsi), %eax
movl %eax, 0x20(%r14)
movq (%r15), %rax
addl $0x8, %ecx
addq %rcx, %rax
addq $0x18, %rax
movq %rax, 0x18(%r14)
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: load_wif(WdtDevice*, char*)
int load_wif(WDT_DEV *pdev, char *path)
{
	if (!pdev || !path)
		return 0;

	strcpy(pdev->wif_access.wif_path, path);

	pdev->wif_access.wif_handle = pdev->func_wh_open_whiff(pdev->wif_access.wif_path);

	if (!pdev->wif_access.wif_handle) {
		wh_printf("Parse input file error");
		return 0;
	}

	memset(&pdev->wif_access.wif_chunk_info, 0, sizeof(CHUNK_INFO_EX));

	return 1;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x68f7
movq %rdi, %rbx
leaq 0x58(%rdi), %r14
movq %r14, %rdi
callq 0x2180
movq %r14, %rdi
callq *0x308(%rbx)
movq %rax, 0x158(%rbx)
testq %rax, %rax
je 0x68e7
xorps %xmm0, %xmm0
movups %xmm0, 0x170(%rbx)
movups %xmm0, 0x160(%rbx)
andq $0x0, 0x180(%rbx)
pushq $0x1
popq %rbp
jmp 0x68f7
leaq 0x4572(%rip), %rdi      # 0xae60
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x24bf
movl %ebp, %eax
popq %rbx
popq %r14
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: fw_version_check(ExecParam*, BoardInfo*, ChunkInfoEx*)
int fw_version_check(EXEC_PARAM *pparam, BOARD_INFO *pinfo, CHUNK_INFO_EX *pchunk_info)
{
	if (!pparam || !pinfo || !pchunk_info)
		return 0;

	char	fw_id = ((pchunk_info->chuckInfo.versionNumber >> 12) & 0xF);
	char	chip_id = ((pinfo->firmware_id >> 12) & 0xF);
	
	if (fw_id != chip_id) {
		printf("This firmware is not matched with this chip, fwid(%x), chip_id(%x)\n", fw_id, chip_id);
		return 0;
	} 

	if (pinfo->dev_type & FW_WDT8760) {
		if ((pchunk_info->chuckInfo.temp & 0xFF000000) != 0x31000000) {			
			printf("The device type is not matched with this firmware (%x)\n", pchunk_info->chuckInfo.temp);
			return 0;
		}
	} else if (pinfo->dev_type & FW_WDT8762) {
		if ((pchunk_info->chuckInfo.temp & 0xFF000000) != 0x32000000) { 		
			printf("The device type is not matched with this firmware (%x)\n", pchunk_info->chuckInfo.temp);
			return 0;
		}
	}

	wh_printf("The firmware vession: dev %x, wif %x\n", pinfo->firmware_id,
			  pchunk_info->chuckInfo.versionNumber); 

	unsigned int fw_ver = pchunk_info->chuckInfo.versionNumber & 0xFFF;
	unsigned int chip_fw_ver = pinfo->firmware_id & 0xFFF;

	/* just ignore the recovery firmware */
	if (( fw_ver <= chip_fw_ver) && (chip_fw_ver != 0xFFF)) 
		return 2;

	return 1;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
testq %rdx, %rdx
sete %al
xorl %ebp, %ebp
orb %cl, %al
jne 0x6c15
movq %rdx, %r14
movq %rsi, %rbx
movl 0xc(%rdx), %edx
movl %edx, %eax
shrl $0xc, %eax
andl $0xf, %eax
movl 0x10(%rsi), %esi
movl %esi, %ecx
shrl $0xc, %ecx
andl $0xf, %ecx
cmpl %ecx, %eax
jne 0x6bbc
movl (%rbx), %eax
btl $0xb, %eax
jb 0x6bd2
btl $0xd, %eax
jae 0x6be6
movl 0x14(%r14), %eax
movl %eax, %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
cmpl $0x32000000, %ecx       # imm = 0x32000000
je 0x6be6
leaq 0x43b2(%rip), %rdi      # 0xaf61
xorl %ebp, %ebp
movl %eax, %esi
xorl %eax, %eax
callq 0x2050
jmp 0x6c15
leaq 0x435a(%rip), %rdi      # 0xaf1d
xorl %ebp, %ebp
movl %eax, %esi
movl %ecx, %edx
xorl %eax, %eax
callq 0x2050
jmp 0x6c15
movl 0x14(%r14), %eax
movl %eax, %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
cmpl $0x31000000, %ecx       # imm = 0x31000000
jne 0x6ba8
leaq 0x43ac(%rip), %rdi      # 0xaf99
xorl %eax, %eax
callq 0x24bf
movl $0xfff, %eax            # imm = 0xFFF
movl 0xc(%r14), %ecx
andl %eax, %ecx
movl 0x10(%rbx), %edx
andl %eax, %edx
cmpl %edx, %ecx
setbe %cl
cmpl %eax, %edx
setne %al
andb %cl, %al
movzbl %al, %ebp
incl %ebp
movl %ebp, %eax
popq %rbx
popq %r14
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: config_id_check(ExecParam*, BoardInfo*, ChunkInfoEx*)
int config_id_check(EXEC_PARAM *pparam, BOARD_INFO *pinfo, CHUNK_INFO_EX *pchunk_info)
{
	if (!pparam || !pinfo || !pchunk_info)
		return 0;

	unsigned int 	xmlId = 0;

	if (pchunk_info->chuckInfo.attribute & 0x2)
		xmlId = pchunk_info->chuckInfo.versionNumber;

	wh_printf("The config data in dev: %04x, wif: %04x\n", xmlId, pinfo->sys_param.xmls_id1);
	if (xmlId == pinfo->sys_param.xmls_id1) 
		return 2;

	return 1;
}

// Assembly:
pushq %rbp
pushq %rbx
pushq %rax
movq %rsi, %rbx
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
testq %rdx, %rdx
sete %sil
xorl %eax, %eax
orb %cl, %sil
jne 0x6c6a
xorl %ebp, %ebp
testb $0x2, 0x10(%rdx)
je 0x6c49
movl 0xc(%rdx), %ebp
movzwl 0x20(%rbx), %edx
leaq 0x436b(%rip), %rdi      # 0xafbf
movl %ebp, %esi
xorl %eax, %eax
callq 0x24bf
movzwl 0x20(%rbx), %ecx
xorl %eax, %eax
cmpl %ecx, %ebp
sete %al
incl %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: program_one_chunk(WdtDevice*, char const*, unsigned int, unsigned int, ChunkInfoEx*)
int program_one_chunk(WDT_DEV *pdev, const char *chunk_name, UINT32 chunk_id, UINT32 option, CHUNK_INFO_EX *pinfo)
{
	CHUNK_INFO_EX	chunk_info;
	CHUNK_INFO_EX	*pchunk_info = NULL;

	if (!pdev)
		return 0;

	if (pinfo == NULL) {
		memset(&chunk_info, 0, sizeof(CHUNK_INFO_EX));
		if (pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, chunk_id, &chunk_info))
			pchunk_info = &chunk_info;
		else
			return 1;
	} else
		pchunk_info = pinfo;

	if (!pchunk_info)
		return 0;
		
	printf("\n[%s] programming ....\n", chunk_name);
				
	if (!pdev->funcs_device_private.p_wh_program_chunk(pdev, pchunk_info, option ))	{
		char	msg[64];

		sprintf(msg, "[%s] Write chunk error", chunk_name);
		wh_printf(msg);
		return 0;	
	}

	printf("[%s] chunk program : pass...\n", chunk_name);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
testq %rdi, %rdi
je 0x6d03
movq %r8, %r12
movl %ecx, %ebp
movq %rsi, %rbx
movq %rdi, %r15
pushq $0x1
popq %r13
testq %r8, %r8
jne 0x6cc6
xorps %xmm0, %xmm0
movq %rsp, %r12
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
andq $0x0, 0x20(%r12)
movq 0x158(%r15), %rdi
movl %edx, %esi
movq %r12, %rdx
callq *0x318(%r15)
testl %eax, %eax
je 0x6cfe
leaq 0x431b(%rip), %rdi      # 0xafe8
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq *0x2b0(%r15)
testl %eax, %eax
je 0x6d08
leaq 0x4323(%rip), %rdi      # 0xb017
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
movl %r13d, %r14d
jmp 0x6d2b
xorl %r14d, %r14d
jmp 0x6d2b
leaq 0x42f1(%rip), %rsi      # 0xb000
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2080
movq %r15, %rdi
xorl %eax, %eax
callq 0x24bf
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: show_wif_info(WdtDevice*, ExecParam*)
int show_wif_info(WDT_DEV *pdev, EXEC_PARAM *pparam)
{
	if (!pdev || !pparam)
		return 0;
	if(show_wif2_info((char*)pparam->image_file))
		return 1;

	if (!load_wif(pdev, (char*)pparam->image_file))
		return 0;

	CHUNK_INFO_EX chunk_info_ex;
	if (pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, CHUNK_ID_FRWR, &chunk_info_ex)) 
		printf("FW id: 0x%08x\n", chunk_info_ex.chuckInfo.versionNumber);

	if (pdev->func_wh_get_chunk_info(pdev->wif_access.wif_handle, CHUNK_ID_CNFG, &chunk_info_ex)) {
		UINT32	config_id = 0;
		
		if (chunk_info_ex.chuckInfo.attribute & 0x2)
			config_id = chunk_info_ex.chuckInfo.versionNumber;
		printf("Config id: 0x%x\n", config_id);		
	}
	
	if (!close_wif(pdev))
		return 0; 
	
	return 1;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x30, %rsp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x77f7
movq %rsi, %r14
movq %rdi, %rbx
movq 0x10(%rsi), %rdi
callq 0x973a
testl %eax, %eax
je 0x777d
pushq $0x1
popq %rbp
jmp 0x77f7
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x688f
testl %eax, %eax
je 0x77f7
movq 0x158(%rbx), %rdi
pushq $0x1
popq %rsi
leaq 0x8(%rsp), %rdx
callq *0x318(%rbx)
testl %eax, %eax
je 0x77b8
movl 0x14(%rsp), %esi
leaq 0x3c52(%rip), %rdi      # 0xb403
xorl %eax, %eax
callq 0x2050
movq 0x158(%rbx), %rdi
pushq $0x2
popq %rsi
leaq 0x8(%rsp), %rdx
callq *0x318(%rbx)
testl %eax, %eax
je 0x77ed
movl 0x18(%rsp), %esi
shll $0x1e, %esi
sarl $0x1f, %esi
andl 0x14(%rsp), %esi
leaq 0x3c2c(%rip), %rdi      # 0xb412
xorl %eax, %eax
callq 0x2050
movq %rbx, %rdi
callq 0x68fe
movl %eax, %ebp
movl %ebp, %eax
addq $0x30, %rsp
popq %rbx
popq %r14
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: find_hid_dev_name(int, int, int, char*)
int find_hid_dev_name(int bus, int vid, int pid, char *device_name)
{
	int ret = 0;
	struct dirent * dev_dir_entry;
	DIR * dev_dir;
	char device_ids[32];

	snprintf(device_ids, 15, "%04X:%04X:%04X", bus, vid, pid);

	dev_dir = opendir("/sys/bus/hid/devices");
	if (!dev_dir) {
		printf("open dev dir failed !\n");
		return 0;
	}

	while ((dev_dir_entry = readdir(dev_dir)) != NULL) {
		if (!strncmp(dev_dir_entry->d_name, device_ids, 14)) {
			strcpy(device_name, dev_dir_entry->d_name);
			ret = 1;
			break;
		}
	}
	closedir(dev_dir);

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %rbx
movl %edx, %r9d
movl %esi, %r8d
movl %edi, %ecx
leaq 0x3c01(%rip), %rdx      # 0xb423
xorl %ebp, %ebp
movq %rsp, %rdi
pushq $0xf
popq %rsi
xorl %eax, %eax
callq 0x2290
leaq 0x3bfa(%rip), %rdi      # 0xb432
callq 0x2250
testq %rax, %rax
je 0x7882
movq %rax, %r14
movq %rsp, %r15
pushq $0xe
popq %r12
movq %r14, %rdi
callq 0x2260
testq %rax, %rax
je 0x7890
movq %rax, %r13
addq $0x13, %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x20e0
testl %eax, %eax
jne 0x784c
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2180
pushq $0x1
popq %rbp
jmp 0x7892
leaq 0x3e6f(%rip), %rdi      # 0xb6f8
callq 0x2270
jmp 0x789a
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x22a0
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: write_devname_to_sys_attr(char const*, char const*)
int write_devname_to_sys_attr(const char *attr, const char *action)
{
	int fd;
	ssize_t size;

	fd = open(attr, O_WRONLY);
	if (fd < 0) {
		printf("%s: open file error !", __func__);
		return 0;
	}

	for (;;) {
		size = write(fd, action, strlen(action));
		if (size < 0) {
			if (errno == EINTR)
				continue;

			return 0;
		}
		break;
	}

	close(fd);

	return (size == (ssize_t) strlen(action));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
xorl %ebp, %ebp
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x7add
movl %eax, %r14d
movq %rbx, %rdi
callq 0x20d0
movl %r14d, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x20b0
testq %rax, %rax
jns 0x7af4
callq 0x2040
cmpl $0x4, (%rax)
je 0x7ab4
xorl %ebp, %ebp
jmp 0x7b10
leaq 0x39a4(%rip), %rdi      # 0xb488
leaq 0x39b3(%rip), %rsi      # 0xb49e
xorl %eax, %eax
callq 0x2050
jmp 0x7b10
movq %rax, %r15
movl %r14d, %edi
callq 0x22e0
movq %rbx, %rdi
callq 0x20d0
xorl %ebp, %ebp
cmpq %rax, %r15
sete %bpl
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq