#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 *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int strcspn(char const *, char const *);
char *struntil(const char *str, const char *reject, const char **save) {
  size_t size;
  char *ret;
  size = strcspn(str, reject) + 1;
  ret = malloc(size * sizeof(char));
  memcpy(ret, str, size - 1);
  ret[size - 1] = '\0';
  if (save) {
    *save = &str[size - 1];
  }
  return ret;
}


x86 assembly (clang, -O0):
<struntil>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcspn@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     11b1 <struntil+0x81>
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
add    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<struntil>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%r15
call   1030 <strcspn@plt>
movslq %eax,%rbx
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
test   %r14,%r14
je     1170 <struntil+0x40>
add    %rbx,%r15
mov    %r15,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @struntil(i8* %0, i8* nocapture %1, i8** writeonly %2) {
%4 = tail call i32 @strcspn(i8* %0, i8* %1)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i8* @malloc(i64 %6)
%8 = add nsw i64 %6, -1
%9 = tail call i32 @memcpy(i8* %7, i8* %0, i64 %8)
%10 = getelementptr inbounds i8, i8* %7, i64 %8
store i8 0, i8* %10
%11 = icmp eq i8** %2, null
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = getelementptr inbounds i8, i8* %0, i64 %8
store i8* %13, i8** %2
br label %14
14: ; preds = %12, %3
ret i8* %7
}