#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 */
size_t strlen(char *);
void fix_truncated_utf8(char *str) {
  size_t len = strlen(str);
  if (len < 3) {
    return;
  }
  char *b = str + len - 3;
  if ((b[2] & 0x80) == 0) {
    return;
  }
  if (b[2] & 0x40) {
    b[2] = 0;
  } else if ((b[1] & 0xe0) == 0xe0) {
    b[1] = 0;
  } else if ((b[0] & 0xf0) == 0xf0) {
    b[0] = 0;
  }
}


x86 assembly (clang, -O0):
<fix_truncated_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x3,-0x10(%rbp)
jae    1139 <fix_truncated_utf8+0x29>
jmp    11d2 <fix_truncated_utf8+0xc2>
mov    -0x8(%rbp),%rax
add    -0x10(%rbp),%rax
add    $0xfffffffffffffffd,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl 0x2(%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1164 <fix_truncated_utf8+0x54>
jmp    11d2 <fix_truncated_utf8+0xc2>
mov    -0x18(%rbp),%rax
movsbl 0x2(%rax),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     1185 <fix_truncated_utf8+0x75>
mov    -0x18(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    11d2 <fix_truncated_utf8+0xc2>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
and    $0xe0,%eax
cmp    $0xe0,%eax
jne    11aa <fix_truncated_utf8+0x9a>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11cd <fix_truncated_utf8+0xbd>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
and    $0xf0,%eax
cmp    $0xf0,%eax
jne    11c8 <fix_truncated_utf8+0xb8>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11cd <fix_truncated_utf8+0xbd>
jmp    11d2 <fix_truncated_utf8+0xc2>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fix_truncated_utf8>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0x3,%rax
jb     1152 <fix_truncated_utf8+0x42>
movzbl -0x1(%rbx,%rax,1),%ecx
test   $0x80,%cl
je     1152 <fix_truncated_utf8+0x42>
test   $0x40,%cl
jne    1141 <fix_truncated_utf8+0x31>
add    %rbx,%rax
add    $0xfffffffffffffffd,%rax
cmpb   $0xdf,0x1(%rax)
jbe    114a <fix_truncated_utf8+0x3a>
add    $0x1,%rax
jmp    114f <fix_truncated_utf8+0x3f>
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
jmp    114f <fix_truncated_utf8+0x3f>
cmpb   $0xf0,(%rax)
jb     1152 <fix_truncated_utf8+0x42>
movb   $0x0,(%rax)
pop    %rbx
ret

LLVM IR (-O0):
define void @fix_truncated_utf8(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = icmp ult i64 %7, 3
br i1 %8, label %9, label %10
9: ; preds = %1
br label %54
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = load i64, i64* %3
%13 = getelementptr inbounds i8, i8* %11, i64 %12
%14 = getelementptr inbounds i8, i8* %13, i64 -3
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i64 2
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = and i32 %18, 128
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %10
br label %54
22: ; preds = %10
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i64 2
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = and i32 %26, 64
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i64 2
store i8 0, i8* %31
br label %54
32: ; preds = %22
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = and i32 %36, 224
%38 = icmp eq i32 %37, 224
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i64 1
store i8 0, i8* %41
br label %53
42: ; preds = %32
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = and i32 %46, 240
%48 = icmp eq i32 %47, 240
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = load i8*, i8** %4
%51 = getelementptr inbounds i8, i8* %50, i64 0
store i8 0, i8* %51
br label %52
52: ; preds = %49, %42
br label %53
53: ; preds = %52, %39
br label %54
54: ; preds = %9, %21, %53, %29
ret void
}

LLVM IR (-O2):
define void @fix_truncated_utf8(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp ult i64 %2, 3
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 %2
%6 = getelementptr inbounds i8, i8* %5, i64 -3
%7 = getelementptr inbounds i8, i8* %6, i64 2
%8 = load i8, i8* %7
%9 = zext i8 %8 to i32
%10 = and i32 %9, 128
%11 = icmp eq i32 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %4
%13 = and i32 %9, 64
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %6, i64 1
%17 = load i8, i8* %16
%18 = icmp ugt i8 %17, -33
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i8, i8* %6
%21 = icmp ugt i8 %20, -17
br i1 %21, label %22, label %24
22: ; preds = %19, %15, %12
%23 = phi i8* [ %7, %12 ], [ %16, %15 ], [ %6, %19 ]
store i8 0, i8* %23
br label %24
24: ; preds = %22, %4, %19, %1
ret void
}