#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*/ free(char *);
char *ltrim(char *, size_t);
char *rtrim(char *, size_t);
char *strdup(char const *);
size_t strlen(char const *);
char *trim(const char *string) {
  size_t len = strlen(string);
  if (!len)
    return NULL;
  char *t_string = strdup(string);
  t_string = ltrim(t_string, len);
  t_string = rtrim(t_string, len);
  len = strlen(t_string);
  if (!len) {
    free(t_string);
    return NULL;
  }
  return t_string;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <trim+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11e6 <trim+0x96>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <ltrim@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <rtrim@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11de <trim+0x8e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e6 <trim+0x96>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %rax,%rax
je     118f <trim+0x3f>
mov    %rax,%rbx
mov    %r14,%rdi
call   1070 <strdup@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <ltrim@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <rtrim@plt>
cmpb   $0x0,(%rax)
jne    1191 <trim+0x41>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp ne i64 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %29
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = load i64, i64* %4
%16 = call i8* @ltrim(i8* %14, i64 %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = load i64, i64* %4
%19 = call i8* @rtrim(i8* %17, i64 %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %4
%22 = load i64, i64* %4
%23 = icmp ne i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %11
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
store i8* null, i8** %2
br label %29
27: ; preds = %11
%28 = load i8*, i8** %5
store i8* %28, i8** %2
br label %29
29: ; preds = %27, %24, %10
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
define i8* @trim(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i8* @ltrim(i8* %5, i64 %2)
%7 = tail call i8* @rtrim(i8* %6, i64 %2)
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @free(i8* %7)
br label %12
12: ; preds = %10, %4, %1
%13 = phi i8* [ null, %1 ], [ null, %10 ], [ %7, %4 ]
ret i8* %13
}