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

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

		if(n>=0x80 && n<=0xbf) { // continuation byte
			if(UTF8_NBYTES_EXPECTED==0) {
				handle_invalid_byte(s, n);
			}
			else { // valid continuation byte
				es->buf[UTF8_NBYTES_SAVED] = n;
				UTF8_NBYTES_SAVED++;
				UTF8_NBYTES_EXPECTED--;
				if(UTF8_NBYTES_EXPECTED==0) {
					ucstring_append_char(s,
						decode_utf8_sequence(es->buf, (UI)UTF8_NBYTES_SAVED));
				}
			}
		}
		else { // not a continuation byte
			// Should not be any pending bytes. If there are, they're invalid.
			if(UTF8_NBYTES_EXPECTED != 0) {
				handle_invalid_bytes(s, es->buf, (UI)UTF8_NBYTES_SAVED);
				UTF8_NBYTES_EXPECTED = 0;
			}

			if(n<=0x7f) {
				ucstring_append_char(s, (i32)n);
				UTF8_NBYTES_EXPECTED = 0; // number of additional continuation bytes expected
			}
			else if(n<=0xdf) { // 2-byte UTF-8 char
				es->buf[0] = n; // save this byte for later
				UTF8_NBYTES_SAVED = 1; // number of bytes saved in buf[0]...buf[3]
				UTF8_NBYTES_EXPECTED = 1; // 1 more byte expected in this sequence
			}
			else if(n<=0xef) { // 3-byte UTF-8 char
				es->buf[0] = n;
				UTF8_NBYTES_SAVED = 1;
				UTF8_NBYTES_EXPECTED = 2;
			}
			else if(n<=0xf7) { // 4-byte UTF-8 char
				es->buf[0] = n;
				UTF8_NBYTES_SAVED = 1;
				UTF8_NBYTES_EXPECTED = 3;
			}
			else {
				handle_invalid_byte(s, n);
				UTF8_NBYTES_EXPECTED = 0;
			}
		}
	}

	// Check if there are unprocessed bytes at the end of the string, when there
	// shouldn't be.
	if(UTF8_NBYTES_EXPECTED!=0 && !(conv_flags & DE_CONVFLAG_PARTIAL_DATA)) {
		handle_invalid_bytes(s, es->buf, (UI)UTF8_NBYTES_SAVED);
		UTF8_NBYTES_EXPECTED = 0;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x171b48
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %eax
cmpl $0x80, %eax
jl 0x171a52
movzbl -0x31(%rbp), %eax
cmpl $0xbf, %eax
jg 0x171a52
movq -0x28(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpl $0x0, %eax
jne 0x1719e9
movq -0x8(%rbp), %rdi
movzbl -0x31(%rbp), %esi
callq 0x171f70
jmp 0x171a4d
movb -0x31(%rbp), %dl
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rcx
movzbl 0x1e(%rcx), %ecx
movb %dl, 0x18(%rax,%rcx)
movq -0x28(%rbp), %rax
movb 0x1e(%rax), %cl
addb $0x1, %cl
movb %cl, 0x1e(%rax)
movq -0x28(%rbp), %rax
movb 0x1f(%rax), %cl
addb $-0x1, %cl
movb %cl, 0x1f(%rax)
movq -0x28(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpl $0x0, %eax
jne 0x171a4b
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
movq -0x28(%rbp), %rax
movzbl 0x1e(%rax), %esi
callq 0x172af0
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x1714c0
jmp 0x171a4d
jmp 0x171b35
movq -0x28(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpl $0x0, %eax
je 0x171a80
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x18, %rsi
movq -0x28(%rbp), %rax
movzbl 0x1e(%rax), %edx
callq 0x172c10
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
movzbl -0x31(%rbp), %eax
cmpl $0x7f, %eax
jg 0x171aa3
movq -0x8(%rbp), %rdi
movzbl -0x31(%rbp), %esi
callq 0x1714c0
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
jmp 0x171b33
movzbl -0x31(%rbp), %eax
cmpl $0xdf, %eax
jg 0x171aca
movb -0x31(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x18(%rax)
movq -0x28(%rbp), %rax
movb $0x1, 0x1e(%rax)
movq -0x28(%rbp), %rax
movb $0x1, 0x1f(%rax)
jmp 0x171b31
movzbl -0x31(%rbp), %eax
cmpl $0xef, %eax
jg 0x171af1
movb -0x31(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x18(%rax)
movq -0x28(%rbp), %rax
movb $0x1, 0x1e(%rax)
movq -0x28(%rbp), %rax
movb $0x2, 0x1f(%rax)
jmp 0x171b2f
movzbl -0x31(%rbp), %eax
cmpl $0xf7, %eax
jg 0x171b18
movb -0x31(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x18(%rax)
movq -0x28(%rbp), %rax
movb $0x1, 0x1e(%rax)
movq -0x28(%rbp), %rax
movb $0x3, 0x1f(%rax)
jmp 0x171b2d
movq -0x8(%rbp), %rdi
movzbl -0x31(%rbp), %esi
callq 0x171f70
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
jmp 0x171b2f
jmp 0x171b31
jmp 0x171b33
jmp 0x171b35
jmp 0x171b37
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x171993
movq -0x28(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpl $0x0, %eax
je 0x171b81
movl -0x1c(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
jne 0x171b81
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x18, %rsi
movq -0x28(%rbp), %rax
movzbl 0x1e(%rax), %edx
callq 0x172c10
movq -0x28(%rbp), %rax
movb $0x0, 0x1f(%rax)
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)