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


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1050 <trim_left@plt>
cmp    $0x0,%rax
jne    1172 <trim+0x42>
movq   $0x0,-0x8(%rbp)
jmp    11bb <trim+0x8b>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
movslq -0x28(%rbp),%rax
add    %rax,%rdx
mov    -0x24(%rbp),%ecx
sub    -0x28(%rbp),%ecx
call   1030 <trim_right@plt>
cmp    $0x0,%rax
jne    11b3 <trim+0x83>
movq   $0x0,-0x8(%rbp)
jmp    11bb <trim+0x8b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <trim_left@plt>
test   %rax,%rax
je     1172 <trim+0x42>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%rdx
sub    %eax,%ebp
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%ecx
call   1030 <trim_right@plt>
test   %rax,%rax
cmove  %rax,%rbx
jmp    1174 <trim+0x44>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
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 i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i8*, i8** %6
%12 = load i8*, i8** %7
%13 = load i8*, i8** %8
%14 = load i32, i32* %9
%15 = call i32* @trim_left(i8* %11, i8* %12, i8* %13, i32 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %4
store i8* null, i8** %5
br label %35
18: ; preds = %4
%19 = load i8*, i8** %8
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %10
%21 = load i8*, i8** %6
%22 = load i8*, i8** %7
%23 = load i8*, i8** %8
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i32, i32* %9
%28 = load i32, i32* %10
%29 = sub nsw i32 %27, %28
%30 = call i32* @trim_right(i8* %21, i8* %22, i8* %26, i32 %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %18
store i8* null, i8** %5
br label %35
33: ; preds = %18
%34 = load i8*, i8** %8
store i8* %34, i8** %5
br label %35
35: ; preds = %33, %32, %17
%36 = load i8*, i8** %5
ret i8* %36
}

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