// File: sundale[P]gcode/driver.cc
// Function: spi_write_multi(int, unsigned short, unsigned short*, unsigned short)
static int spi_write_multi(int fd,uint16_t addr ,uint16_t *buf,uint16_t count)
{
	
	int ret;
	uint8_t tx_temp[4];
	uint8_t rx_temp[4];
	uint16_t *t_temp,*r_temp;
	int i;
	struct spi_ioc_transfer *tr;
	uint16_t my_addr;
	if((count>20)||(count == 0))
		return 0;
	
	tr = (struct spi_ioc_transfer *)malloc(sizeof(struct spi_ioc_transfer)*(count*2));
	if(tr == NULL)
		return -1;
	t_temp = (uint16_t *)malloc(sizeof(uint16_t)*(count*2));
	if(t_temp == NULL)
	{
		free(tr);
		return -1;
	}
	r_temp = (uint16_t *)malloc(sizeof(uint16_t)*(count*2));
	if(r_temp == NULL)
	{
		free(tr);
		free(t_temp);
		return -1;
	}
	memset(tx_temp,0xff,4);
	memset(rx_temp,0,4);
	tx_temp[0] = (addr >> 8) | 0x80;
	tx_temp[1] = (addr & 0x00ff);
	my_addr = addr;


	for(i=0;i<count;i++)
	{
		t_temp[i*2] = (((my_addr&0x00ff)<<8)|(my_addr>>8));
		t_temp[i*2] |= 0x0080;
		r_temp[i*2] = 0x00;
		tr[i*2].tx_buf = (unsigned long)&(t_temp[i*2]);
		tr[i*2].rx_buf = (unsigned long)&r_temp[i*2]; 
		tr[i*2].len = 2;
		tr[i*2].delay_usecs = 0;
		tr[i*2].speed_hz = SPI_SPEED;
		tr[i*2].bits_per_word = SPI_BITS;
		tr[i*2].cs_change = 0;
		t_temp[i*2+1] = (((buf[i]&0x00ff)<<8)|(buf[i]>>8));
		r_temp[i*2+1] = 0x00;
		tr[i*2+1].tx_buf = (unsigned long)&t_temp[i*2+1];
		tr[i*2+1].rx_buf = (unsigned long)(r_temp[i*2+1]); 
		tr[i*2+1].len = 2;
		tr[i*2+1].delay_usecs = 0;
		tr[i*2+1].speed_hz = SPI_SPEED;
		tr[i*2+1].bits_per_word = SPI_BITS;
		if(i==(count-1))
			tr[i*2+1].cs_change = 0;
		else
			tr[i*2+1].cs_change = 1;
		my_addr = my_addr +2;
	}
	ret = ioctl(fd, SPI_IOC_MESSAGE(count*2), tr);
	if (ret < 1)
		pabort("can't send spi message");
	free(tr);
	free(r_temp);
	free(t_temp);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movw %cx, %ax
movw %si, %cx
movl %edi, -0x8(%rbp)
movw %cx, -0xa(%rbp)
movq %rdx, -0x18(%rbp)
movw %ax, -0x1a(%rbp)
movzwl -0x1a(%rbp), %eax
cmpl $0x14, %eax
jg 0x493f
movzwl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jne 0x494b
movl $0x0, -0x4(%rbp)
jmp 0x4d5a
movzwl -0x1a(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq $0x5, %rdi
callq 0x4230
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x4974
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4d5a
movzwl -0x1a(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq %rdi
callq 0x4230
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x49a5
movq -0x48(%rbp), %rdi
callq 0x41e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4d5a
movzwl -0x1a(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq %rdi
callq 0x4230
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x49df
movq -0x48(%rbp), %rdi
callq 0x41e0
movq -0x30(%rbp), %rdi
callq 0x41e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4d5a
leaq -0x24(%rbp), %rdi
movl $0xff, %esi
movl $0x4, %edx
callq 0x40e0
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x40e0
movzwl -0xa(%rbp), %eax
sarl $0x8, %eax
orl $0x80, %eax
movb %al, -0x24(%rbp)
movzwl -0xa(%rbp), %eax
andl $0xff, %eax
movb %al, -0x23(%rbp)
movw -0xa(%rbp), %ax
movw %ax, -0x4a(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movzwl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4cd2
movzwl -0x4a(%rbp), %eax
andl $0xff, %eax
shll $0x8, %eax
movzwl -0x4a(%rbp), %ecx
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %dx
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %edx
orl $0x80, %edx
movw %dx, (%rax,%rcx,2)
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
movw $0x0, (%rax,%rcx,2)
movq -0x30(%rbp), %rcx
movl -0x3c(%rbp), %eax
shll %eax
cltq
shlq %rax
addq %rax, %rcx
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %edx
shll %edx
movslq %edx, %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rcx
movl -0x3c(%rbp), %eax
shll %eax
cltq
shlq %rax
addq %rax, %rcx
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %edx
shll %edx
movslq %edx, %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl $0x2, 0x10(%rax)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movw $0x0, 0x18(%rax)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl $0xf4240, 0x14(%rax)    # imm = 0xF4240
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x8, 0x1a(%rax)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x0, 0x1b(%rax)
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0xff, %eax
shll $0x8, %eax
movq -0x18(%rbp), %rcx
movslq -0x3c(%rbp), %rdx
movzwl (%rcx,%rdx,2), %ecx
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %dx
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw $0x0, (%rax,%rcx,2)
movq -0x30(%rbp), %rcx
movl -0x3c(%rbp), %eax
shll %eax
addl $0x1, %eax
cltq
shlq %rax
addq %rax, %rcx
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl $0x2, 0x10(%rax)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movw $0x0, 0x18(%rax)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl $0xf4240, 0x14(%rax)    # imm = 0xF4240
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x8, 0x1a(%rax)
movl -0x3c(%rbp), %eax
movzwl -0x1a(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x4c9f
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x0, 0x1b(%rax)
jmp 0x4cb9
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x1, 0x1b(%rax)
movzwl -0x4a(%rbp), %eax
addl $0x2, %eax
movw %ax, -0x4a(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x4a2c
movl -0x8(%rbp), %eax
movl %eax, -0x50(%rbp)
movzwl -0x1a(%rbp), %eax
shll %eax
cltq
shlq $0x5, %rax
cmpq $0x4000, %rax           # imm = 0x4000
jae 0x4cfe
movzwl -0x1a(%rbp), %eax
shll %eax
cltq
shlq $0x5, %rax
movq %rax, -0x58(%rbp)
jmp 0x4d06
xorl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0x4d06
movl -0x50(%rbp), %edi
movq -0x58(%rbp), %rsi
shlq $0x10, %rsi
orq $0x40006b00, %rsi       # imm = 0x40006B00
movq -0x48(%rbp), %rdx
movb $0x0, %al
callq 0x4250
movl %eax, -0x20(%rbp)
cmpl $0x1, -0x20(%rbp)
jge 0x4d38
leaq 0x6343(%rip), %rdi      # 0xb076
callq 0x4e80
movq -0x48(%rbp), %rdi
callq 0x41e0
movq -0x38(%rbp), %rdi
callq 0x41e0
movq -0x30(%rbp), %rdi
callq 0x41e0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)