#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*/ assert(int /*<<< orphan*/);
char *cbstrcututf(char *str, int num) {
  unsigned char *wp;
  int cnt;
  assert(str && num >= 0);
  wp = (unsigned char *)str;
  cnt = 0;
  while (*wp != '\0') {
    if ((*wp & 0x80) == 0x00 || (*wp & 0xe0) == 0xc0 || (*wp & 0xf0) == 0xe0 ||
        (*wp & 0xf8) == 0xf0) {
      cnt++;
      if (cnt > num) {
        *wp = '\0';
        break;
      }
    }
    wp++;
  }
  return str;
}


x86 assembly (clang, -O0):
<cbstrcututf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x1d(%rbp)
je     1139 <cbstrcututf+0x29>
cmpl   $0x0,-0xc(%rbp)
setge  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     11f6 <cbstrcututf+0xe6>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11bf <cbstrcututf+0xaf>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
je     11bf <cbstrcututf+0xaf>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
je     11bf <cbstrcututf+0xaf>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    11e5 <cbstrcututf+0xd5>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    11e0 <cbstrcututf+0xd0>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11f6 <cbstrcututf+0xe6>
jmp    11e5 <cbstrcututf+0xd5>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1155 <cbstrcututf+0x45>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbstrcututf>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
test   %rdi,%rdi
setne  %al
test   %esi,%esi
setns  %cl
and    %al,%cl
movzbl %cl,%edi
call   1030 <assert@plt>
mov    (%rbx),%dl
test   %dl,%dl
je     1183 <cbstrcututf+0x73>
lea    0x1(%rbx),%rax
xor    %ecx,%ecx
movdqa 0xebf(%rip),%xmm0
movdqa 0xec7(%rip),%xmm1
jmp    115b <cbstrcututf+0x4b>
nopl   0x0(%rax,%rax,1)
movzbl (%rax),%edx
add    $0x1,%rax
test   %dl,%dl
je     1183 <cbstrcututf+0x73>
movzbl %dl,%edx
movd   %edx,%xmm2
pshufd $0x0,%xmm2,%xmm2
pand   %xmm0,%xmm2
pcmpeqd %xmm1,%xmm2
movmskps %xmm2,%edx
test   %edx,%edx
je     1150 <cbstrcututf+0x40>
cmp    %ebp,%ecx
jge    117f <cbstrcututf+0x6f>
add    $0x1,%ecx
jmp    1150 <cbstrcututf+0x40>
movb   $0x0,-0x1(%rax)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i8* @cbstrcututf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 0
br label %12
12: ; preds = %9, %2
%13 = phi i1 [ false, %2 ], [ %11, %9 ]
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %3
store i8* %16, i8** %5
store i32 0, i32* %6
br label %17
17: ; preds = %55, %12
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %58
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = and i32 %25, 128
%27 = icmp eq i32 %26, 0
br i1 %27, label %46, label %28
28: ; preds = %22
%29 = load i8*, i8** %5
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = and i32 %31, 224
%33 = icmp eq i32 %32, 192
br i1 %33, label %46, label %34
34: ; preds = %28
%35 = load i8*, i8** %5
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = and i32 %37, 240
%39 = icmp eq i32 %38, 224
br i1 %39, label %46, label %40
40: ; preds = %34
%41 = load i8*, i8** %5
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = and i32 %43, 248
%45 = icmp eq i32 %44, 240
br i1 %45, label %46, label %55
46: ; preds = %40, %34, %28, %22
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = load i32, i32* %4
%51 = icmp sgt i32 %49, %50
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = load i8*, i8** %5
store i8 0, i8* %53
br label %58
54: ; preds = %46
br label %55
55: ; preds = %54, %40
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %5
br label %17
58: ; preds = %52, %17
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
define i8* @cbstrcututf(i8* returned %0, i32 %1) {
%3 = icmp ne i8* %0, null
%4 = icmp sgt i32 %1, -1
%5 = and i1 %3, %4
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %31, label %10
10: ; preds = %2, %26
%11 = phi i8 [ %29, %26 ], [ %8, %2 ]
%12 = phi i32 [ %27, %26 ], [ 0, %2 ]
%13 = phi i8* [ %28, %26 ], [ %0, %2 ]
%14 = zext i8 %11 to i32
%15 = insertelement <4 x i32> poison, i32 %14, i64 0
%16 = shufflevector <4 x i32> %15, <4 x i32> poison, <4 x i32> zeroinitializer
%17 = freeze <4 x i32> %16
%18 = and <4 x i32> %17, <i32 128, i32 224, i32 240, i32 248>
%19 = icmp eq <4 x i32> %18, <i32 0, i32 192, i32 224, i32 240>
%20 = bitcast <4 x i1> %19 to i4
%21 = icmp eq i4 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %10
%23 = add nsw i32 %12, 1
%24 = icmp slt i32 %12, %1
br i1 %24, label %26, label %25
25: ; preds = %22
store i8 0, i8* %13
br label %31
26: ; preds = %10, %22
%27 = phi i32 [ %23, %22 ], [ %12, %10 ]
%28 = getelementptr inbounds i8, i8* %13, i64 1
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %10
31: ; preds = %26, %2, %25
ret i8* %0
}