#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 *realloc(char *, scalar_t__);
scalar_t__ strlen(char *);
char *trim(char *str) {
  char *str_tmp = str;
  char *str_new = str;
  while (*str_tmp == ' ')
    ++str_tmp;
  while (*str_tmp)
    *(str_new++) = *(str_tmp++);
  *str_new = '\0';
  str_new = realloc(str, strlen(str) + 1);
  return str_new;
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    115d <trim+0x3d>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    113c <trim+0x1c>
jmp    1162 <trim+0x42>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1196 <trim+0x76>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1162 <trim+0x42>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %rbx
mov    %rdi,%rbx
mov    %rdi,%rax
nopw   0x0(%rax,%rax,1)
movzbl (%rax),%ecx
add    $0x1,%rax
cmp    $0x20,%cl
je     1130 <trim+0x10>
mov    %rbx,%rdx
test   %cl,%cl
je     1161 <trim+0x41>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %cl,(%rdx)
add    $0x1,%rdx
movzbl (%rax),%ecx
add    $0x1,%rax
test   %cl,%cl
jne    1150 <trim+0x30>
movb   $0x0,(%rdx)
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbx,%rdi
pop    %rbx
jmp    1040 <realloc@plt>

LLVM IR (-O0):
define i8* @trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %3
%6 = load i8*, i8** %2
store i8* %6, i8** %4
br label %7
7: ; preds = %12, %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 32
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %3
br label %7
15: ; preds = %7
br label %16
16: ; preds = %20, %15
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = icmp ne i8 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %3
%23 = load i8, i8* %21
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
store i8 %23, i8* %24
br label %16
26: ; preds = %16
%27 = load i8*, i8** %4
store i8 0, i8* %27
%28 = load i8*, i8** %2
%29 = load i8*, i8** %2
%30 = call i64 @strlen(i8* %29)
%31 = add nsw i64 %30, 1
%32 = call i8* @realloc(i8* %28, i64 %31)
store i8* %32, i8** %4
%33 = load i8*, i8** %4
ret i8* %33
}

LLVM IR (-O2):
define noalias i8* @trim(i8* nocapture %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i8* [ %0, %1 ], [ %5, %2 ]
%4 = load i8, i8* %3
%5 = getelementptr inbounds i8, i8* %3, i64 1
switch i8 %4, label %6 [
i8 32, label %2
i8 0, label %14
]
6: ; preds = %2, %6
%7 = phi i8* [ %11, %6 ], [ %0, %2 ]
%8 = phi i8* [ %10, %6 ], [ %3, %2 ]
%9 = phi i8 [ %12, %6 ], [ %4, %2 ]
%10 = getelementptr inbounds i8, i8* %8, i64 1
%11 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %9, i8* %7
%12 = load i8, i8* %10
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %6
14: ; preds = %2, %6
%15 = phi i8* [ %11, %6 ], [ %0, %2 ]
store i8 0, i8* %15
%16 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%17 = add nsw i64 %16, 1
%18 = tail call i8* @realloc(i8* %0, i64 %17)
ret i8* %18
}