#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;
size_t strnlen(char *, size_t);
size_t sc_string_append_char_pair(char *dst, size_t dst_size, char c1,
                                  char c2) {
  errno = 0;
  if (dst == NULL) {
    die("cannot append character pair: buffer is NULL");
  }
  size_t dst_len = strnlen(dst, dst_size);
  if (dst_len == dst_size) {
    die("cannot append character pair: dst is unterminated");
  }
  size_t max_str_len = dst_size - dst_len;
  if (max_str_len < 3) {
    die("cannot append character pair: not enough space");
  }
  if (c1 == 0 || c2 == 0) {
    die("cannot append character pair: cannot append string terminator");
  }
  dst[dst_len + 0] = c1;
  dst[dst_len + 1] = c2;
  dst[dst_len + 2] = '\0';
  return dst_len + 2;
}


x86 assembly (clang, -O0):
<sc_string_append_char_pair>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %cl,-0x11(%rbp)
mov    %al,-0x12(%rbp)
mov    0x2e9f(%rip),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x8(%rbp)
jne    115f <sc_string_append_char_pair+0x3f>
lea    0xea6(%rip),%rdi
call   1040 <die@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    118a <sc_string_append_char_pair+0x6a>
lea    0xea8(%rip),%rdi
call   1040 <die@plt>
mov    -0x10(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x3,-0x28(%rbp)
jae    11ad <sc_string_append_char_pair+0x8d>
lea    0xeb7(%rip),%rdi
call   1040 <die@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
je     11c7 <sc_string_append_char_pair+0xa7>
movsbl -0x12(%rbp),%eax
cmp    $0x0,%eax
jne    11d3 <sc_string_append_char_pair+0xb3>
lea    0xec0(%rip),%rdi
call   1040 <die@plt>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x12(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,0x1(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,0x2(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_string_append_char_pair>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2ea5(%rip),%rax
movq   $0x0,(%rax)
test   %rdi,%rdi
jne    1153 <sc_string_append_char_pair+0x33>
lea    0xeb2(%rip),%rdi
call   1040 <die@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <strnlen@plt>
mov    %rax,%rbp
cmp    %rbx,%rax
jne    1172 <sc_string_append_char_pair+0x52>
lea    0xec0(%rip),%rdi
call   1040 <die@plt>
sub    %rbp,%rbx
cmp    $0x2,%rbx
ja     1187 <sc_string_append_char_pair+0x67>
lea    0xedd(%rip),%rdi
call   1040 <die@plt>
test   %r15b,%r15b
je     1191 <sc_string_append_char_pair+0x71>
test   %r14b,%r14b
jne    119d <sc_string_append_char_pair+0x7d>
lea    0xef6(%rip),%rdi
call   1040 <die@plt>
mov    %r15b,(%r12,%rbp,1)
mov    %r14b,0x1(%r12,%rbp,1)
mov    %rbp,%rax
add    $0x2,%rax
movb   $0x0,0x2(%r12,%rbp,1)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@.str = [45 x i8] c"cannot append character pair: buffer is NULL\00"
@.str.1 = [50 x i8] c"cannot append character pair: dst is unterminated\00"
@.str.2 = [47 x i8] c"cannot append character pair: not enough space\00"
@.str.3 = [62 x i8] c"cannot append character pair: cannot append string terminator\00"
define i64 @sc_string_append_char_pair(i8* %0, i64 %1, i8 signext %2, i8 signext %3) {
store i64 0, i64* @errno
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 @die(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %4
%9 = tail call i64 @strnlen(i8* %0, i64 %1)
%10 = icmp eq i64 %9, %1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @die(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = sub i64 %1, %9
%15 = icmp ult i64 %14, 3
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @die(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = icmp eq i8 %2, 0
%20 = icmp eq i8 %3, 0
%21 = or i1 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @die(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %18, %22
%25 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 %2, i8* %25
%26 = add i64 %9, 1
%27 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 %3, i8* %27
%28 = add i64 %9, 2
%29 = getelementptr inbounds i8, i8* %0, i64 %28
store i8 0, i8* %29
ret i64 %28
}