#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int strlen(char const *);
char *strsep(char **, char const *);
char *xmalloc(size_t);
char *xstrdup(char const *);
char *str_chop(const char *str, const char *delim) {
  assert(str != NULL);
  assert(delim != NULL);
  size_t length = strlen(str) + 1;
  char *chunks = xmalloc(length * sizeof(char));
  char *copy = xstrdup(str);
  char *fresh_copy = copy;
  int i = 0;
  while (*copy) {
    chunks[i] = *(strsep(&copy, delim));
    i += 1;
  }
  free(fresh_copy);
  return chunks;
}


x86 assembly (clang, -O0):
<str_chop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1208 <str_chop+0xa8>
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdi
call   1080 <strsep@plt>
mov    (%rax),%dl
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d3 <str_chop+0x73>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_chop>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r13
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %r13,%r13
setne  %dil
call   1070 <assert@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1060 <xstrdup@plt>
mov    %rax,%r15
mov    %rax,0x8(%rsp)
cmpb   $0x0,(%rax)
je     11ee <str_chop+0x8e>
lea    0x8(%rsp),%r12
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r13,%rsi
call   1080 <strsep@plt>
movzbl (%rax),%eax
mov    %al,(%rbx)
mov    0x8(%rsp),%rax
add    $0x1,%rbx
cmpb   $0x0,(%rax)
jne    11d0 <str_chop+0x70>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_chop(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = mul i64 %22, 1
%24 = call i8* @xmalloc(i64 %23)
store i8* %24, i8** %6
%25 = load i8*, i8** %3
%26 = call i8* @xstrdup(i8* %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
store i8* %27, i8** %8
store i32 0, i32* %9
br label %28
28: ; preds = %32, %2
%29 = load i8*, i8** %7
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = call i8* @strsep(i8** %7, i8* %33)
%35 = load i8, i8* %34
%36 = load i8*, i8** %6
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 %35, i8* %39
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %28
42: ; preds = %28
%43 = load i8*, i8** %8
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %6
ret i8* %45
}

LLVM IR (-O2):
define i8* @str_chop(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8* %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32 @strlen(i8* %0)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = tail call i8* @xmalloc(i64 %12)
%14 = bitcast i8** %3 to i8*
%15 = tail call i8* @xstrdup(i8* %0)
store i8* %15, i8** %3
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %2, %18
%19 = phi i64 [ %23, %18 ], [ 0, %2 ]
%20 = call i8* @strsep(i8** %3, i8* %1)
%21 = load i8, i8* %20
%22 = getelementptr inbounds i8, i8* %13, i64 %19
store i8 %21, i8* %22
%23 = add nuw i64 %19, 1
%24 = load i8*, i8** %3
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %18
27: ; preds = %18, %2
%28 = call i32 @free(i8* %15)
ret i8* %13
}