#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 */
char *memccpy(char *, char const *, char, size_t);
size_t copy_substr(char dst[], size_t dst_len, const char src[],
                   char terminator) {
  char *past_end;
  if (dst_len == 0U) {
    return 0U;
  }
  past_end = memccpy(dst, src, terminator, dst_len);
  if (past_end == NULL) {
    dst[dst_len - 1] = '\0';
    return dst_len;
  } else {
    past_end[-1] = '\0';
    return past_end - dst;
  }
}


x86 assembly (clang, -O0):
<copy_substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %al,-0x21(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1141 <copy_substr+0x31>
movq   $0x0,-0x8(%rbp)
jmp    119b <copy_substr+0x8b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x21(%rbp),%al
mov    -0x18(%rbp),%rcx
movsbl %al,%edx
call   1030 <memccpy@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1184 <copy_substr+0x74>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    119b <copy_substr+0x8b>
mov    -0x30(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_substr>:
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1140 <copy_substr+0x30>
mov    %rsi,%rbx
mov    %rdi,%r14
movsbl %cl,%eax
mov    %rdx,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1030 <memccpy@plt>
test   %rax,%rax
je     1144 <copy_substr+0x34>
movb   $0x0,-0x1(%rax)
sub    %r14,%rax
mov    %rax,%rbx
jmp    114a <copy_substr+0x3a>
xor    %ebx,%ebx
jmp    114a <copy_substr+0x3a>
movb   $0x0,-0x1(%rbx,%r14,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @copy_substr(i8* %0, i64 %1, i8* nocapture %2, i8 signext %3) {
%5 = icmp eq i64 %1, 0
br i1 %5, label %17, label %6
6: ; preds = %4
%7 = tail call i8* @memccpy(i8* %0, i8* %2, i8 signext %3, i64 %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = add i64 %1, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
br label %17
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %7, i64 -1
store i8 0, i8* %13
%14 = ptrtoint i8* %7 to i64
%15 = ptrtoint i8* %0 to i64
%16 = sub i64 %14, %15
br label %17
17: ; preds = %4, %12, %9
%18 = phi i64 [ %1, %9 ], [ %16, %12 ], [ 0, %4 ]
ret i64 %18
}