#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(char const *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *strdup(char const *);
size_t strlen(char const *);
char *strndup(char const *, size_t);
char *strnappend(const char *s, const char *suffix, size_t b) {
  size_t a;
  char *r;
  if (!s && !suffix)
    return strdup("");
  if (!s)
    return strndup(suffix, b);
  if (!suffix)
    return strdup(s);
  assert(s);
  assert(suffix);
  a = strlen(s);
  if (b > ((size_t)-1) - a)
    return NULL;
  r = malloc(a + b + 1);
  if (!r)
    return NULL;
  memcpy(r, s, a);
  memcpy(r + a, suffix, b);
  r[a + b] = 0;
  return r;
}


x86 assembly (clang, -O0):
<strnappend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119f <strnappend+0x3f>
cmpq   $0x0,-0x18(%rbp)
jne    119f <strnappend+0x3f>
lea    0xe6f(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    128c <strnappend+0x12c>
cmpq   $0x0,-0x10(%rbp)
jne    11c0 <strnappend+0x60>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strndup@plt>
mov    %rax,-0x8(%rbp)
jmp    128c <strnappend+0x12c>
cmpq   $0x0,-0x18(%rbp)
jne    11dd <strnappend+0x7d>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    128c <strnappend+0x12c>
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    $0xffffffffffffffff,%rcx
sub    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jbe    1221 <strnappend+0xc1>
movq   $0x0,-0x8(%rbp)
jmp    128c <strnappend+0x12c>
mov    -0x28(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    124e <strnappend+0xee>
movq   $0x0,-0x8(%rbp)
jmp    128c <strnappend+0x12c>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strnappend>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rax
or     %rsi,%rax
je     11ec <strnappend+0x8c>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
test   %rdi,%rdi
je     1206 <strnappend+0xa6>
test   %r15,%r15
je     1234 <strnappend+0xd4>
mov    %rbx,%rdi
call   1050 <assert@plt>
mov    %r15,%rdi
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
add    %r14,%rbp
jb     121f <strnappend+0xbf>
mov    %rax,%r13
lea    (%rax,%r14,1),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     121f <strnappend+0xbf>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
mov    %r12,%rdi
add    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r12,%rbp,1)
jmp    1222 <strnappend+0xc2>
lea    0xe0d(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <strdup@plt>
mov    %r15,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strndup@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
jmp    11f3 <strnappend+0x93>

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @strnappend(i8* %0, i8* %1, i64 %2) {
%4 = icmp ne i8* %0, null
%5 = icmp ne i8* %1, null
%6 = or i1 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %3
br i1 %4, label %12, label %10
10: ; preds = %9
%11 = tail call i8* @strndup(i8* %1, i64 %2)
br label %31
12: ; preds = %9
br i1 %5, label %15, label %13
13: ; preds = %12
%14 = tail call i8* @strdup(i8* %0)
br label %31
15: ; preds = %12
%16 = tail call i32 @assert(i8* %0)
%17 = tail call i32 @assert(i8* %1)
%18 = tail call i64 @strlen(i8* %0)
%19 = xor i64 %18, -1
%20 = icmp ult i64 %19, %2
br i1 %20, label %31, label %21
21: ; preds = %15
%22 = add i64 %18, %2
%23 = add i64 %22, 1
%24 = tail call i8* @malloc(i64 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = tail call i32 @memcpy(i8* %24, i8* %0, i64 %18)
%28 = getelementptr inbounds i8, i8* %24, i64 %18
%29 = tail call i32 @memcpy(i8* %28, i8* %1, i64 %2)
%30 = getelementptr inbounds i8, i8* %24, i64 %22
store i8 0, i8* %30
br label %31
31: ; preds = %21, %15, %26, %13, %10, %7
%32 = phi i8* [ %24, %26 ], [ %14, %13 ], [ %11, %10 ], [ %8, %7 ], [ null, %15 ], [ null, %21 ]
ret i8* %32
}