#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int u_int32_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ calloc(int, int);
int count_u8_caracs(char const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int u8_seqlen(char const *);
u_int32_t *u8_convert_to_u8str(const char *str) {
  int str_nb_u8_chars = count_u8_caracs(str);
  u_int32_t *p_u8_chars;
  if (NULL == (p_u8_chars = (u_int32_t *)calloc(str_nb_u8_chars + 1,
                                                sizeof(u_int32_t)))) {
    fprintf(stderr,
            "ch_to_u8() failed - cannot allocate memory to initialize a new "
            "array of u8 chars."
            " - %s:%d\n",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/msoula/romhacking/refs/heads/master/"
            "hacks/knt/src/utils/spc_utf8.c",
            95);
    return NULL;
  }
  int u8_pos = 0;
  const char *ch_pos = str;
  const char *ch_end = &str[strlen(str)];
  while (ch_pos != ch_end) {
    u_int32_t u8_char;
    int nb_bytes = u8_seqlen(ch_pos);
    switch (nb_bytes) {
    case 1:
      u8_char = (ch_pos[0] & 0xFF);
      break;
    case 2:
      u8_char = ((ch_pos[0] & 0xFF) * 0x0100) + (ch_pos[1] & 0xFF);
      break;
    case 3:
      u8_char = ((ch_pos[0] & 0xFF) * 0x100 * 0x100) +
                ((ch_pos[1] & 0xFF) * 0x100) + (ch_pos[2] & 0xFF);
      u8_char &= 0x00FFFFFF;
      break;
    case 4:
      u8_char = ((ch_pos[0] & 0xFF) * 0x100 * 0x100 * 0x100) +
                ((ch_pos[1] & 0xFF) * 0x100 * 0x100) +
                ((ch_pos[2] & 0xFF) * 0x100) + (ch_pos[3] & 0xFF);
      u8_char &= 0xFFFFFFFF;
      break;
    default:
      fprintf(stderr,
              "ch_to_u8() failed - invalid character %c."
              " - %s:%d\n",
              ch_pos[0],
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/msoula/romhacking/refs/heads/master/"
              "hacks/knt/src/utils/spc_utf8.c",
              121);
      exit(EXIT_FAILURE);
      break;
    }
    p_u8_chars[u8_pos++] = u8_char;
    ch_pos += nb_bytes;
  }
  p_u8_chars[u8_pos] = '\0';
  return p_u8_chars;
}


x86 assembly (clang, -O0):
<u8_convert_to_u8str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <count_u8_caracs@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11ca <u8_convert_to_u8str+0x6a>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
lea    0xeba(%rip),%rdx
mov    $0x5f,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    139b <u8_convert_to_u8str+0x23b>
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     1384 <u8_convert_to_u8str+0x224>
mov    -0x30(%rbp),%rdi
call   1050 <u8_seqlen@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x50(%rbp)
sub    $0x3,%eax
ja     131d <u8_convert_to_u8str+0x1bd>
mov    -0x50(%rbp),%rax
lea    0xdce(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
mov    %eax,-0x3c(%rbp)
jmp    1356 <u8_convert_to_u8str+0x1f6>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
shl    $0x8,%eax
mov    -0x30(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
and    $0xff,%ecx
add    %ecx,%eax
mov    %eax,-0x3c(%rbp)
jmp    1356 <u8_convert_to_u8str+0x1f6>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
shl    $0x8,%eax
shl    $0x8,%eax
mov    -0x30(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
and    $0xff,%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    -0x30(%rbp),%rcx
movsbl 0x2(%rcx),%ecx
and    $0xff,%ecx
add    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
and    $0xffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    1356 <u8_convert_to_u8str+0x1f6>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
shl    $0x8,%eax
shl    $0x8,%eax
shl    $0x8,%eax
mov    -0x30(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
and    $0xff,%ecx
shl    $0x8,%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    -0x30(%rbp),%rcx
movsbl 0x2(%rcx),%ecx
and    $0xff,%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    -0x30(%rbp),%rcx
movsbl 0x3(%rcx),%ecx
and    $0xff,%ecx
add    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
and    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    1356 <u8_convert_to_u8str+0x1f6>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rax
movsbl (%rax),%edx
lea    0xdd4(%rip),%rsi
lea    0xd30(%rip),%rcx
mov    $0x79,%r8d
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x3c(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x40(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    11f8 <u8_convert_to_u8str+0x98>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u8_convert_to_u8str>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
call   1070 <count_u8_caracs@plt>
lea    0x1(%rax),%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     121f <u8_convert_to_u8str+0xbf>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     1247 <u8_convert_to_u8str+0xe7>
mov    %rax,%r15
add    %rbx,%r15
xor    %r12d,%r12d
lea    0xe55(%rip),%r13
jmp    11d7 <u8_convert_to_u8str+0x77>
nopl   (%rax)
movzbl (%rbx),%ecx
shl    $0x10,%ecx
movzbl 0x1(%rbx),%edx
shl    $0x8,%edx
or     %ecx,%edx
movzbl 0x2(%rbx),%ecx
or     %edx,%ecx
mov    %ecx,(%r14,%r12,4)
add    $0x1,%r12
mov    %eax,%eax
add    %rax,%rbx
cmp    %r15,%rbx
je     121a <u8_convert_to_u8str+0xba>
mov    %rbx,%rdi
call   1050 <u8_seqlen@plt>
lea    -0x1(%rax),%ecx
cmp    $0x3,%ecx
ja     125e <u8_convert_to_u8str+0xfe>
movslq 0x0(%r13,%rcx,4),%rcx
add    %r13,%rcx
jmp    *%rcx
movzbl (%rbx),%ecx
jmp    11c5 <u8_convert_to_u8str+0x65>
cs nopw 0x0(%rax,%rax,1)
mov    (%rbx),%ecx
jmp    1216 <u8_convert_to_u8str+0xb6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzwl (%rbx),%ecx
shl    $0x10,%ecx
bswap  %ecx
jmp    11c5 <u8_convert_to_u8str+0x65>
mov    %r12d,%eax
jmp    1249 <u8_convert_to_u8str+0xe9>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
lea    0xe35(%rip),%rdx
xor    %r14d,%r14d
mov    $0x5f,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1251 <u8_convert_to_u8str+0xf1>
xor    %eax,%eax
movl   $0x0,(%r14,%rax,4)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
movsbl (%rbx),%edx
lea    0xe97(%rip),%rsi
lea    0xdf3(%rip),%rcx
mov    $0x79,%r8d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [91 x i8] c"ch_to_u8() failed - cannot allocate memory to initialize a new array of u8 chars. - %s:%d\0A\00"
@.str.1 = [157 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/msoula/romhacking/refs/heads/master/hacks/knt/src/utils/spc_utf8.c\00"
@.str.2 = [51 x i8] c"ch_to_u8() failed - invalid character %c. - %s:%d\0A\00"
@EXIT_FAILURE = global i32 0
define i32* @u8_convert_to_u8str(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i32 @count_u8_caracs(i8* %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
%15 = call i64 @calloc(i32 %14, i32 4)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %5
%17 = icmp eq i32* null, %16
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str.1, i64 0, i64 0), i32 95)
store i32* null, i32** %2
br label %135
21: ; preds = %1
store i32 0, i32* %6
%22 = load i8*, i8** %3
store i8* %22, i8** %7
%23 = load i8*, i8** %3
%24 = load i8*, i8** %3
%25 = call i64 @strlen(i8* %24)
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8* %26, i8** %8
br label %27
27: ; preds = %118, %21
%28 = load i8*, i8** %7
%29 = load i8*, i8** %8
%30 = icmp ne i8* %28, %29
br i1 %30, label %31, label %129
31: ; preds = %27
%32 = load i8*, i8** %7
%33 = call i32 @u8_seqlen(i8* %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
switch i32 %34, label %109 [
i32 1, label %35
i32 2, label %41
i32 3, label %54
i32 4, label %77
]
35: ; preds = %31
%36 = load i8*, i8** %7
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = and i32 %39, 255
store i32 %40, i32* %9
br label %118
41: ; preds = %31
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = and i32 %45, 255
%47 = mul nsw i32 %46, 256
%48 = load i8*, i8** %7
%49 = getelementptr inbounds i8, i8* %48, i64 1
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = and i32 %51, 255
%53 = add nsw i32 %47, %52
store i32 %53, i32* %9
br label %118
54: ; preds = %31
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i64 0
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = and i32 %58, 255
%60 = mul nsw i32 %59, 256
%61 = mul nsw i32 %60, 256
%62 = load i8*, i8** %7
%63 = getelementptr inbounds i8, i8* %62, i64 1
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = and i32 %65, 255
%67 = mul nsw i32 %66, 256
%68 = add nsw i32 %61, %67
%69 = load i8*, i8** %7
%70 = getelementptr inbounds i8, i8* %69, i64 2
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = and i32 %72, 255
%74 = add nsw i32 %68, %73
store i32 %74, i32* %9
%75 = load i32, i32* %9
%76 = and i32 %75, 16777215
store i32 %76, i32* %9
br label %118
77: ; preds = %31
%78 = load i8*, i8** %7
%79 = getelementptr inbounds i8, i8* %78, i64 0
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = and i32 %81, 255
%83 = mul nsw i32 %82, 256
%84 = mul nsw i32 %83, 256
%85 = mul nsw i32 %84, 256
%86 = load i8*, i8** %7
%87 = getelementptr inbounds i8, i8* %86, i64 1
%88 = load i8, i8* %87
%89 = sext i8 %88 to i32
%90 = and i32 %89, 255
%91 = mul nsw i32 %90, 256
%92 = mul nsw i32 %91, 256
%93 = add nsw i32 %85, %92
%94 = load i8*, i8** %7
%95 = getelementptr inbounds i8, i8* %94, i64 2
%96 = load i8, i8* %95
%97 = sext i8 %96 to i32
%98 = and i32 %97, 255
%99 = mul nsw i32 %98, 256
%100 = add nsw i32 %93, %99
%101 = load i8*, i8** %7
%102 = getelementptr inbounds i8, i8* %101, i64 3
%103 = load i8, i8* %102
%104 = sext i8 %103 to i32
%105 = and i32 %104, 255
%106 = add nsw i32 %100, %105
store i32 %106, i32* %9
%107 = load i32, i32* %9
%108 = and i32 %107, -1
store i32 %108, i32* %9
br label %118
109: ; preds = %31
%110 = load i32, i32* @stderr
%111 = load i8*, i8** %7
%112 = getelementptr inbounds i8, i8* %111, i64 0
%113 = load i8, i8* %112
%114 = sext i8 %113 to i32
%115 = call i32 (i32, i8*, ...) @fprintf(i32 %110, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %114, i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str.1, i64 0, i64 0), i32 121)
%116 = load i32, i32* @EXIT_FAILURE
%117 = call i32 @exit(i32 %116)
unreachable
118: ; preds = %77, %54, %41, %35
%119 = load i32, i32* %9
%120 = load i32*, i32** %5
%121 = load i32, i32* %6
%122 = add nsw i32 %121, 1
store i32 %122, i32* %6
%123 = sext i32 %121 to i64
%124 = getelementptr inbounds i32, i32* %120, i64 %123
store i32 %119, i32* %124
%125 = load i32, i32* %10
%126 = load i8*, i8** %7
%127 = sext i32 %125 to i64
%128 = getelementptr inbounds i8, i8* %126, i64 %127
store i8* %128, i8** %7
br label %27
129: ; preds = %27
%130 = load i32*, i32** %5
%131 = load i32, i32* %6
%132 = sext i32 %131 to i64
%133 = getelementptr inbounds i32, i32* %130, i64 %132
store i32 0, i32* %133
%134 = load i32*, i32** %5
store i32* %134, i32** %2
br label %135
135: ; preds = %129, %18
%136 = load i32*, i32** %2
ret i32* %136
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [91 x i8] c"ch_to_u8() failed - cannot allocate memory to initialize a new array of u8 chars. - %s:%d\0A\00"
@.str.1 = [157 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/msoula/romhacking/refs/heads/master/hacks/knt/src/utils/spc_utf8.c\00"
@.str.2 = [51 x i8] c"ch_to_u8() failed - invalid character %c. - %s:%d\0A\00"
@EXIT_FAILURE = global i32 0
define i32* @u8_convert_to_u8str(i8* %0) {
%2 = tail call i32 @count_u8_caracs(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i64 @calloc(i32 %3, i32 4)
%5 = inttoptr i64 %4 to i32*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str.1, i64 0, i64 0), i32 95)
br label %79
10: ; preds = %1
%11 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = icmp eq i64 %11, 0
br i1 %13, label %76, label %14
14: ; preds = %10, %67
%15 = phi i64 [ %69, %67 ], [ 0, %10 ]
%16 = phi i8* [ %72, %67 ], [ %0, %10 ]
%17 = tail call i32 @u8_seqlen(i8* %16)
switch i32 %17, label %60 [
i32 1, label %18
i32 2, label %21
i32 3, label %29
i32 4, label %42
]
18: ; preds = %14
%19 = load i8, i8* %16
%20 = zext i8 %19 to i32
br label %67
21: ; preds = %14
%22 = load i8, i8* %16
%23 = zext i8 %22 to i32
%24 = shl nuw nsw i32 %23, 8
%25 = getelementptr inbounds i8, i8* %16, i64 1
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = or i32 %24, %27
br label %67
29: ; preds = %14
%30 = load i8, i8* %16
%31 = zext i8 %30 to i32
%32 = shl nuw nsw i32 %31, 16
%33 = getelementptr inbounds i8, i8* %16, i64 1
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = shl nuw nsw i32 %35, 8
%37 = or i32 %36, %32
%38 = getelementptr inbounds i8, i8* %16, i64 2
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = or i32 %37, %40
br label %67
42: ; preds = %14
%43 = load i8, i8* %16
%44 = zext i8 %43 to i32
%45 = shl nuw nsw i32 %44, 24
%46 = getelementptr inbounds i8, i8* %16, i64 1
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = shl nuw nsw i32 %48, 16
%50 = or i32 %49, %45
%51 = getelementptr inbounds i8, i8* %16, i64 2
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = shl nuw nsw i32 %53, 8
%55 = or i32 %50, %54
%56 = getelementptr inbounds i8, i8* %16, i64 3
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = or i32 %55, %58
br label %67
60: ; preds = %14
%61 = load i32, i32* @stderr
%62 = load i8, i8* %16
%63 = sext i8 %62 to i32
%64 = tail call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %63, i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str.1, i64 0, i64 0), i32 121)
%65 = load i32, i32* @EXIT_FAILURE
%66 = tail call i32 @exit(i32 %65)
unreachable
67: ; preds = %42, %29, %21, %18
%68 = phi i32 [ %59, %42 ], [ %41, %29 ], [ %28, %21 ], [ %20, %18 ]
%69 = add nuw i64 %15, 1
%70 = getelementptr inbounds i32, i32* %5, i64 %15
store i32 %68, i32* %70
%71 = zext i32 %17 to i64
%72 = getelementptr inbounds i8, i8* %16, i64 %71
%73 = icmp eq i8* %72, %12
br i1 %73, label %74, label %14
74: ; preds = %67
%75 = and i64 %69, 4294967295
br label %76
76: ; preds = %74, %10
%77 = phi i64 [ 0, %10 ], [ %75, %74 ]
%78 = getelementptr inbounds i32, i32* %5, i64 %77
store i32 0, i32* %78
br label %79
79: ; preds = %76, %7
%80 = phi i32* [ null, %7 ], [ %5, %76 ]
ret i32* %80
}