#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 *_cstr_clone(char *);
int /*<<< orphan*/ _extend(char ***, int *);
int /*<<< orphan*/ _inplace_trim(char *);
int /*<<< orphan*/ _itemize(char *, char ***, int *);
int /*<<< orphan*/ _items_cleanup(char **, int);
char *_items_join(char **, int);
scalar_t__ _streq(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
const char *cstr_item_add(const char *in_string, const char *in_item) {
  char **items;
  int count, exists = 0;
  _itemize((char *)in_string, &items, &count);
  for (int i = 0; i < count; i++) {
    if (_streq(_inplace_trim(items[i]), (char *)in_item)) {
      exists = 1;
      break;
    }
  }
  if (!exists) {
    _extend(&items, &count);
    items[count - 1] = _cstr_clone((char *)in_item);
  }
  static char *result = NULL;
  if (result)
    free(result);
  result = _items_join(items, count);
  _items_cleanup(items, count);
  return result;
}


x86 assembly (clang, -O0):
<cstr_item_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1090 <_itemize@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1200 <cstr_item_add+0x80>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <_inplace_trim@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
call   1080 <_streq@plt>
cmp    $0x0,%rax
je     11ed <cstr_item_add+0x6d>
movl   $0x1,-0x20(%rbp)
jmp    1200 <cstr_item_add+0x80>
jmp    11f2 <cstr_item_add+0x72>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11af <cstr_item_add+0x2f>
cmpl   $0x0,-0x20(%rbp)
jne    1234 <cstr_item_add+0xb4>
lea    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1060 <_extend@plt>
mov    -0x10(%rbp),%rdi
call   1050 <_cstr_clone@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
cmpq   $0x0,0x2e2c(%rip)
je     124e <cstr_item_add+0xce>
mov    0x2e1f(%rip),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <_items_join@plt>
mov    %rax,0x2e07(%rip)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   10a0 <_items_cleanup@plt>
mov    0x2df4(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstr_item_add>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1090 <_itemize@plt>
cmpl   $0x0,0xc(%rsp)
jle    11db <cstr_item_add+0x5b>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x10(%rsp),%rax
mov    (%rax,%rbx,8),%rdi
call   1040 <_inplace_trim@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1080 <_streq@plt>
test   %rax,%rax
jne    1201 <cstr_item_add+0x81>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     11b0 <cstr_item_add+0x30>
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1060 <_extend@plt>
mov    %r14,%rdi
call   1050 <_cstr_clone@plt>
mov    0x10(%rsp),%rcx
movslq 0xc(%rsp),%rdx
mov    %rax,-0x8(%rcx,%rdx,8)
mov    0x2e60(%rip),%rdi
test   %rdi,%rdi
je     1212 <cstr_item_add+0x92>
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%esi
call   1070 <_items_join@plt>
mov    %rax,0x2e41(%rip)
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%esi
call   10a0 <_items_cleanup@plt>
mov    0x2e2c(%rip),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@cstr_item_add.result = internal global i8* null
define i8* @cstr_item_add(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %7
%9 = load i8*, i8** %3
%10 = call i32 @_itemize(i8* %9, i8*** %5, i32* %6)
store i32 0, i32* %8
br label %11
11: ; preds = %27, %2
%12 = load i32, i32* %8
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = load i8**, i8*** %5
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %16, i64 %18
%20 = load i8*, i8** %19
%21 = call i32 @_inplace_trim(i8* %20)
%22 = load i8*, i8** %4
%23 = call i64 @_streq(i32 %21, i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %15
store i32 1, i32* %7
br label %30
26: ; preds = %15
br label %27
27: ; preds = %26
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %11
30: ; preds = %25, %11
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %42, label %33
33: ; preds = %30
%34 = call i32 @_extend(i8*** %5, i32* %6)
%35 = load i8*, i8** %4
%36 = call i8* @_cstr_clone(i8* %35)
%37 = load i8**, i8*** %5
%38 = load i32, i32* %6
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %37, i64 %40
store i8* %36, i8** %41
br label %42
42: ; preds = %33, %30
%43 = load i8*, i8** @cstr_item_add.result
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i8*, i8** @cstr_item_add.result
%47 = call i32 @free(i8* %46)
br label %48
48: ; preds = %45, %42
%49 = load i8**, i8*** %5
%50 = load i32, i32* %6
%51 = call i8* @_items_join(i8** %49, i32 %50)
store i8* %51, i8** @cstr_item_add.result
%52 = load i8**, i8*** %5
%53 = load i32, i32* %6
%54 = call i32 @_items_cleanup(i8** %52, i32 %53)
%55 = load i8*, i8** @cstr_item_add.result
ret i8* %55
}

LLVM IR (-O2):
@cstr_item_add.result = internal unnamed_addr global i8* null
define i8* @cstr_item_add(i8* %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = bitcast i8*** %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i32 @_itemize(i8* %0, i8*** %3, i32* %4)
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %14, label %23
10: ; preds = %14
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = icmp slt i64 %22, %12
br i1 %13, label %14, label %23
14: ; preds = %2, %10
%15 = phi i64 [ %22, %10 ], [ 0, %2 ]
%16 = load i8**, i8*** %3
%17 = getelementptr inbounds i8*, i8** %16, i64 %15
%18 = load i8*, i8** %17
%19 = call i32 @_inplace_trim(i8* %18)
%20 = call i64 @_streq(i32 %19, i8* %1)
%21 = icmp eq i64 %20, 0
%22 = add nuw nsw i64 %15, 1
br i1 %21, label %10, label %31
23: ; preds = %10, %2
%24 = call i32 @_extend(i8*** %3, i32* %4)
%25 = call i8* @_cstr_clone(i8* %1)
%26 = load i8**, i8*** %3
%27 = load i32, i32* %4
%28 = add nsw i32 %27, -1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %26, i64 %29
store i8* %25, i8** %30
br label %31
31: ; preds = %14, %23
%32 = load i8*, i8** @cstr_item_add.result
%33 = icmp eq i8* %32, null
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @free(i8* %32)
br label %36
36: ; preds = %34, %31
%37 = load i8**, i8*** %3
%38 = load i32, i32* %4
%39 = call i8* @_items_join(i8** %37, i32 %38)
store i8* %39, i8** @cstr_item_add.result
%40 = load i8**, i8*** %3
%41 = load i32, i32* %4
%42 = call i32 @_items_cleanup(i8** %40, i32 %41)
%43 = load i8*, i8** @cstr_item_add.result
ret i8* %43
}