#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 */
scalar_t__ isspace(char);
int /*<<< orphan*/ perror(char *);
char *str_sub(char *const, size_t, size_t);
size_t strlen(char *const);
char *str_trim(char *const s) {
  size_t i = 0;
  while (isspace(s[i])) {
    i++;
  }
  size_t j = strlen(s);
  while (isspace(s[j - 1])) {
    j--;
  }
  if (j != 0) {
    j -= i;
  }
  char *str_new = str_sub(s, i, j);
  if (!str_new) {
    perror("str_trim: str_sub returned NULL.");
  }
  return (str_new);
}


x86 assembly (clang, -O0):
<str_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1180 <str_trim+0x40>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1154 <str_trim+0x14>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11bd <str_trim+0x7d>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    118d <str_trim+0x4d>
cmpq   $0x0,-0x18(%rbp)
je     11d7 <str_trim+0x97>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <str_sub@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <str_trim+0xc3>
lea    0xe02(%rip),%rdi
call   1050 <perror@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_trim>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %r12d,%r12d
xchg   %ax,%ax
mov    %r12,%r15
movsbl (%r14,%r12,1),%edi
call   1040 <isspace@plt>
add    $0x1,%r12
test   %rax,%rax
jne    1150 <str_trim+0x10>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
add    $0x2,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movsbl -0x3(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    1180 <str_trim+0x40>
mov    %rbx,%rax
sub    %r12,%rax
xor    %edx,%edx
cmp    $0x1,%rbx
cmovne %rax,%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <str_sub@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c6 <str_trim+0x86>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe33(%rip),%rdi
call   1050 <perror@plt>
jmp    11b7 <str_trim+0x77>

LLVM IR (-O0):
@.str = [33 x i8] c"str_trim: str_sub returned NULL.\00"
define i8* @str_trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* %3
br label %6
6: ; preds = %13, %1
%7 = load i8*, i8** %2
%8 = load i64, i64* %3
%9 = getelementptr inbounds i8, i8* %7, i64 %8
%10 = load i8, i8* %9
%11 = call i64 @isspace(i8 signext %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = load i64, i64* %3
%15 = add i64 %14, 1
store i64 %15, i64* %3
br label %6
16: ; preds = %6
%17 = load i8*, i8** %2
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %4
br label %19
19: ; preds = %27, %16
%20 = load i8*, i8** %2
%21 = load i64, i64* %4
%22 = sub i64 %21, 1
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = call i64 @isspace(i8 signext %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i64, i64* %4
%29 = add i64 %28, -1
store i64 %29, i64* %4
br label %19
30: ; preds = %19
%31 = load i64, i64* %4
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i64, i64* %3
%35 = load i64, i64* %4
%36 = sub i64 %35, %34
store i64 %36, i64* %4
br label %37
37: ; preds = %33, %30
%38 = load i8*, i8** %2
%39 = load i64, i64* %3
%40 = load i64, i64* %4
%41 = call i8* @str_sub(i8* %38, i64 %39, i64 %40)
store i8* %41, i8** %5
%42 = load i8*, i8** %5
%43 = icmp ne i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %44, %37
%47 = load i8*, i8** %5
ret i8* %47
}

LLVM IR (-O2):
@.str = [33 x i8] c"str_trim: str_sub returned NULL.\00"
define i8* @str_trim(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ 0, %1 ], [ %8, %2 ]
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
%6 = tail call i64 @isspace(i8 signext %5)
%7 = icmp eq i64 %6, 0
%8 = add i64 %3, 1
br i1 %7, label %9, label %2
9: ; preds = %2
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
br label %11
11: ; preds = %11, %9
%12 = phi i64 [ %10, %9 ], [ %13, %11 ]
%13 = add i64 %12, -1
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = tail call i64 @isspace(i8 signext %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %11
18: ; preds = %11
%19 = icmp eq i64 %12, 0
%20 = sub i64 %12, %3
%21 = select i1 %19, i64 0, i64 %20
%22 = tail call i8* @str_sub(i8* %0, i64 %3, i64 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
ret i8* %22
}