// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/deark-ucstring.c
// Function: append_bytes_utf16
static void append_bytes_utf16(de_ucstring *s, const u8 *cbuf, i64 buflen,
	unsigned int conv_flags, struct de_encconv_state *es, int is_le)
{
	i64 pos = 0;
	i32 ch;

	for(pos=0; pos<buflen; pos++) {
		u8 n = cbuf[pos];

		es->buf[UTF16_NBYTES_SAVED] = n;
		UTF16_NBYTES_SAVED++;

		if(UTF16_NBYTES_SAVED==2) {
			ch = (i32)getu16x_direct(es->buf, is_le);
			if(ch>=0xd800 && ch<0xdc00) {
				; // lead surrogate; do nothing
			}
			else if(ch>=0xdc00 && ch<0xe000) {
				// trail surrogate, shouldn't be here
				ucstring_append_char(s, 0xfffd);
				UTF16_NBYTES_SAVED = 0;
			}
			else { // non-surrogate
				ucstring_append_char(s, ch);
				UTF16_NBYTES_SAVED = 0;
			}
		}
		else if(UTF16_NBYTES_SAVED>=4) { // >4 is impossible
			i32 ch2;

			ch = (i32)getu16x_direct(es->buf, is_le);
			ch2 = (i32)getu16x_direct(&es->buf[2], is_le);

			if(ch2>=0xd800 && ch2<0xdc00) {
				; // lead surrogate immediately following another lead surrogate
				ucstring_append_char(s, 0xfffd);
				es->buf[0] = es->buf[2];
				es->buf[1] = es->buf[3];
				UTF16_NBYTES_SAVED = 2;
			}
			else if(ch2>=0xdc00 && ch2<0xe000) {
				// well-formed surrogate pair
				ucstring_append_char(s, 0x10000 + (((ch-0xd800)<<10) | (ch2-0xdc00)));
				UTF16_NBYTES_SAVED = 0;
			}
			else { // non-surrogate immediately following a lead surrogate
				ucstring_append_char(s, 0xfffd);
				ucstring_append_char(s, ch2);
				UTF16_NBYTES_SAVED = 0;
			}

		}
	}

	// Check if there are unprocessed bytes at the end of the string, when there
	// shouldn't be.
	if(UTF16_NBYTES_SAVED!=0 && !(conv_flags & DE_CONVFLAG_PARTIAL_DATA)) {
		ucstring_append_char(s, 0xfffd);
		UTF16_NBYTES_SAVED = 0;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x171d71
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x3d(%rbp)
movb -0x3d(%rbp), %dl
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rcx
movzbl 0x1f(%rcx), %ecx
movb %dl, 0x18(%rax,%rcx)
movq -0x28(%rbp), %rax
movb 0x1f(%rax), %cl
addb $0x1, %cl
movb %cl, 0x1f(%rax)
movq -0x28(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpl $0x2, %eax
jne 0x171c74
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
movl -0x2c(%rbp), %esi
callq 0x172c60
movl %eax, -0x3c(%rbp)
cmpl $0xd800, -0x3c(%rbp)    # imm = 0xD800
jl 0x171c2f
cmpl $0xdc00, -0x3c(%rbp)    # imm = 0xDC00
jge 0x171c2f
jmp 0x171c6f
cmpl $0xdc00, -0x3c(%rbp)    # imm = 0xDC00
jl 0x171c59
cmpl $0xe000, -0x3c(%rbp)    # imm = 0xE000
jge 0x171c59
movq -0x8(%rbp), %rdi
movl $0xfffd, %esi           # imm = 0xFFFD
callq 0x1714c0
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
jmp 0x171c6d
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x1714c0
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
jmp 0x171c6f
jmp 0x171d5e
movq -0x28(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpl $0x4, %eax
jl 0x171d5c
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
movl -0x2c(%rbp), %esi
callq 0x172c60
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
addq $0x2, %rdi
movl -0x2c(%rbp), %esi
callq 0x172c60
movl %eax, -0x44(%rbp)
cmpl $0xd800, -0x44(%rbp)    # imm = 0xD800
jl 0x171cf5
cmpl $0xdc00, -0x44(%rbp)    # imm = 0xDC00
jge 0x171cf5
movq -0x8(%rbp), %rdi
movl $0xfffd, %esi           # imm = 0xFFFD
callq 0x1714c0
movq -0x28(%rbp), %rax
movb 0x1a(%rax), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x18(%rax)
movq -0x28(%rbp), %rax
movb 0x1b(%rax), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x19(%rax)
movq -0x28(%rbp), %rax
movb $0x2, 0x1f(%rax)
jmp 0x171d5a
cmpl $0xdc00, -0x44(%rbp)    # imm = 0xDC00
jl 0x171d36
cmpl $0xe000, -0x44(%rbp)    # imm = 0xE000
jge 0x171d36
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
subl $0xd800, %esi           # imm = 0xD800
shll $0xa, %esi
movl -0x44(%rbp), %eax
subl $0xdc00, %eax           # imm = 0xDC00
orl %eax, %esi
addl $0x10000, %esi          # imm = 0x10000
callq 0x1714c0
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
jmp 0x171d58
movq -0x8(%rbp), %rdi
movl $0xfffd, %esi           # imm = 0xFFFD
callq 0x1714c0
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x1714c0
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
jmp 0x171d5a
jmp 0x171d5c
jmp 0x171d5e
jmp 0x171d60
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x171bbf
movq -0x28(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpl $0x0, %eax
je 0x171d9f
movl -0x1c(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
jne 0x171d9f
movq -0x8(%rbp), %rdi
movl $0xfffd, %esi           # imm = 0xFFFD
callq 0x1714c0
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)