#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*/ memmove(char *, char const *, size_t);
size_t cwk_path_output_sized(char *buffer, size_t buffer_size, size_t position,
                             const char *str, size_t length) {
  size_t amount_written;
  if (buffer_size > position + length) {
    amount_written = length;
  } else if (buffer_size > position) {
    amount_written = buffer_size - position;
  } else {
    amount_written = 0;
  }
  if (amount_written > 0) {
    memmove(&buffer[position], str, amount_written);
  }
  return length;
}


x86 assembly (clang, -O0):
<cwk_path_output_sized>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jbe    114e <cwk_path_output_sized+0x3e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    117a <cwk_path_output_sized+0x6a>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    116d <cwk_path_output_sized+0x5d>
mov    -0x10(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1175 <cwk_path_output_sized+0x65>
movq   $0x0,-0x30(%rbp)
jmp    117a <cwk_path_output_sized+0x6a>
cmpq   $0x0,-0x30(%rbp)
jbe    119a <cwk_path_output_sized+0x8a>
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <memmove@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cwk_path_output_sized>:
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rdx,%rax
add    %rdx,%r8
xor    %edx,%edx
mov    %rsi,%rbx
sub    %rax,%rbx
cmovae %rbx,%rdx
cmp    %rsi,%r8
cmovb  %r14,%rdx
test   %rdx,%rdx
je     1140 <cwk_path_output_sized+0x30>
add    %rax,%rdi
mov    %rcx,%rsi
call   1030 <memmove@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @cwk_path_output_sized(i8* %0, i64 %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%12 = load i64, i64* %7
%13 = load i64, i64* %8
%14 = load i64, i64* %10
%15 = add i64 %13, %14
%16 = icmp ugt i64 %12, %15
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = load i64, i64* %10
store i64 %18, i64* %11
br label %29
19: ; preds = %5
%20 = load i64, i64* %7
%21 = load i64, i64* %8
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i64, i64* %7
%25 = load i64, i64* %8
%26 = sub i64 %24, %25
store i64 %26, i64* %11
br label %28
27: ; preds = %19
store i64 0, i64* %11
br label %28
28: ; preds = %27, %23
br label %29
29: ; preds = %28, %17
%30 = load i64, i64* %11
%31 = icmp ugt i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = load i8*, i8** %6
%34 = load i64, i64* %8
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = load i8*, i8** %9
%37 = load i64, i64* %11
%38 = call i32 @memmove(i8* %35, i8* %36, i64 %37)
br label %39
39: ; preds = %32, %29
%40 = load i64, i64* %10
ret i64 %40
}

LLVM IR (-O2):
define i64 @cwk_path_output_sized(i8* %0, i64 %1, i64 %2, i8* %3, i64 returned %4) {
%6 = add i64 %4, %2
%7 = icmp ult i64 %6, %1
%8 = tail call i64 @llvm.usub.sat.i64(i64 %1, i64 %2)
%9 = select i1 %7, i64 %4, i64 %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %5
%12 = getelementptr inbounds i8, i8* %0, i64 %2
%13 = tail call i32 @memmove(i8* %12, i8* %3, i64 %9)
br label %14
14: ; preds = %11, %5
ret i64 %4
}