#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 /*<<< orphan*/ mbstate_t;
typedef int /*<<< orphan*/ mbs;

/* Variables and functions */
int /*<<< orphan*/ MB_LEN_MAX;
size_t mbrlen(char *, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strchr(char *, char);
scalar_t__ strcmp(char const *, char *);
void stripsuffix(char *p, const char *suffix, size_t suffixlen) {
  char *q, *r;
  mbstate_t mbs;
  size_t n;
  if (suffixlen && (q = strchr(p, '\0') - suffixlen) > p &&
      strcmp(suffix, q) == 0) {
    memset(&mbs, 0, sizeof(mbs));
    for (r = p; r < q; r += n) {
      n = mbrlen(r, MB_LEN_MAX, &mbs);
      if (n == (size_t)-1 || n == (size_t)-2) {
        memset(&mbs, 0, sizeof(mbs));
        n = 1;
      }
    }
    if (q == r)
      *q = '\0';
  }
}


x86 assembly (clang, -O0):
<stripsuffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1233 <stripsuffix+0xf3>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
cltq
sub    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmp    -0x8(%rbp),%rax
jbe    1233 <stripsuffix+0xf3>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1233 <stripsuffix+0xf3>
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1219 <stripsuffix+0xd9>
mov    -0x28(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x2c(%rbp),%rdx
call   1060 <mbrlen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
je     11eb <stripsuffix+0xab>
cmpq   $0xfffffffffffffffe,-0x38(%rbp)
jne    1203 <stripsuffix+0xc3>
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movq   $0x1,-0x38(%rbp)
jmp    1208 <stripsuffix+0xc8>
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11ad <stripsuffix+0x6d>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    122e <stripsuffix+0xee>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1233 <stripsuffix+0xf3>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stripsuffix>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11d9 <stripsuffix+0x99>
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %esi,%esi
call   1030 <strchr@plt>
movslq %eax,%r14
sub    %r12,%r14
cmp    %rbx,%r14
jbe    11d9 <stripsuffix+0x99>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11d9 <stripsuffix+0x99>
lea    0x4(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e49(%rip),%r12
jmp    11a8 <stripsuffix+0x68>
nopl   0x0(%rax)
add    %rax,%rbx
cmp    %r14,%rbx
jae    11d3 <stripsuffix+0x93>
mov    (%r12),%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1060 <mbrlen@plt>
cmp    $0xfffffffffffffffe,%rax
jb     11a0 <stripsuffix+0x60>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    $0x1,%eax
jmp    11a0 <stripsuffix+0x60>
jne    11d9 <stripsuffix+0x99>
movb   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@MB_LEN_MAX = global i32 0
define void @stripsuffix(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i64 %2, 0
br i1 %6, label %32, label %7
7: ; preds = %3
%8 = tail call i32 @strchr(i8* %0, i8 signext 0)
%9 = sext i32 %8 to i64
%10 = sub i64 %9, %2
%11 = inttoptr i64 %10 to i8*
%12 = icmp ugt i8* %11, %0
br i1 %12, label %13, label %32
13: ; preds = %7
%14 = tail call i64 @strcmp(i8* %1, i8* %11)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %13
%17 = call i32 @memset(i32* %4, i32 0, i32 4)
br label %18
18: ; preds = %16, %25
%19 = phi i8* [ %27, %25 ], [ %0, %16 ]
%20 = load i32, i32* @MB_LEN_MAX
%21 = call i64 @mbrlen(i8* %19, i32 %20, i32* %4)
%22 = icmp ugt i64 %21, -3
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @memset(i32* %4, i32 0, i32 4)
br label %25
25: ; preds = %23, %18
%26 = phi i64 [ 1, %23 ], [ %21, %18 ]
%27 = getelementptr inbounds i8, i8* %19, i64 %26
%28 = icmp ult i8* %27, %11
br i1 %28, label %18, label %29
29: ; preds = %25
%30 = icmp eq i8* %27, %11
br i1 %30, label %31, label %32
31: ; preds = %29
store i8 0, i8* %11
br label %32
32: ; preds = %29, %31, %13, %7, %3
ret void
}