#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 *);
scalar_t__ isspace(char);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, char, size_t);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
void trim(char *str) {
  if (!str) {
    printf("Inner: Null string when calling trim(..) function.\n");
    return;
  }
  if (strlen(str) <= 1)
    return;
  int i = strlen(str) - 1;
  while (i > 0 && isspace(str[i])) {
    i--;
  }
  str[i + 1] = '\0';
  char *capacity = (char *)malloc(sizeof(char) * (1 + strlen(str)));
  strcpy(capacity, str);
  capacity[strlen(str)] = '\0';
  char *p = capacity;
  while (isspace(*p))
    p++;
  memset(str, '\0', strlen(str));
  strcpy(str, p);
  free(capacity);
}


x86 assembly (clang, -O0):
<trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1198 <trim+0x28>
lea    0xe72(%rip),%rdi
call   1060 <printf@plt>
jmp    12c2 <trim+0x152>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%rax
ja     11b0 <trim+0x40>
jmp    12c2 <trim+0x152>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%rax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x21(%rbp)
jle    11ea <trim+0x7a>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1080 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11fa <trim+0x8a>
jmp    1208 <trim+0x98>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    11c0 <trim+0x50>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1080 <isspace@plt>
cmp    $0x0,%rax
je     128d <trim+0x11d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1266 <trim+0xf6>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     118e <trim+0x1e>
mov    %rdi,%r15
call   1060 <strlen@plt>
cmp    $0x2,%rax
jae    119f <trim+0x2f>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe6b(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <puts@plt>
lea    -0x1(%rax),%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%r14d
test   %ebx,%ebx
jle    11d1 <trim+0x61>
movsbl (%r15,%rbx,1),%edi
call   1080 <isspace@plt>
mov    %rax,%rcx
add    $0xffffffffffffffff,%rbx
lea    -0x1(%r14),%eax
test   %rcx,%rcx
jne    11b0 <trim+0x40>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rbx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1080 <isspace@plt>
test   %rax,%rax
jne    1210 <trim+0xa0>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %rax,%rdx
call   1070 <memset@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [52 x i8] c"Inner: Null string when calling trim(..) function.\0A\00"
define void @trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
br label %75
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i64 @strlen(i8* %11)
%13 = icmp ule i64 %12, 1
br i1 %13, label %14, label %15
14: ; preds = %10
br label %75
15: ; preds = %10
%16 = load i8*, i8** %2
%17 = call i64 @strlen(i8* %16)
%18 = sub i64 %17, 1
%19 = trunc i64 %18 to i32
store i32 %19, i32* %3
br label %20
20: ; preds = %33, %15
%21 = load i32, i32* %3
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i8*, i8** %2
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = call i64 @isspace(i8 signext %28)
%30 = icmp ne i64 %29, 0
br label %31
31: ; preds = %23, %20
%32 = phi i1 [ false, %20 ], [ %30, %23 ]
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = load i32, i32* %3
%35 = add nsw i32 %34, -1
store i32 %35, i32* %3
br label %20
36: ; preds = %31
%37 = load i8*, i8** %2
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %2
%43 = call i64 @strlen(i8* %42)
%44 = add i64 1, %43
%45 = mul i64 1, %44
%46 = trunc i64 %45 to i32
%47 = call i64 @malloc(i32 %46)
%48 = inttoptr i64 %47 to i8*
store i8* %48, i8** %4
%49 = load i8*, i8** %4
%50 = load i8*, i8** %2
%51 = call i32 @strcpy(i8* %49, i8* %50)
%52 = load i8*, i8** %4
%53 = load i8*, i8** %2
%54 = call i64 @strlen(i8* %53)
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
%56 = load i8*, i8** %4
store i8* %56, i8** %5
br label %57
57: ; preds = %62, %36
%58 = load i8*, i8** %5
%59 = load i8, i8* %58
%60 = call i64 @isspace(i8 signext %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i8*, i8** %5
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %5
br label %57
65: ; preds = %57
%66 = load i8*, i8** %2
%67 = load i8*, i8** %2
%68 = call i64 @strlen(i8* %67)
%69 = call i32 @memset(i8* %66, i8 signext 0, i64 %68)
%70 = load i8*, i8** %2
%71 = load i8*, i8** %5
%72 = call i32 @strcpy(i8* %70, i8* %71)
%73 = load i8*, i8** %4
%74 = call i32 @free(i8* %73)
br label %75
75: ; preds = %65, %14, %8
ret void
}

LLVM IR (-O2):
@str = [51 x i8] c"Inner: Null string when calling trim(..) function.\00"
define void @trim(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %42
5: ; preds = %1
%6 = tail call i64 @strlen(i8* %0)
%7 = icmp ult i64 %6, 2
br i1 %7, label %42, label %8
8: ; preds = %5
%9 = trunc i64 %6 to i32
br label %10
10: ; preds = %14, %8
%11 = phi i32 [ %9, %8 ], [ %12, %14 ]
%12 = add i32 %11, -1
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = zext i32 %12 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = tail call i64 @isspace(i8 signext %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %10
20: ; preds = %10, %14
%21 = sext i32 %11 to i64
%22 = getelementptr inbounds i8, i8* %0, i64 %21
store i8 0, i8* %22
%23 = tail call i64 @strlen(i8* %0)
%24 = trunc i64 %23 to i32
%25 = add i32 %24, 1
%26 = tail call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = tail call i32 @strcpy(i8* %27, i8* %0)
%29 = tail call i64 @strlen(i8* %0)
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %31, %20
%32 = phi i8* [ %27, %20 ], [ %36, %31 ]
%33 = load i8, i8* %32
%34 = tail call i64 @isspace(i8 signext %33)
%35 = icmp eq i64 %34, 0
%36 = getelementptr inbounds i8, i8* %32, i64 1
br i1 %35, label %37, label %31
37: ; preds = %31
%38 = tail call i64 @strlen(i8* %0)
%39 = tail call i32 @memset(i8* %0, i8 signext 0, i64 %38)
%40 = tail call i32 @strcpy(i8* %0, i8* %32)
%41 = tail call i32 @free(i8* %27)
br label %42
42: ; preds = %5, %37, %3
ret void
}