#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 const *, char const *);
size_t strlen(char const *);
int strncmp_suffix(const char *str1, const char *str2, size_t n) {
  size_t len1 = strlen(str1);
  size_t len2 = strlen(str2);
  if (len1 > n)
    len1 = n;
  if (len2 <= len1)
    return strcmp(str1 + len1 - len2, str2);
  else
    return strcmp(str1, str2);
}


x86 assembly (clang, -O0):
<strncmp_suffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1164 <strncmp_suffix+0x44>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
ja     1194 <strncmp_suffix+0x74>
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
xor    %eax,%eax
sub    -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcmp@plt>
mov    %eax,-0x4(%rbp)
jmp    11a4 <strncmp_suffix+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcmp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncmp_suffix>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cmp    %r15,%rbx
cmova  %r15,%rbx
lea    (%r12,%rbx,1),%rdi
sub    %rax,%rdi
cmp    %rbx,%rax
cmova  %r12,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <strcmp@plt>

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

LLVM IR (-O2):
define i32 @strncmp_suffix(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = icmp ugt i64 %4, %2
%7 = select i1 %6, i64 %2, i64 %4
%8 = icmp ugt i64 %5, %7
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = sub i64 0, %5
%11 = getelementptr inbounds i8, i8* %9, i64 %10
%12 = select i1 %8, i8* %0, i8* %11
%13 = tail call i32 @strcmp(i8* dereferenceable(1) %12, i8* dereferenceable(1) %1)
ret i32 %13
}