#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 *cc_skip_chars(char const *, char const *const);
char *cc_stop_at_chars(char const *, char const *const);
char *malloc(size_t);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *cc_str_trim_new(char const *const restrict str,
                      char const *const restrict chars) {
  if (!str)
    return NULL;
  if (!chars)
    return NULL;
  char const *start = str;
  char const *end = NULL;
  start = cc_skip_chars(start, chars);
  end = cc_stop_at_chars(start, chars);
  if (end == start)
    return NULL;
  size_t len = end - start;
  char *pos = malloc(len + 1);
  if (!pos)
    return NULL;
  strncpy(pos, start, len);
  pos[len] = '\0';
  return pos;
}


x86 assembly (clang, -O0):
<cc_str_trim_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1168 <cc_str_trim_new+0x28>
movq   $0x0,-0x8(%rbp)
jmp    122a <cc_str_trim_new+0xea>
cmpq   $0x0,-0x18(%rbp)
jne    1180 <cc_str_trim_new+0x40>
movq   $0x0,-0x8(%rbp)
jmp    122a <cc_str_trim_new+0xea>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <cc_skip_chars@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <cc_stop_at_chars@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11cd <cc_str_trim_new+0x8d>
movq   $0x0,-0x8(%rbp)
jmp    122a <cc_str_trim_new+0xea>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1205 <cc_str_trim_new+0xc5>
movq   $0x0,-0x8(%rbp)
jmp    122a <cc_str_trim_new+0xea>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cc_str_trim_new>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %r12d,%r12d
test   %rdi,%rdi
je     119f <cc_str_trim_new+0x5f>
mov    %rsi,%rbx
test   %rsi,%rsi
je     119f <cc_str_trim_new+0x5f>
mov    %rbx,%rsi
call   1040 <cc_skip_chars@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <cc_stop_at_chars@plt>
mov    %rax,%r15
sub    %r14,%r15
je     119c <cc_str_trim_new+0x5c>
lea    0x1(%r15),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     119c <cc_str_trim_new+0x5c>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%r12,%r15,1)
jmp    119f <cc_str_trim_new+0x5f>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @cc_str_trim_new(i8* noalias %0, i8* noalias %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i8* null, i8** %3
br label %50
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %13
store i8* null, i8** %3
br label %50
17: ; preds = %13
%18 = load i8*, i8** %4
store i8* %18, i8** %6
store i8* null, i8** %7
%19 = load i8*, i8** %6
%20 = load i8*, i8** %5
%21 = call i8* @cc_skip_chars(i8* %19, i8* %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i8*, i8** %5
%24 = call i8* @cc_stop_at_chars(i8* %22, i8* %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = load i8*, i8** %6
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %29
28: ; preds = %17
store i8* null, i8** %3
br label %50
29: ; preds = %17
%30 = load i8*, i8** %7
%31 = load i8*, i8** %6
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
store i64 %34, i64* %8
%35 = load i64, i64* %8
%36 = add i64 %35, 1
%37 = call i8* @malloc(i64 %36)
store i8* %37, i8** %9
%38 = load i8*, i8** %9
%39 = icmp ne i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %29
store i8* null, i8** %3
br label %50
41: ; preds = %29
%42 = load i8*, i8** %9
%43 = load i8*, i8** %6
%44 = load i64, i64* %8
%45 = call i32 @strncpy(i8* %42, i8* %43, i64 %44)
%46 = load i8*, i8** %9
%47 = load i64, i64* %8
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %9
store i8* %49, i8** %3
br label %50
50: ; preds = %41, %40, %28, %16, %12
%51 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
define i8* @cc_str_trim_new(i8* noalias %0, i8* noalias %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %20, label %6
6: ; preds = %2
%7 = tail call i8* @cc_skip_chars(i8* %0, i8* %1)
%8 = tail call i8* @cc_stop_at_chars(i8* %7, i8* %1)
%9 = icmp eq i8* %8, %7
br i1 %9, label %20, label %10
10: ; preds = %6
%11 = ptrtoint i8* %8 to i64
%12 = ptrtoint i8* %7 to i64
%13 = sub i64 %11, %12
%14 = add i64 %13, 1
%15 = tail call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = tail call i32 @strncpy(i8* %15, i8* %7, i64 %13)
%19 = getelementptr inbounds i8, i8* %15, i64 %13
store i8 0, i8* %19
br label %20
20: ; preds = %6, %10, %17, %2
%21 = phi i8* [ null, %2 ], [ null, %6 ], [ %15, %17 ], [ null, %10 ]
ret i8* %21
}