#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*/);
scalar_t__ strcmp(char *, char const *);
size_t strlen(char const *);
char *strendzap(char *str, const char *pat) {
  size_t len, patlen;
  assert(str != NULL);
  assert(pat != NULL);
  len = strlen(str);
  patlen = strlen(pat);
  if (patlen <= len) {
    str += len - patlen;
    if (strcmp(str, pat) == 0)
      *str = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<strendzap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11bf <strendzap+0x8f>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11ba <strendzap+0x8a>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    11bf <strendzap+0x8f>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strendzap>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
sub    %rax,%rbx
jb     1186 <strendzap+0x56>
add    %rbx,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    1186 <strendzap+0x56>
movb   $0x0,(%r15)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strendzap(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %3
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %5
%17 = load i8*, i8** %4
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = load i64, i64* %5
%21 = icmp ule i64 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %2
%23 = load i64, i64* %5
%24 = load i64, i64* %6
%25 = sub i64 %23, %24
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i64 %25
store i8* %27, i8** %3
%28 = load i8*, i8** %3
%29 = load i8*, i8** %4
%30 = call i64 @strcmp(i8* %28, i8* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = load i8*, i8** %3
store i8 0, i8* %33
br label %34
34: ; preds = %32, %22
br label %35
35: ; preds = %34, %2
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
define i8* @strendzap(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i8* %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%11 = icmp ult i64 %9, %10
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = sub i64 %9, %10
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = tail call i64 @strcmp(i8* %14, i8* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i8 0, i8* %14
br label %18
18: ; preds = %12, %17, %2
%19 = phi i8* [ %14, %17 ], [ %14, %12 ], [ %0, %2 ]
ret i8* %19
}