// File: /workspace/github_c_makefile_star2_O0/sharpyfox[P]citrusleaf-client/src/citrusleaf.c
// Function: write_fields
static uint8_t *
write_fields(uint8_t *buf, const char *ns, int ns_len, const char *set, int set_len, const cl_object *key, const cf_digest *d, cf_digest *d_ret, 
	uint64_t trid, cl_scan_param_field *scan_param_field)
{
	
	// lay out the fields
	cl_msg_field *mf = (cl_msg_field *) buf;
	cl_msg_field *mf_tmp = mf;
	
	if (ns) {
		mf->type = CL_MSG_FIELD_TYPE_NAMESPACE;
		mf->field_sz = ns_len + 1;
		memcpy(mf->data, ns, ns_len);
		mf_tmp = cl_msg_field_get_next(mf);
		cl_msg_swap_field(mf);
		mf = mf_tmp;
	}

	if (set) {	
		mf->type = CL_MSG_FIELD_TYPE_SET;
		mf->field_sz = set_len + 1;
		memcpy(mf->data, set, set_len);
		mf_tmp = cl_msg_field_get_next(mf);
		cl_msg_swap_field(mf);
		mf = mf_tmp;
	}

	if (trid) {
		mf->type = CL_MSG_FIELD_TYPE_TRID;
		//Convert the transaction-id to network byte order (big-endian)
		uint64_t trid_nbo = __cpu_to_be64(trid); //swaps in place
		mf->field_sz = sizeof(trid_nbo) + 1;
		memcpy(mf->data, &trid_nbo, sizeof(trid_nbo));
		mf_tmp = cl_msg_field_get_next(mf);
		cl_msg_swap_field(mf);
		mf = mf_tmp;
	}

	if (scan_param_field) {
		mf->type = CL_MSG_FIELD_TYPE_SCAN_OPTIONS;
		mf->field_sz = sizeof(cl_scan_param_field) + 1;
		memcpy(mf->data, scan_param_field, sizeof(cl_scan_param_field));
		mf_tmp = cl_msg_field_get_next(mf);
		cl_msg_swap_field(mf);
		mf = mf_tmp;
	}	

	if (key) {
		mf->type = CL_MSG_FIELD_TYPE_KEY;
		// make a function call here, similar to our prototype code in the server
		uint8_t *fd = (uint8_t *) &mf->data;
		switch (key->type) {
			case CL_STR:
				fd[0] = key->type;
				mf->field_sz = key->sz + 2;
				memcpy(&fd[1], key->u.str, key->sz);
				break;
			case CL_INT:
				fd[0] = key->type;
				mf->field_sz = value_to_op_int(key->u.i64, &fd[1]) + 2; 
                uint64_t swapped = __swab64(key->u.i64);
                memcpy(&fd[1], &swapped, sizeof(swapped));
				break;
			case CL_BLOB:
			case CL_JAVA_BLOB:
			case CL_CSHARP_BLOB:
			case CL_PYTHON_BLOB:
			case CL_RUBY_BLOB:
			case CL_PHP_BLOB:
				fd[0] = key->type;
				mf->field_sz = key->sz + 2;
				memcpy(&fd[1], key->u.blob, key->sz);
				break;
			default:
				cf_error("transmit key: unknown citrusleaf type %d",key->type);
				return(0);
		}
		mf_tmp = cl_msg_field_get_next(mf);
		cl_msg_swap_field(mf);
		
		// calculate digest
		if (d_ret) {
			cf_digest_compute2( (char *)set, set_len, mf->data, key->sz + 1, d_ret);
		}
		
		mf = mf_tmp;
	}
	
	if (d) {
		mf->type = CL_MSG_FIELD_TYPE_DIGEST_RIPE;
		mf->field_sz = sizeof(cf_digest) + 1;
		memcpy(mf->data, d, sizeof(cf_digest));
		mf_tmp = cl_msg_field_get_next(mf);
		cl_msg_swap_field(mf);
		if (d_ret)
			memcpy(d_ret, d, sizeof(cf_digest));

		mf = mf_tmp;
		
	}
	return ( (uint8_t *) mf_tmp );
}

