#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 *memchr(char *, char, int);
int buffer_after_terminator(char *buf, int buf_len, char terminator,
                            char **after_term, int *after_len) {
  char *term_addr = memchr(buf, terminator, buf_len);
  if (!term_addr) {
    *after_term = NULL;
    return -1;
  }
  *term_addr = '\0';
  *after_term = term_addr + 1;
  *after_len = buf_len - (term_addr - buf + 1);
  return 0;
}


x86 assembly (clang, -O0):
<buffer_after_terminator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %al,-0x15(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x15(%rbp),%al
mov    -0x14(%rbp),%edx
movsbl %al,%esi
call   1030 <memchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1164 <buffer_after_terminator+0x54>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <buffer_after_terminator+0x8f>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rcx
add    $0x1,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movslq -0x14(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    $0x1,%rcx
sub    %rcx,%rax
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_after_terminator>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %edx,%esi
mov    %ebp,%edx
call   1030 <memchr@plt>
test   %rax,%rax
je     114a <buffer_after_terminator+0x3a>
movb   $0x0,(%rax)
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,(%r15)
not    %eax
add    %eax,%ebx
add    %ebp,%ebx
mov    %ebx,(%r14)
xor    %eax,%eax
jmp    1156 <buffer_after_terminator+0x46>
movq   $0x0,(%r15)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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