#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *trim_left(char const *, char const *, char *, int);
int /*<<< orphan*/ *trim_right(char *, char const *, char *, int);
const char *trim(const char *text, const char *needle, char *dest, int size) {
  char tmp[size];
  memset(tmp, 0, size);
  if (trim_left(text, needle, tmp, size) == NULL) {
    return NULL;
  }
  if (trim_right(tmp, needle, dest, size) == NULL) {
    return NULL;
  }
  return dest;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x48(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%ecx
call   1050 <trim_left@plt>
cmp    $0x0,%rax
jne    11a9 <trim+0x79>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11ea <trim+0xba>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1030 <trim_right@plt>
cmp    $0x0,%rax
jne    11db <trim+0xab>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11ea <trim+0xba>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,-0x30(%rbp)
mov    %ecx,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ecx,%edx
call   1040 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r12d,%ecx
call   1050 <trim_left@plt>
test   %rax,%rax
je     119f <trim+0x6f>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r12d,%ecx
call   1030 <trim_right@plt>
test   %rax,%rax
cmove  %rax,%r14
mov    %r14,%r13
mov    %r13,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @trim(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i32, i32* %9
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = load i32, i32* %9
%18 = call i32 @memset(i8* %16, i32 0, i32 %17)
%19 = load i8*, i8** %6
%20 = load i8*, i8** %7
%21 = load i32, i32* %9
%22 = call i32* @trim_left(i8* %19, i8* %20, i8* %16, i32 %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %4
store i8* null, i8** %5
store i32 1, i32* %12
br label %34
25: ; preds = %4
%26 = load i8*, i8** %7
%27 = load i8*, i8** %8
%28 = load i32, i32* %9
%29 = call i32* @trim_right(i8* %16, i8* %26, i8* %27, i32 %28)
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %32
31: ; preds = %25
store i8* null, i8** %5
store i32 1, i32* %12
br label %34
32: ; preds = %25
%33 = load i8*, i8** %8
store i8* %33, i8** %5
store i32 1, i32* %12
br label %34
34: ; preds = %32, %31, %24
%35 = load i8*, i8** %10
%36 = load i8*, i8** %5
ret i8* %36
}

LLVM IR (-O2):
define i8* @trim(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = zext i32 %3 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @memset(i8* %6, i32 0, i32 %3)
%8 = call i32* @trim_left(i8* %0, i8* %1, i8* %6, i32 %3)
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %4
%11 = call i32* @trim_right(i8* %6, i8* %1, i8* %2, i32 %3)
%12 = icmp eq i32* %11, null
%13 = select i1 %12, i8* null, i8* %2
br label %14
14: ; preds = %10, %4
%15 = phi i8* [ null, %4 ], [ %13, %10 ]
ret i8* %15
}