#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(int);
scalar_t__ strlen(char *);
char *strncat(char *, char const *, size_t);
char *StrAppendMax(char *target, size_t max, const char *source) {
  assert((0 != (target)));
  assert((0 != (source)));
  assert(max > 0);
  max -= strlen((target)) + 1;
  return (max > 0) ? strncat(target, source, max) : target;
}


x86 assembly (clang, -O0):
<StrAppendMax>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    11bc <StrAppendMax+0x8c>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <strncat@plt>
mov    %rax,-0x20(%rbp)
jmp    11c4 <StrAppendMax+0x94>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrAppendMax>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
not    %rax
add    %r15,%rax
je     1186 <StrAppendMax+0x56>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <strncat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @StrAppendMax(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%7 = load i8*, i8** %4
%8 = icmp ne i8* null, %7
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8*, i8** %6
%12 = icmp ne i8* null, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i64, i64* %5
%16 = icmp ugt i64 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %4
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = load i64, i64* %5
%23 = sub i64 %22, %21
store i64 %23, i64* %5
%24 = load i64, i64* %5
%25 = icmp ugt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %3
%27 = load i8*, i8** %4
%28 = load i8*, i8** %6
%29 = load i64, i64* %5
%30 = call i8* @strncat(i8* %27, i8* %28, i64 %29)
br label %33
31: ; preds = %3
%32 = load i8*, i8** %4
br label %33
33: ; preds = %31, %26
%34 = phi i8* [ %30, %26 ], [ %32, %31 ]
ret i8* %34
}

LLVM IR (-O2):
define i8* @StrAppendMax(i8* returned %0, i64 %1, i8* %2) {
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8* %2, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i64 %1, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%14 = xor i64 %13, -1
%15 = add i64 %14, %1
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %3
%18 = tail call i8* @strncat(i8* dereferenceable(1) %0, i8* %2, i64 %15)
br label %19
19: ; preds = %3, %17
ret i8* %0
}