#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 strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void string_append(char *target, int target_length, const char *source) {
  int offset;
  if (target_length <= 0) {
    return;
  }
  offset = strlen(target);
  if (offset >= target_length - 1) {
    return;
  }
  strncpy(target + offset, source, target_length - offset - 1);
  target[target_length - 1] = '\0';
}


x86 assembly (clang, -O0):
<string_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1142 <string_append+0x22>
jmp    1192 <string_append+0x72>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1164 <string_append+0x44>
jmp    1192 <string_append+0x72>
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_append>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    115a <string_append+0x3a>
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
call   1040 <strlen@plt>
lea    -0x1(%rbx),%ebp
cmp    %ebp,%eax
jge    115a <string_append+0x3a>
movslq %eax,%rdi
add    %r15,%rdi
not    %eax
add    %ebx,%eax
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    %ebp,%eax
movb   $0x0,(%r15,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @string_append(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i32, i32* %5
%9 = icmp sle i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %3
br label %35
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = icmp sge i32 %14, %16
br i1 %17, label %18, label %19
18: ; preds = %11
br label %35
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8*, i8** %6
%25 = load i32, i32* %5
%26 = load i32, i32* %7
%27 = sub nsw i32 %25, %26
%28 = sub nsw i32 %27, 1
%29 = call i32 @strncpy(i8* %23, i8* %24, i32 %28)
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %19, %18, %10
ret void
}

LLVM IR (-O2):
define void @string_append(i8* %0, i32 %1, i8* %2) {
%4 = icmp slt i32 %1, 1
br i1 %4, label %17, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %0)
%7 = add nsw i32 %1, -1
%8 = icmp slt i32 %6, %7
br i1 %8, label %9, label %17
9: ; preds = %5
%10 = sext i32 %6 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = xor i32 %6, -1
%13 = add i32 %12, %1
%14 = tail call i32 @strncpy(i8* %11, i8* %2, i32 %13)
%15 = zext i32 %7 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %5, %3, %9
ret void
}