#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 *ft_strchr(char const *, char);
char *ft_strdup(char *);
char *ft_strsub(char const *, int /*<<< orphan*/, int);
scalar_t__ is_useless(char const);
char *ft_strtrim(const char *s) {
  const char *temp;
  while (is_useless(*s))
    s++;
  if (!*s)
    return (ft_strdup(""));
  temp = ft_strchr(s, '\0') - 1;
  while (is_useless(*temp))
    temp--;
  return (ft_strsub(s, 0, temp - s + 1));
}


x86 assembly (clang, -O0):
<ft_strtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <is_useless@plt>
cmp    $0x0,%rax
je     1173 <ft_strtrim+0x33>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    114c <ft_strtrim+0xc>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1195 <ft_strtrim+0x55>
lea    0xe79(%rip),%rdi
call   1050 <ft_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11ef <ft_strtrim+0xaf>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <ft_strchr@plt>
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <is_useless@plt>
cmp    $0x0,%rax
je     11cf <ft_strtrim+0x8f>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11a8 <ft_strtrim+0x68>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <ft_strsub@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ft_strtrim>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
lea    -0x2(%rdi),%ebx
add    $0xffffffffffffffff,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl 0x1(%r15),%edi
add    $0x1,%r15
call   1030 <is_useless@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1160 <ft_strtrim+0x20>
cmpb   $0x0,(%r15)
je     11cd <ft_strtrim+0x8d>
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <ft_strchr@plt>
mov    %rax,%rbp
mov    %ebp,%r14d
sub    %ebx,%r14d
add    $0xffffffffffffffff,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0x0(%rbp),%edi
call   1030 <is_useless@plt>
add    $0xffffffff,%r14d
add    $0xffffffffffffffff,%rbp
test   %rax,%rax
jne    11a0 <ft_strtrim+0x60>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <ft_strsub@plt>
lea    0xe2c(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <ft_strdup@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @ft_strtrim(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
br label %5
5: ; preds = %10, %1
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = call i64 @is_useless(i8 signext %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %3
br label %5
13: ; preds = %5
%14 = load i8*, i8** %3
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = call i8* @ft_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %2
br label %41
19: ; preds = %13
%20 = load i8*, i8** %3
%21 = call i8* @ft_strchr(i8* %20, i8 signext 0)
%22 = getelementptr inbounds i8, i8* %21, i64 -1
store i8* %22, i8** %4
br label %23
23: ; preds = %28, %19
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = call i64 @is_useless(i8 signext %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %4
br label %23
31: ; preds = %23
%32 = load i8*, i8** %3
%33 = load i8*, i8** %4
%34 = load i8*, i8** %3
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = add nsw i64 %37, 1
%39 = trunc i64 %38 to i32
%40 = call i8* @ft_strsub(i8* %32, i32 0, i32 %39)
store i8* %40, i8** %2
br label %41
41: ; preds = %31, %17
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @ft_strtrim(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i8* [ %0, %1 ], [ %7, %2 ]
%4 = load i8, i8* %3
%5 = tail call i64 @is_useless(i8 signext %4)
%6 = icmp eq i64 %5, 0
%7 = getelementptr inbounds i8, i8* %3, i64 1
br i1 %6, label %8, label %2
8: ; preds = %2
%9 = load i8, i8* %3
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i8* @ft_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %28
13: ; preds = %8
%14 = tail call i8* @ft_strchr(i8* %3, i8 signext 0)
br label %15
15: ; preds = %15, %13
%16 = phi i8* [ %14, %13 ], [ %17, %15 ]
%17 = getelementptr inbounds i8, i8* %16, i64 -1
%18 = load i8, i8* %17
%19 = tail call i64 @is_useless(i8 signext %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %15
21: ; preds = %15
%22 = ptrtoint i8* %17 to i64
%23 = ptrtoint i8* %3 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = add i32 %25, 1
%27 = tail call i8* @ft_strsub(i8* %3, i32 0, i32 %26)
br label %28
28: ; preds = %21, %11
%29 = phi i8* [ %27, %21 ], [ %12, %11 ]
ret i8* %29
}