#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*/ die(char *);
scalar_t__ errno;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char *);
size_t strnlen(char const *, size_t);
size_t sc_string_append(char *dst, size_t dst_size, const char *str) {
  errno = 0;
  if (dst == NULL) {
    die("cannot append string: buffer is NULL");
  }
  if (str == NULL) {
    die("cannot append string: string is NULL");
  }
  size_t dst_len = strnlen(dst, dst_size);
  if (dst_len == dst_size) {
    die("cannot append string: dst is unterminated");
  }
  size_t max_str_len = dst_size - dst_len;
  size_t str_len = strnlen(str, max_str_len);
  if (str_len == max_str_len) {
    die("cannot append string: str is too long or unterminated");
  }
  memcpy(dst + dst_len, str, str_len);
  dst[dst_len + str_len] = '\0';
  return strlen(dst);
}


x86 assembly (clang, -O0):
<sc_string_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e85(%rip),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x8(%rbp)
jne    1179 <sc_string_append+0x39>
lea    0xe8c(%rip),%rdi
call   1060 <die@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1190 <sc_string_append+0x50>
lea    0xe9a(%rip),%rdi
call   1060 <die@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11bb <sc_string_append+0x7b>
lea    0xe94(%rip),%rdi
call   1060 <die@plt>
mov    -0x10(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strnlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11f2 <sc_string_append+0xb2>
lea    0xe87(%rip),%rdi
call   1060 <die@plt>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_string_append>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
mov    0x2e87(%rip),%rax
movq   $0x0,(%rax)
test   %rdi,%rdi
je     11d3 <sc_string_append+0x93>
test   %r14,%r14
je     11e4 <sc_string_append+0xa4>
mov    %r15,%rdi
mov    %r13,%rsi
call   1040 <strnlen@plt>
mov    %rax,%r12
cmp    %r13,%rax
jne    1189 <sc_string_append+0x49>
lea    0xec6(%rip),%rdi
call   1060 <die@plt>
sub    %r12,%r13
mov    %r14,%rdi
mov    %r13,%rsi
call   1040 <strnlen@plt>
mov    %rax,%rbx
cmp    %r13,%rax
jne    11ab <sc_string_append+0x6b>
lea    0xece(%rip),%rdi
call   1060 <die@plt>
lea    (%r15,%r12,1),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    %r12,%rbx
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <strlen@plt>
lea    0xe26(%rip),%rdi
call   1060 <die@plt>
test   %r14,%r14
jne    116a <sc_string_append+0x2a>
lea    0xe3a(%rip),%rdi
call   1060 <die@plt>
jmp    116a <sc_string_append+0x2a>

LLVM IR (-O0):
@errno = global i64 0
@.str = [37 x i8] c"cannot append string: buffer is NULL\00"
@.str.1 = [37 x i8] c"cannot append string: string is NULL\00"
@.str.2 = [42 x i8] c"cannot append string: dst is unterminated\00"
@.str.3 = [54 x i8] c"cannot append string: str is too long or unterminated\00"
define i64 @sc_string_append(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
store i64 0, i64* @errno
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = load i8*, i8** %4
%21 = load i64, i64* %5
%22 = call i64 @strnlen(i8* %20, i64 %21)
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = load i64, i64* %5
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = load i64, i64* %5
%30 = load i64, i64* %7
%31 = sub i64 %29, %30
store i64 %31, i64* %8
%32 = load i8*, i8** %6
%33 = load i64, i64* %8
%34 = call i64 @strnlen(i8* %32, i64 %33)
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = load i64, i64* %8
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %28
%41 = load i8*, i8** %4
%42 = load i64, i64* %7
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = load i8*, i8** %6
%45 = load i64, i64* %9
%46 = call i32 @memcpy(i8* %43, i8* %44, i64 %45)
%47 = load i8*, i8** %4
%48 = load i64, i64* %7
%49 = load i64, i64* %9
%50 = add i64 %48, %49
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %4
%53 = call i64 @strlen(i8* %52)
ret i64 %53
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [37 x i8] c"cannot append string: buffer is NULL\00"
@.str.1 = [37 x i8] c"cannot append string: string is NULL\00"
@.str.2 = [42 x i8] c"cannot append string: dst is unterminated\00"
@.str.3 = [54 x i8] c"cannot append string: str is too long or unterminated\00"
define i64 @sc_string_append(i8* %0, i64 %1, i8* %2) {
store i64 0, i64* @errno
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %3
%8 = icmp eq i8* %2, null
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %7
%12 = tail call i64 @strnlen(i8* %0, i64 %1)
%13 = icmp eq i64 %12, %1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = sub i64 %1, %12
%18 = tail call i64 @strnlen(i8* %2, i64 %17)
%19 = icmp eq i64 %18, %17
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = getelementptr inbounds i8, i8* %0, i64 %12
%24 = tail call i32 @memcpy(i8* %23, i8* %2, i64 %18)
%25 = add i64 %18, %12
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
%27 = tail call i64 @strlen(i8* dereferenceable(1) %0)
ret i64 %27
}