#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*/ reverse_utf8_seq(char *, int);
int utf8_following_bytes(char);
void fix_backwards_sequences(char *string) {
  for (char *p = string; *p != '\0'; p++) {
    int bytes = utf8_following_bytes(*p);
    if (bytes < 1) {
      continue;
    }
    reverse_utf8_seq(p - bytes, bytes + 1);
  }
}


x86 assembly (clang, -O0):
<fix_backwards_sequences>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     118e <fix_backwards_sequences+0x6e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <utf8_following_bytes@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    1162 <fix_backwards_sequences+0x42>
jmp    117d <fix_backwards_sequences+0x5d>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1040 <reverse_utf8_seq@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1134 <fix_backwards_sequences+0x14>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fix_backwards_sequences>:
push   %rbx
mov    (%rdi),%al
test   %al,%al
je     115c <fix_backwards_sequences+0x3c>
mov    %rdi,%rbx
jmp    113c <fix_backwards_sequences+0x1c>
nopl   0x0(%rax)
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     115c <fix_backwards_sequences+0x3c>
movsbl %al,%edi
call   1030 <utf8_following_bytes@plt>
test   %eax,%eax
jle    1130 <fix_backwards_sequences+0x10>
mov    %eax,%ecx
mov    %rbx,%rdi
sub    %rcx,%rdi
add    $0x1,%eax
mov    %eax,%esi
call   1040 <reverse_utf8_seq@plt>
jmp    1130 <fix_backwards_sequences+0x10>
pop    %rbx
ret

LLVM IR (-O0):
define void @fix_backwards_sequences(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %3
br label %6
6: ; preds = %27, %1
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %6
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
%14 = call i32 @utf8_following_bytes(i8 signext %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %11
br label %27
18: ; preds = %11
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = sub i64 0, %21
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
%26 = call i32 @reverse_utf8_seq(i8* %23, i32 %25)
br label %27
27: ; preds = %18, %17
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %3
br label %6
30: ; preds = %6
ret void
}

LLVM IR (-O2):
define void @fix_backwards_sequences(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %4, label %5
4: ; preds = %16, %1
ret void
5: ; preds = %1, %16
%6 = phi i8 [ %18, %16 ], [ %2, %1 ]
%7 = phi i8* [ %17, %16 ], [ %0, %1 ]
%8 = tail call i32 @utf8_following_bytes(i8 signext %6)
%9 = icmp slt i32 %8, 1
br i1 %9, label %16, label %10
10: ; preds = %5
%11 = zext i32 %8 to i64
%12 = sub nsw i64 0, %11
%13 = getelementptr inbounds i8, i8* %7, i64 %12
%14 = add nuw nsw i32 %8, 1
%15 = tail call i32 @reverse_utf8_seq(i8* %13, i32 %14)
br label %16
16: ; preds = %5, %10
%17 = getelementptr inbounds i8, i8* %7, i64 1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %4, label %5
}