// Assembly:
subq $0x78, %rsp
movq 0x98(%rsp), %rax
movq 0x90(%rsp), %rax
movq 0x88(%rsp), %rax
movq 0x80(%rsp), %rax
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movl %edx, 0x5c(%rsp)
movq %rcx, 0x50(%rsp)
movl %r8d, 0x4c(%rsp)
movq %r9, 0x40(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x60(%rsp)
je 0x674f
movq 0x38(%rsp), %rax
movb $0x0, 0x4(%rax)
movl 0x5c(%rsp), %ecx
addl $0x1, %ecx
movq 0x38(%rsp), %rax
movl %ecx, (%rax)
movq 0x38(%rsp), %rdi
addq $0x5, %rdi
movq 0x60(%rsp), %rsi
movslq 0x5c(%rsp), %rdx
callq 0x21e0
movq 0x38(%rsp), %rdi
callq 0x7220
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
callq 0x13cd0
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x67a9
movq 0x38(%rsp), %rax
movb $0x1, 0x4(%rax)
movl 0x4c(%rsp), %ecx
addl $0x1, %ecx
movq 0x38(%rsp), %rax
movl %ecx, (%rax)
movq 0x38(%rsp), %rdi
addq $0x5, %rdi
movq 0x50(%rsp), %rsi
movslq 0x4c(%rsp), %rdx
callq 0x21e0
movq 0x38(%rsp), %rdi
callq 0x7220
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
callq 0x13cd0
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x90(%rsp)
je 0x6815
movq 0x38(%rsp), %rax
movb $0x7, 0x4(%rax)
movq 0x90(%rsp), %rdi
callq 0x7210
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x38(%rsp), %rax
movl $0x9, (%rax)
movq 0x38(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x5(%rax)
movq 0x38(%rsp), %rdi
callq 0x7220
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
callq 0x13cd0
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x98(%rsp)
je 0x686b
movq 0x38(%rsp), %rax
movb $0x8, 0x4(%rax)
movq 0x38(%rsp), %rax
movl $0x3, (%rax)
movq 0x38(%rsp), %rax
movq 0x98(%rsp), %rcx
movw (%rcx), %cx
movw %cx, 0x5(%rax)
movq 0x38(%rsp), %rdi
callq 0x7220
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
callq 0x13cd0
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x40(%rsp)
je 0x6a46
movq 0x38(%rsp), %rax
movb $0x2, 0x4(%rax)
movq 0x38(%rsp), %rax
addq $0x5, %rax
movq %rax, 0x20(%rsp)
movq 0x40(%rsp), %rax
movl (%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, 0x8(%rsp)
subl $0xa, %eax
ja 0x69b0
movq 0x8(%rsp), %rax
leaq 0x11edd(%rip), %rcx     # 0x18790
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x40(%rsp), %rax
movl (%rax), %eax
movb %al, %cl
movq 0x20(%rsp), %rax
movb %cl, (%rax)
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
addq $0x2, %rax
movl %eax, %ecx
movq 0x38(%rsp), %rax
movl %ecx, (%rax)
movq 0x20(%rsp), %rdi
addq $0x1, %rdi
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rdx
callq 0x21e0
jmp 0x69eb
movq 0x40(%rsp), %rax
movl (%rax), %eax
movb %al, %cl
movq 0x20(%rsp), %rax
movb %cl, (%rax)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rsp), %rsi
addq $0x1, %rsi
callq 0x5ef0
movl %eax, %ecx
addl $0x2, %ecx
movq 0x38(%rsp), %rax
movl %ecx, (%rax)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x7210
movq %rax, (%rsp)
movq (%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x1(%rax)
jmp 0x69eb
movq 0x40(%rsp), %rax
movl (%rax), %eax
movb %al, %cl
movq 0x20(%rsp), %rax
movb %cl, (%rax)
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
addq $0x2, %rax
movl %eax, %ecx
movq 0x38(%rsp), %rax
movl %ecx, (%rax)
movq 0x20(%rsp), %rdi
addq $0x1, %rdi
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rdx
callq 0x21e0
jmp 0x69eb
leaq 0x17921(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x69dd
leaq 0x1791a(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
movq 0x40(%rsp), %rax
movl (%rax), %edx
xorl %edi, %edi
leaq 0x11f2d(%rip), %rsi     # 0x18906
movb $0x0, %al
callq *%rcx
movq $0x0, 0x70(%rsp)
jmp 0x6ae7
movq 0x38(%rsp), %rdi
callq 0x7220
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
callq 0x13cd0
cmpq $0x0, 0x88(%rsp)
je 0x6a3c
movq 0x50(%rsp), %rdi
movslq 0x4c(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq $0x5, %rdx
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq 0x88(%rsp), %r8
callq 0x96d0
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x80(%rsp)
je 0x6add
movq 0x38(%rsp), %rax
movb $0x4, 0x4(%rax)
movq 0x38(%rsp), %rax
movl $0x15, (%rax)
movq 0x38(%rsp), %rax
movq 0x80(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x5(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0xd(%rax)
movl 0x10(%rcx), %ecx
movl %ecx, 0x15(%rax)
movq 0x38(%rsp), %rdi
callq 0x7220
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
callq 0x13cd0
cmpq $0x0, 0x88(%rsp)
je 0x6ad3
movq 0x88(%rsp), %rax
movq 0x80(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movl 0x10(%rcx), %ecx
movl %ecx, 0x10(%rax)
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)