#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(unsigned char);
int strlen(char *);
char *os_trim(char *str) {
  while (isspace((unsigned char)*str)) {
    str++;
  }
  if (*str != '\0') {
    char *edge = str + strlen(str) - 1;
    while ((edge > str) && isspace((unsigned char)*edge)) {
      *edge = '\0';
      edge--;
    }
  }
  return str;
}


x86 assembly (clang, -O0):
<os_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1153 <os_trim+0x33>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    112c <os_trim+0xc>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11de <os_trim+0xbe>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
jbe    11b1 <os_trim+0x91>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11c1 <os_trim+0xa1>
jmp    11d9 <os_trim+0xb9>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1188 <os_trim+0x68>
jmp    11de <os_trim+0xbe>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_trim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isspace@plt>
test   %rax,%rax
jne    1130 <os_trim+0x10>
cmpb   $0x0,(%rbx)
je     117b <os_trim+0x5b>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%r14
add    $0xffffffffffffffff,%r14
cmp    %rbx,%r14
jbe    117b <os_trim+0x5b>
xchg   %ax,%ax
movzbl (%r14),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     117b <os_trim+0x5b>
movb   $0x0,(%r14)
add    $0xffffffffffffffff,%r14
cmp    %rbx,%r14
ja     1160 <os_trim+0x40>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @os_trim(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
br label %4
4: ; preds = %9, %1
%5 = load i8*, i8** %2
%6 = load i8, i8* %5
%7 = call i64 @isspace(i8 zeroext %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i8*, i8** %2
%11 = getelementptr inbounds i8, i8* %10, i32 1
store i8* %11, i8** %2
br label %4
12: ; preds = %4
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %40
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = load i8*, i8** %2
%20 = call i32 @strlen(i8* %19)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = getelementptr inbounds i8, i8* %22, i64 -1
store i8* %23, i8** %3
br label %24
24: ; preds = %35, %17
%25 = load i8*, i8** %3
%26 = load i8*, i8** %2
%27 = icmp ugt i8* %25, %26
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i8*, i8** %3
%30 = load i8, i8* %29
%31 = call i64 @isspace(i8 zeroext %30)
%32 = icmp ne i64 %31, 0
br label %33
33: ; preds = %28, %24
%34 = phi i1 [ false, %24 ], [ %32, %28 ]
br i1 %34, label %35, label %39
35: ; preds = %33
%36 = load i8*, i8** %3
store i8 0, i8* %36
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i32 -1
store i8* %38, i8** %3
br label %24
39: ; preds = %33
br label %40
40: ; preds = %39, %12
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
define i8* @os_trim(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 @isspace(i8 zeroext %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 %25, label %11
11: ; preds = %8
%12 = tail call i32 @strlen(i8* %3)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %3, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 -1
%16 = icmp ugt i8* %15, %3
br i1 %16, label %17, label %25
17: ; preds = %11, %22
%18 = phi i8* [ %23, %22 ], [ %15, %11 ]
%19 = load i8, i8* %18
%20 = tail call i64 @isspace(i8 zeroext %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17
store i8 0, i8* %18
%23 = getelementptr inbounds i8, i8* %18, i64 -1
%24 = icmp ugt i8* %23, %3
br i1 %24, label %17, label %25
25: ; preds = %22, %17, %11, %8
ret i8* %3
}