#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*/ strncpy(char *, char const *, int);
void string_copy(char *target, int target_length, const char *source,
                 int source_length) {
  int copy_length;
  if (target_length <= 0) {
    return;
  }
  if (source_length >= 0 && source_length < target_length - 1) {
    copy_length = source_length;
  } else {
    copy_length = target_length - 1;
  }
  strncpy(target, source, copy_length);
  target[copy_length] = '\0';
}


x86 assembly (clang, -O0):
<string_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1135 <string_copy+0x25>
jmp    1180 <string_copy+0x70>
cmpl   $0x0,-0x1c(%rbp)
jl     115b <string_copy+0x4b>
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    115b <string_copy+0x4b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1164 <string_copy+0x54>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_copy>:
test   %esi,%esi
jle    1140 <string_copy+0x30>
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
add    $0xffffffff,%esi
cmp    %ecx,%esi
mov    %esi,%ebx
cmovg  %ecx,%ebx
test   %ecx,%ecx
cmovs  %esi,%ebx
mov    %rdx,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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