#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const, char *, int);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int u8_seqlen(char const *);
size_t count_u8_caracs(const char *s) {
  int nb_bytes;
  size_t length = 0;
  const char *end = &(s[strlen(s)]);
  const char *current = s;
  while (current < end) {
    nb_bytes = u8_seqlen(current);
    if (nb_bytes < 0 || nb_bytes > 4) {
      fprintf(stderr,
              "count_u8_caracs() failed - invalid character %c."
              " - %s:%d\n",
              current[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",
              25);
      exit(EXIT_FAILURE);
    }
    current += nb_bytes;
    length++;
  }
  return length;
}


x86 assembly (clang, -O0):
<count_u8_caracs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1202 <count_u8_caracs+0xc2>
mov    -0x28(%rbp),%rdi
call   1040 <u8_seqlen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11a9 <count_u8_caracs+0x69>
cmpl   $0x4,-0xc(%rbp)
jle    11e0 <count_u8_caracs+0xa0>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
lea    0xe43(%rip),%rsi
lea    0xe76(%rip),%rcx
mov    $0x19,%r8d
movsbl (%rax),%edx
call   1050 <fprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    117b <count_u8_caracs+0x3b>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_u8_caracs>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
jle    117d <count_u8_caracs+0x3d>
mov    %rax,%r14
add    %rbx,%r14
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <u8_seqlen@plt>
cmp    $0x5,%eax
jae    1189 <count_u8_caracs+0x49>
mov    %eax,%eax
add    %rax,%rbx
add    $0x1,%r15
cmp    %r14,%rbx
jb     1160 <count_u8_caracs+0x20>
jmp    1180 <count_u8_caracs+0x40>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
movsbl (%rbx),%edx
lea    0xe64(%rip),%rsi
lea    0xe97(%rip),%rcx
mov    $0x19,%r8d
call   1050 <fprintf@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [58 x i8] c"count_u8_caracs() failed - invalid character %c. - %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"
@EXIT_FAILURE = global i32 0
define i64 @count_u8_caracs(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* %4
%7 = load i8*, i8** %2
%8 = load i8*, i8** %2
%9 = call i64 @strlen(i8* %8)
%10 = getelementptr inbounds i8, i8* %7, i64 %9
store i8* %10, i8** %5
%11 = load i8*, i8** %2
store i8* %11, i8** %6
br label %12
12: ; preds = %32, %1
%13 = load i8*, i8** %6
%14 = load i8*, i8** %5
%15 = icmp ult i8* %13, %14
br i1 %15, label %16, label %39
16: ; preds = %12
%17 = load i8*, i8** %6
%18 = call i32 @u8_seqlen(i8* %17)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* %3
%23 = icmp sgt i32 %22, 4
br i1 %23, label %24, label %32
24: ; preds = %21, %16
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8 signext %28, i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str.1, i64 0, i64 0), i32 25)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %21
%33 = load i32, i32* %3
%34 = load i8*, i8** %6
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %6
%37 = load i64, i64* %4
%38 = add i64 %37, 1
store i64 %38, i64* %4
br label %12
39: ; preds = %12
%40 = load i64, i64* %4
ret i64 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [58 x i8] c"count_u8_caracs() failed - invalid character %c. - %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"
@EXIT_FAILURE = global i32 0
define i64 @count_u8_caracs(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = getelementptr inbounds i8, i8* %0, i64 %2
%4 = icmp sgt i64 %2, 0
br i1 %4, label %5, label %21
5: ; preds = %1, %16
%6 = phi i8* [ %18, %16 ], [ %0, %1 ]
%7 = phi i64 [ %19, %16 ], [ 0, %1 ]
%8 = tail call i32 @u8_seqlen(i8* %6)
%9 = icmp ugt i32 %8, 4
br i1 %9, label %10, label %16
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = load i8, i8* %6
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8 signext %12, i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str.1, i64 0, i64 0), i32 25)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %5
%17 = zext i32 %8 to i64
%18 = getelementptr inbounds i8, i8* %6, i64 %17
%19 = add i64 %7, 1
%20 = icmp ult i8* %18, %3
br i1 %20, label %5, label %21
21: ; preds = %16, %1
%22 = phi i64 [ 0, %1 ], [ %19, %16 ]
ret i64 %22
}