#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*/ bprintf(char *, size_t, char *, char *, char *);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ strcmp(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, size_t);
char *swapsuffix(char *buf, size_t sz, char *s, char *suf, char *swap) {
  size_t slen, suflen, swaplen;
  slen = strlen(s);
  suflen = strlen(suf);
  swaplen = strlen(swap);
  if (slen < suflen)
    return NULL;
  if (slen + swaplen >= sz)
    die("swapsuffix: buf too small");
  buf[0] = '\0';
  if (suflen < slen && !strcmp(suf, &s[slen - suflen])) {
    strncat(buf, s, slen - suflen);
    strncat(buf, swap, swaplen);
  } else {
    bprintf(buf, sz, "%s%s", s, swap);
  }
  return buf;
}


x86 assembly (clang, -O0):
<swapsuffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    11ae <swapsuffix+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    1251 <swapsuffix+0x101>
mov    -0x38(%rbp),%rax
add    -0x48(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11cc <swapsuffix+0x7c>
lea    0xe39(%rip),%rdi
call   1070 <die@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    122d <swapsuffix+0xdd>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rax
sub    -0x40(%rbp),%rax
add    %rax,%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    122d <swapsuffix+0xdd>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
sub    -0x40(%rbp),%rdx
call   1050 <strncat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1050 <strncat@plt>
jmp    1249 <swapsuffix+0xf9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xdd6(%rip),%rdx
call   1040 <bprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swapsuffix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r15
mov    %rcx,%r13
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,0x10(%rsp)
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    %r12,%rbx
jae    1199 <swapsuffix+0x49>
xor    %ebp,%ebp
jmp    1220 <swapsuffix+0xd0>
mov    %r13,0x20(%rsp)
mov    %rbp,%r13
mov    %r15,0x8(%rsp)
mov    %rax,0x18(%rsp)
add    %rbx,%rax
cmp    %r14,%rax
jb     11bf <swapsuffix+0x6f>
lea    0xe46(%rip),%rdi
call   1070 <die@plt>
mov    0x10(%rsp),%rbp
movb   $0x0,0x0(%rbp)
sub    %r12,%rbx
jbe    11e4 <swapsuffix+0x94>
lea    (%rbx,%r13,1),%rsi
mov    0x20(%rsp),%rdi
call   1060 <strcmp@plt>
mov    0x10(%rsp),%rbp
test   %eax,%eax
je     1200 <swapsuffix+0xb0>
lea    0xe2f(%rip),%rdx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r13,%rcx
mov    0x8(%rsp),%r8
call   1040 <bprintf@plt>
jmp    1220 <swapsuffix+0xd0>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <strncat@plt>
mov    %rbp,%rdi
mov    0x8(%rsp),%rsi
mov    0x18(%rsp),%rdx
call   1050 <strncat@plt>
mov    %rbp,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"swapsuffix: buf too small\00"
@.str.1 = [5 x i8] c"%s%s\00"
define i8* @swapsuffix(i8* %0, i64 %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%15 = load i8*, i8** %9
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %12
%17 = load i8*, i8** %10
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %13
%19 = load i8*, i8** %11
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %14
%21 = load i64, i64* %12
%22 = load i64, i64* %13
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %5
store i8* null, i8** %6
br label %67
25: ; preds = %5
%26 = load i64, i64* %12
%27 = load i64, i64* %14
%28 = add i64 %26, %27
%29 = load i64, i64* %8
%30 = icmp uge i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %25
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i64 0
store i8 0, i8* %35
%36 = load i64, i64* %13
%37 = load i64, i64* %12
%38 = icmp ult i64 %36, %37
br i1 %38, label %39, label %59
39: ; preds = %33
%40 = load i8*, i8** %10
%41 = load i8*, i8** %9
%42 = load i64, i64* %12
%43 = load i64, i64* %13
%44 = sub i64 %42, %43
%45 = getelementptr inbounds i8, i8* %41, i64 %44
%46 = call i32 @strcmp(i8* %40, i8* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %59, label %48
48: ; preds = %39
%49 = load i8*, i8** %7
%50 = load i8*, i8** %9
%51 = load i64, i64* %12
%52 = load i64, i64* %13
%53 = sub i64 %51, %52
%54 = call i32 @strncat(i8* %49, i8* %50, i64 %53)
%55 = load i8*, i8** %7
%56 = load i8*, i8** %11
%57 = load i64, i64* %14
%58 = call i32 @strncat(i8* %55, i8* %56, i64 %57)
br label %65
59: ; preds = %39, %33
%60 = load i8*, i8** %7
%61 = load i64, i64* %8
%62 = load i8*, i8** %9
%63 = load i8*, i8** %11
%64 = call i32 @bprintf(i8* %60, i64 %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %62, i8* %63)
br label %65
65: ; preds = %59, %48
%66 = load i8*, i8** %7
store i8* %66, i8** %6
br label %67
67: ; preds = %65, %24
%68 = load i8*, i8** %6
ret i8* %68
}

LLVM IR (-O2):
@.str = [26 x i8] c"swapsuffix: buf too small\00"
@.str.1 = [5 x i8] c"%s%s\00"
define i8* @swapsuffix(i8* %0, i64 %1, i8* %2, i8* nocapture %3, i8* %4) {
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%9 = icmp ult i64 %6, %7
br i1 %9, label %27, label %10
10: ; preds = %5
%11 = add i64 %8, %6
%12 = icmp ult i64 %11, %1
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
store i8 0, i8* %0
%16 = icmp ugt i64 %6, %7
br i1 %16, label %17, label %25
17: ; preds = %15
%18 = sub i64 %6, %7
%19 = getelementptr inbounds i8, i8* %2, i64 %18
%20 = tail call i32 @strcmp(i8* dereferenceable(1) %3, i8* dereferenceable(1) %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = tail call i32 @strncat(i8* %0, i8* %2, i64 %18)
%24 = tail call i32 @strncat(i8* %0, i8* %4, i64 %8)
br label %27
25: ; preds = %17, %15
%26 = tail call i32 @bprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %4)
br label %27
27: ; preds = %22, %25, %5
%28 = phi i8* [ null, %5 ], [ %0, %25 ], [ %0, %22 ]
ret i8* %28
}