// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_utils.c
// Function: utf8_encodeValue
int utf8_encodeValue(int value, uint8_t* bytes) {

  if (value <= 0x7f) {
    *bytes = value & 0x7f;
    return 1;
  }

  // 2 byte character 110xxxxx 10xxxxxx -> last 6 bits write to 2nd byte and
  // first 5 bit write to first byte
  if (value <= 0x7ff) {
    *(bytes++) = (uint8_t)(0b11000000 | ((value & 0b11111000000) >> 6));
    *(bytes) = (uint8_t)(0b10000000 | ((value & 111111)));
    return 2;
  }

  // 3 byte character 1110xxxx 10xxxxxx 10xxxxxx -> from last, 6 bits write
  // to  3rd byte, next 6 bits write to 2nd byte, and 4 bits to first byte.
  if (value <= 0xffff) {
    *(bytes++) = (uint8_t)(0b11100000 | ((value & 0b1111000000000000) >> 12));
    *(bytes++) = (uint8_t)(0b10000000 | ((value & 0b111111000000) >> 6));
    *(bytes) =   (uint8_t)(0b10000000 | ((value & 0b111111)));
    return 3;
  }

  // 4 byte character 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx -> last 6 bits to
  // to 4th byte, next 6 bits to 3rd byte, next 6 bits to 2nd byte, 3 bits
  // first byte.
  if (value <= 0x10ffff) {
    *(bytes++) = (uint8_t)(0b11110000 | ((value & (0b111    << 18)) >> 18));
    *(bytes++) = (uint8_t)(0b10000000 | ((value & (0b111111 << 12)) >> 12));
    *(bytes++) = (uint8_t)(0b10000000 | ((value & (0b111111 << 6))  >> 6));
    *(bytes)   = (uint8_t)(0b10000000 | ((value &  0b111111)));
    return 4;
  }

  return 0;
}

// Assembly:
movl %edi, -0x8(%rsp)
movq %rsi, -0x10(%rsp)
cmpl $0x7f, -0x8(%rsp)
jg 0x1423d
movl -0x8(%rsp), %eax
andl $0x7f, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movb %cl, (%rax)
movl $0x1, -0x4(%rsp)
jmp 0x143b0
cmpl $0x7ff, -0x8(%rsp)      # imm = 0x7FF
jg 0x14291
movl -0x8(%rsp), %eax
andl $0x7c0, %eax            # imm = 0x7C0
sarl $0x6, %eax
orl $0xc0, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x10(%rsp)
movb %cl, (%rax)
movl -0x8(%rsp), %eax
andl $0x1b207, %eax          # imm = 0x1B207
orl $0x80, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movb %cl, (%rax)
movl $0x2, -0x4(%rsp)
jmp 0x143b0
cmpl $0xffff, -0x8(%rsp)     # imm = 0xFFFF
jg 0x14309
movl -0x8(%rsp), %eax
andl $0xf000, %eax           # imm = 0xF000
sarl $0xc, %eax
orl $0xe0, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x10(%rsp)
movb %cl, (%rax)
movl -0x8(%rsp), %eax
andl $0xfc0, %eax            # imm = 0xFC0
sarl $0x6, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x10(%rsp)
movb %cl, (%rax)
movl -0x8(%rsp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movb %cl, (%rax)
movl $0x3, -0x4(%rsp)
jmp 0x143b0
cmpl $0x10ffff, -0x8(%rsp)   # imm = 0x10FFFF
jg 0x143a8
movl -0x8(%rsp), %eax
andl $0x1c0000, %eax         # imm = 0x1C0000
sarl $0x12, %eax
orl $0xf0, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x10(%rsp)
movb %cl, (%rax)
movl -0x8(%rsp), %eax
andl $0x3f000, %eax          # imm = 0x3F000
sarl $0xc, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x10(%rsp)
movb %cl, (%rax)
movl -0x8(%rsp), %eax
andl $0xfc0, %eax            # imm = 0xFC0
sarl $0x6, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x10(%rsp)
movb %cl, (%rax)
movl -0x8(%rsp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x10(%rsp), %rax
movb %cl, (%rax)
movl $0x4, -0x4(%rsp)
jmp 0x143b0
movl $0x0, -0x4(%rsp)
movl -0x4(%rsp), %eax
retq
nopw %cs:(%rax,%rax)