// File: /workspace/github_c_makefile_star2_O0/GangZhuo[P]dohclient/src/ns_msg.c
// Function: ns_set_ecs4
static int ns_set_ecs4(ns_opt_t* opt, struct sockaddr_in* addr, int srcprefix, int scopeprefix)
{
	stream_t s = STREAM_INIT();
	uint8_t* optdata;
	int addrlen;
	uint8_t saddr[4];
	uint8_t mask = 0xff;

	memcpy(saddr, (uint8_t*)(&addr->sin_addr), 4);

	addrlen = srcprefix / 8;
	if (srcprefix % 8)
		addrlen++;

	if (addrlen > 4)
		addrlen = 4;

	optdata = realloc(opt->data, 4 + addrlen);
	if (optdata == NULL) {
		return -1;
	}
	
	if (srcprefix % 8) {
		mask <<= (8 - (srcprefix % 8));
		saddr[addrlen - 1] &= mask;
	}

	opt->length = 4 + addrlen;
	opt->data = optdata;
	s.array = opt->data;
	s.cap = opt->length;
	if (stream_writei16(&s, ADDR_FAMILY_NUM_IP) != 2)
		return -1;
	if (stream_writei8(&s, srcprefix) != 1)
		return -1;
	if (stream_writei8(&s, scopeprefix) != 1)
		return -1;
	if (stream_write(&s, (const char*)saddr, addrlen) != addrlen)
		return -1;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3200
movb $-0x1, -0x49(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x44(%rbp)
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x29b1b
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
cmpl $0x4, -0x44(%rbp)
jle 0x29b28
movl $0x4, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x44(%rbp), %eax
addl $0x4, %eax
movslq %eax, %rsi
callq 0x35c0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x29b55
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x29c4f
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x29b97
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl $0x8, %ecx
subl %edx, %ecx
movzbl -0x49(%rbp), %eax
shll %cl, %eax
movb %al, -0x49(%rbp)
movzbl -0x49(%rbp), %edx
movl -0x44(%rbp), %eax
subl $0x1, %eax
cltq
movzbl -0x48(%rbp,%rax), %ecx
andl %edx, %ecx
movb %cl, -0x48(%rbp,%rax)
movl -0x44(%rbp), %eax
addl $0x4, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0x28(%rbp)
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x29bec
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x29c4f
movl -0x1c(%rbp), %esi
leaq -0x38(%rbp), %rdi
movl $0x1, %edx
callq 0x2dc90
cmpl $0x1, %eax
je 0x29c0b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x29c4f
movl -0x20(%rbp), %esi
leaq -0x38(%rbp), %rdi
movl $0x1, %edx
callq 0x2dc90
cmpl $0x1, %eax
je 0x29c2a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x29c4f
leaq -0x48(%rbp), %rsi
movl -0x44(%rbp), %edx
leaq -0x38(%rbp), %rdi
callq 0x2d9e0
cmpl -0x44(%rbp), %eax
je 0x29c48
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x29c4f
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)