#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 */
scalar_t__ alloca(int);
char *malloc(int);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strcut(char *, int, int);
int strlen(char *);
char *parse_slice(char *buf, int len, char sep) {
  char *pr = strchr(buf, sep);
  if (pr == NULL)
    return pr;
  int plen = strlen(pr);
  int size = sizeof(*pr) * plen;
  char *cache = (char *)alloca(size);
  strcpy(cache, buf);
  strcut(cache, 0, strlen(buf) - plen + 1);
  char *part = malloc(size);
  if (part == NULL)
    return NULL;
  strcpy(part, (char *)cache);
  int offset = strlen(buf) - strlen(pr);
  strcut(buf, offset, len);
  return part;
}


x86 assembly (clang, -O0):
<parse_slice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x15(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119d <parse_slice+0x3d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1258 <parse_slice+0xf8>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <alloca@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
sub    -0x24(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1080 <strcut@plt>
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1215 <parse_slice+0xb5>
movq   $0x0,-0x8(%rbp)
jmp    1258 <parse_slice+0xf8>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1080 <strcut@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_slice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    %edx,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1200 <parse_slice+0xa0>
mov    %rax,%r15
mov    %ebx,0x4(%rsp)
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <alloca@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
sub    %ebx,%eax
lea    0x1(%rax),%edx
xor    %ebp,%ebp
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <strcut@plt>
mov    %ebx,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1202 <parse_slice+0xa2>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
mov    0x4(%rsp),%edx
call   1080 <strcut@plt>
mov    %r12,%rbp
jmp    1202 <parse_slice+0xa2>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @parse_slice(i8* %0, i32 %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8 %2, i8* %7
%14 = load i8*, i8** %5
%15 = load i8, i8* %7
%16 = call i8* @strchr(i8* %14, i8 signext %15)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = load i8*, i8** %8
store i8* %20, i8** %4
br label %60
21: ; preds = %3
%22 = load i8*, i8** %8
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = mul i64 1, %25
%27 = trunc i64 %26 to i32
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = call i64 @alloca(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %11
%31 = load i8*, i8** %11
%32 = load i8*, i8** %5
%33 = call i32 @strcpy(i8* %31, i8* %32)
%34 = load i8*, i8** %11
%35 = load i8*, i8** %5
%36 = call i32 @strlen(i8* %35)
%37 = load i32, i32* %9
%38 = sub nsw i32 %36, %37
%39 = add nsw i32 %38, 1
%40 = call i32 @strcut(i8* %34, i32 0, i32 %39)
%41 = load i32, i32* %10
%42 = call i8* @malloc(i32 %41)
store i8* %42, i8** %12
%43 = load i8*, i8** %12
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %46
45: ; preds = %21
store i8* null, i8** %4
br label %60
46: ; preds = %21
%47 = load i8*, i8** %12
%48 = load i8*, i8** %11
%49 = call i32 @strcpy(i8* %47, i8* %48)
%50 = load i8*, i8** %5
%51 = call i32 @strlen(i8* %50)
%52 = load i8*, i8** %8
%53 = call i32 @strlen(i8* %52)
%54 = sub nsw i32 %51, %53
store i32 %54, i32* %13
%55 = load i8*, i8** %5
%56 = load i32, i32* %13
%57 = load i32, i32* %6
%58 = call i32 @strcut(i8* %55, i32 %56, i32 %57)
%59 = load i8*, i8** %12
store i8* %59, i8** %4
br label %60
60: ; preds = %46, %45, %19
%61 = load i8*, i8** %4
ret i8* %61
}

LLVM IR (-O2):
define i8* @parse_slice(i8* %0, i32 %1, i8 signext %2) {
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %4)
%8 = tail call i64 @alloca(i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i32 @strcpy(i8* %9, i8* %0)
%11 = tail call i32 @strlen(i8* %0)
%12 = sub i32 1, %7
%13 = add i32 %12, %11
%14 = tail call i32 @strcut(i8* %9, i32 0, i32 %13)
%15 = tail call i8* @malloc(i32 %7)
%16 = icmp eq i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %6
%18 = tail call i32 @strcpy(i8* %15, i8* %9)
%19 = tail call i32 @strlen(i8* %0)
%20 = tail call i32 @strlen(i8* %4)
%21 = sub nsw i32 %19, %20
%22 = tail call i32 @strcut(i8* %0, i32 %21, i32 %1)
br label %23
23: ; preds = %17, %6, %3
%24 = phi i8* [ null, %3 ], [ %15, %17 ], [ null, %6 ]
ret i8* %24
}