// File: /workspace/github_c_makefile_star2_O0/truenas[P]binaries/src/plx_eeprom.c
// Function: plx_eeprom_write
static int
plx_eeprom_write(void *mem, u_int addr, uint32_t data)
{
	int i;
	uint32_t val;

	if ((addr > 0x00ffffff) || (addr & 0x3) != 0)
		errx(EX_OSERR, "Incorrect EEPROM address %x", addr);
	addr >>= 2;

	/* Write address byte 3. */
	val = read4(mem, 0x26c);
	write4(mem, 0x26c, (val & ~0xff) | (addr >> 14));

	/* Enable write. */
	val = read4(mem, 0x260);
	val &= 0xffe00000;
	val |= (6 << 13);
	write4(mem, 0x260, val);

	/* Wait for completion. */
	i = 100;
	while (((val = read4(mem, 0x260)) & 0x40000) != 0 && val > 0) {
		usleep(100);
		i--;
	}
	if (i == 0)
		errx(EX_OSERR, "EEPROM write enable timeout");

	/* Prepare data. */
	write4(mem, 0x264, data);

	/* Initiate write. */
	val = read4(mem, 0x260);
	val &= 0xffe00000;
	val |= ((addr & 0x2000) << 7) | (2 << 13) | (addr & 0x1fff);
	write4(mem, 0x260, val);

	/* Wait for completion. */
	i = 100;
	while (((val = read4(mem, 0x260)) & 0x40000) != 0 && val > 0) {
		usleep(100);
		i--;
	}
	if (i == 0)
		errx(EX_OSERR, "EEPROM write timeout");
	return (0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
cmpl $0xffffff, -0xc(%rbp)   # imm = 0xFFFFFF
ja 0x1a36
movl -0xc(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1a4c
movl -0xc(%rbp), %edx
movl $0x47, %edi
leaq 0x6e6(%rip), %rsi       # 0x212b
movb $0x0, %al
callq 0x1060
movl -0xc(%rbp), %eax
shrl $0x2, %eax
movl %eax, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movl $0x26c, %esi            # imm = 0x26C
callq 0x1260
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %edx
andl $0xffffff00, %edx       # imm = 0xFFFFFF00
movl -0xc(%rbp), %eax
shrl $0xe, %eax
orl %eax, %edx
movl $0x26c, %esi            # imm = 0x26C
callq 0x1280
movq -0x8(%rbp), %rdi
movl $0x260, %esi            # imm = 0x260
callq 0x1260
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
andl $0xffe00000, %eax       # imm = 0xFFE00000
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
orl $0xc000, %eax           # imm = 0xC000
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %edx
movl $0x260, %esi            # imm = 0x260
callq 0x1280
movl $0x64, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movl $0x260, %esi            # imm = 0x260
callq 0x1260
movl %eax, %ecx
movl %ecx, -0x18(%rbp)
andl $0x40000, %ecx          # imm = 0x40000
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x19(%rbp)
je 0x1af1
cmpl $0x0, -0x18(%rbp)
seta %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
testb $0x1, %al
jne 0x1afa
jmp 0x1b0f
movl $0x64, %edi
callq 0x1150
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1ac4
cmpl $0x0, -0x14(%rbp)
jne 0x1b28
movl $0x47, %edi
leaq 0x674(%rip), %rsi       # 0x2195
movb $0x0, %al
callq 0x1060
movq -0x8(%rbp), %rdi
movl -0x10(%rbp), %edx
movl $0x264, %esi            # imm = 0x264
callq 0x1280
movq -0x8(%rbp), %rdi
movl $0x260, %esi            # imm = 0x260
callq 0x1260
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
andl $0xffe00000, %eax       # imm = 0xFFE00000
movl %eax, -0x18(%rbp)
movl -0xc(%rbp), %eax
andl $0x2000, %eax           # imm = 0x2000
shll $0x7, %eax
orl $0x4000, %eax           # imm = 0x4000
movl -0xc(%rbp), %ecx
andl $0x1fff, %ecx           # imm = 0x1FFF
orl %ecx, %eax
orl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %edx
movl $0x260, %esi            # imm = 0x260
callq 0x1280
movl $0x64, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movl $0x260, %esi            # imm = 0x260
callq 0x1260
movl %eax, %ecx
movl %ecx, -0x18(%rbp)
andl $0x40000, %ecx          # imm = 0x40000
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x1a(%rbp)
je 0x1bbb
cmpl $0x0, -0x18(%rbp)
seta %al
movb %al, -0x1a(%rbp)
movb -0x1a(%rbp), %al
testb $0x1, %al
jne 0x1bc4
jmp 0x1bd9
movl $0x64, %edi
callq 0x1150
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1b8e
cmpl $0x0, -0x14(%rbp)
jne 0x1bf2
movl $0x47, %edi
leaq 0x55c(%rip), %rsi       # 0x2147
movb $0x0, %al
callq 0x1060
xorl %eax, %eax
addq $0x20, %rsp
popq %rbp
retq
nopw (%rax,%rax)