#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 strcmp(char *, char *);
int strlen(char *);
int str_tailcmp(char *s, char *tail) {
  int ls = strlen(s), lt = strlen(tail);
  if (lt == 0)
    return 0;
  if (lt > ls)
    return 1;
  return strcmp(s + ls - lt, tail);
}


x86 assembly (clang, -O0):
<str_tailcmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    115e <str_tailcmp+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    1199 <str_tailcmp+0x79>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1176 <str_tailcmp+0x56>
movl   $0x1,-0x4(%rbp)
jmp    1199 <str_tailcmp+0x79>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
movslq -0x20(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcmp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_tailcmp>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     1160 <str_tailcmp+0x40>
mov    $0x1,%ecx
cmp    %ebp,%eax
jg     1162 <str_tailcmp+0x42>
movslq %ebp,%rcx
add    %rcx,%rbx
cltq
sub    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <strcmp@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @str_tailcmp(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = icmp sgt i32 %4, %3
br i1 %7, label %15, label %8
8: ; preds = %6
%9 = sext i32 %3 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = sext i32 %4 to i64
%12 = sub nsw i64 0, %11
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = tail call i32 @strcmp(i8* dereferenceable(1) %13, i8* dereferenceable(1) %1)
br label %15
15: ; preds = %6, %2, %8
%16 = phi i32 [ %14, %8 ], [ 0, %2 ], [ 1, %6 ]
ret i32 %16
}