#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 drop_rev_spaces(char const *, int);
int drop_spaces(char const *);
int ft_strlen(char const *);
char *ft_strsub(char const *, int, int);
scalar_t__ malloc(int);
char *ft_strtrim(char const *s) {
  char *str;
  int i;
  int j;
  if (s == NULL)
    return (NULL);
  i = drop_spaces(s);
  j = ft_strlen(s);
  if (i == j) {
    str = (char *)malloc(sizeof(char));
    str[0] = '\0';
    return (str);
  }
  j = drop_rev_spaces(s, j - 1);
  str = ft_strsub(s, i, j - i + 1);
  return (str);
}


x86 assembly (clang, -O0):
<ft_strtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <ft_strtrim+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11ed <ft_strtrim+0x9d>
mov    -0x10(%rbp),%rdi
call   1040 <drop_spaces@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <ft_strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    11ba <ft_strtrim+0x6a>
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ed <ft_strtrim+0x9d>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
sub    $0x1,%esi
call   1030 <drop_rev_spaces@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
sub    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1060 <ft_strsub@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ft_strtrim>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     117d <ft_strtrim+0x2d>
mov    %rdi,%rbx
call   1040 <drop_spaces@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <ft_strlen@plt>
cmp    %eax,%ebp
jne    1186 <ft_strtrim+0x36>
mov    $0x1,%edi
call   1070 <malloc@plt>
movb   $0x0,(%rax)
jmp    117f <ft_strtrim+0x2f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
add    $0xffffffff,%eax
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <drop_rev_spaces@plt>
sub    %ebp,%eax
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <ft_strsub@plt>

LLVM IR (-O0):
define i8* @ft_strtrim(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %37
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @drop_spaces(i8* %11)
store i32 %12, i32* %5
%13 = load i8*, i8** %3
%14 = call i32 @ft_strlen(i8* %13)
store i32 %14, i32* %6
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %24
18: ; preds = %10
%19 = call i64 @malloc(i32 1)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 0, i8* %22
%23 = load i8*, i8** %4
store i8* %23, i8** %2
br label %37
24: ; preds = %10
%25 = load i8*, i8** %3
%26 = load i32, i32* %6
%27 = sub nsw i32 %26, 1
%28 = call i32 @drop_rev_spaces(i8* %25, i32 %27)
store i32 %28, i32* %6
%29 = load i8*, i8** %3
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = load i32, i32* %5
%33 = sub nsw i32 %31, %32
%34 = add nsw i32 %33, 1
%35 = call i8* @ft_strsub(i8* %29, i32 %30, i32 %34)
store i8* %35, i8** %4
%36 = load i8*, i8** %4
store i8* %36, i8** %2
br label %37
37: ; preds = %24, %18, %9
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
define i8* @ft_strtrim(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %16, label %3
3: ; preds = %1
%4 = tail call i32 @drop_spaces(i8* %0)
%5 = tail call i32 @ft_strlen(i8* %0)
%6 = icmp eq i32 %4, %5
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i64 @malloc(i32 1)
%9 = inttoptr i64 %8 to i8*
store i8 0, i8* %9
br label %16
10: ; preds = %3
%11 = add nsw i32 %5, -1
%12 = tail call i32 @drop_rev_spaces(i8* %0, i32 %11)
%13 = sub i32 1, %4
%14 = add i32 %13, %12
%15 = tail call i8* @ft_strsub(i8* %0, i32 %4, i32 %14)
br label %16
16: ; preds = %1, %10, %7
%17 = phi i8* [ %9, %7 ], [ %15, %10 ], [ null, %1 ]
ret i8* %17
}