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


x86 assembly (clang, -O0):
<sc_string_append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    0x2ea4(%rip),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x8(%rbp)
jne    115a <sc_string_append_char+0x3a>
lea    0xeab(%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    1185 <sc_string_append_char+0x65>
lea    0xea8(%rip),%rdi
call   1040 <die@plt>
mov    -0x10(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x2,-0x28(%rbp)
jae    11a8 <sc_string_append_char+0x88>
lea    0xeb2(%rip),%rdi
call   1040 <die@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    11c1 <sc_string_append_char+0xa1>
lea    0xec3(%rip),%rdi
call   1040 <die@plt>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_string_append_char>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
mov    0x2ea9(%rip),%rax
movq   $0x0,(%rax)
test   %rdi,%rdi
jne    114f <sc_string_append_char+0x2f>
lea    0xeb6(%rip),%rdi
call   1040 <die@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strnlen@plt>
mov    %rax,%rbp
cmp    %rbx,%rax
jne    116e <sc_string_append_char+0x4e>
lea    0xebf(%rip),%rdi
call   1040 <die@plt>
sub    %rbp,%rbx
cmp    $0x1,%rbx
ja     1183 <sc_string_append_char+0x63>
lea    0xed7(%rip),%rdi
call   1040 <die@plt>
test   %r14b,%r14b
jne    1194 <sc_string_append_char+0x74>
lea    0xef0(%rip),%rdi
call   1040 <die@plt>
mov    %r14b,(%r15,%rbp,1)
mov    %rbp,%rax
add    $0x1,%rax
movb   $0x0,0x1(%r15,%rbp,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@.str = [40 x i8] c"cannot append character: buffer is NULL\00"
@.str.1 = [45 x i8] c"cannot append character: dst is unterminated\00"
@.str.2 = [42 x i8] c"cannot append character: not enough space\00"
@.str.3 = [57 x i8] c"cannot append character: cannot append string terminator\00"
define i64 @sc_string_append_char(i8* %0, i64 %1, i8 signext %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 ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %3
%8 = tail call i64 @strnlen(i8* %0, i64 %1)
%9 = icmp eq i64 %8, %1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @die(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = sub i64 %1, %8
%14 = icmp ult i64 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = icmp eq i8 %2, 0
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 @die(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %17
%22 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %2, i8* %22
%23 = add i64 %8, 1
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
ret i64 %23
}