#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*/ assert(char *);
scalar_t__ chars_is_white_space(char);
char *strings_empty();
unsigned int strings_length(char *);
char *strings_substring(char *, unsigned int, unsigned int);
char *strings_trim_imp(char *string, int control) {
  assert(string);
  unsigned int length = strings_length(string);
  long start = 0, end = length;
  if (length == 0)
    return strings_empty();
  if (control & 1) {
    while (chars_is_white_space(string[start]) && start < length)
      start++;
  }
  if (control & 2) {
    while (chars_is_white_space(string[end - 1]) && end > start)
      end--;
  }
  return strings_substring(string, (unsigned int)start, (unsigned int)end);
}


x86 assembly (clang, -O0):
<strings_trim_imp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strings_length@plt>
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    119d <strings_trim_imp+0x4d>
mov    $0x0,%al
call   1070 <strings_empty@plt>
mov    %rax,-0x8(%rbp)
jmp    1292 <strings_trim_imp+0x142>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     120a <strings_trim_imp+0xba>
jmp    11b1 <strings_trim_imp+0x61>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <chars_is_white_space@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     11e4 <strings_trim_imp+0x94>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
cmp    %rcx,%rax
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11f4 <strings_trim_imp+0xa4>
jmp    1205 <strings_trim_imp+0xb5>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b1 <strings_trim_imp+0x61>
jmp    120a <strings_trim_imp+0xba>
mov    -0x14(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1279 <strings_trim_imp+0x129>
jmp    121e <strings_trim_imp+0xce>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <chars_is_white_space@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x2a(%rbp)
je     1253 <strings_trim_imp+0x103>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
setg   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    1263 <strings_trim_imp+0x113>
jmp    1274 <strings_trim_imp+0x124>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    121e <strings_trim_imp+0xce>
jmp    1279 <strings_trim_imp+0x129>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1030 <strings_substring@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strings_trim_imp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
call   1060 <assert@plt>
mov    %r14,%rdi
call   1050 <strings_length@plt>
test   %eax,%eax
je     1186 <strings_trim_imp+0x36>
mov    %eax,%r13d
test   $0x1,%r15b
jne    119b <strings_trim_imp+0x4b>
xor    %r12d,%r12d
test   $0x2,%r15b
jne    11d0 <strings_trim_imp+0x80>
jmp    11f3 <strings_trim_imp+0xa3>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <strings_empty@plt>
xor    %ebp,%ebp
nopl   (%rax)
movsbl (%r14,%rbp,1),%edi
call   1040 <chars_is_white_space@plt>
lea    0x1(%rbp),%r12
test   %rax,%rax
je     11bb <strings_trim_imp+0x6b>
cmp    %r13,%rbp
mov    %r12,%rbp
jb     11a0 <strings_trim_imp+0x50>
add    $0xffffffffffffffff,%r12
test   $0x2,%r15b
je     11f3 <strings_trim_imp+0xa3>
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x1(%r13),%rbx
movsbl -0x1(%r14,%r13,1),%edi
call   1040 <chars_is_white_space@plt>
test   %rax,%rax
je     11ec <strings_trim_imp+0x9c>
cmp    %r12,%r13
mov    %rbx,%r13
jg     11d0 <strings_trim_imp+0x80>
add    $0x1,%rbx
mov    %rbx,%r13
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r13d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strings_substring@plt>

LLVM IR (-O0):
define i8* @strings_trim_imp(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i32 @assert(i8* %9)
%11 = load i8*, i8** %4
%12 = call i32 @strings_length(i8* %11)
store i32 %12, i32* %6
store i64 0, i64* %7
%13 = load i32, i32* %6
%14 = zext i32 %13 to i64
store i64 %14, i64* %8
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i8* (...) @strings_empty()
store i8* %18, i8** %3
br label %72
19: ; preds = %2
%20 = load i32, i32* %5
%21 = and i32 %20, 1
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %42
23: ; preds = %19
br label %24
24: ; preds = %38, %23
%25 = load i8*, i8** %4
%26 = load i64, i64* %7
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8, i8* %27
%29 = call i64 @chars_is_white_space(i8 signext %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %24
%32 = load i64, i64* %7
%33 = load i32, i32* %6
%34 = zext i32 %33 to i64
%35 = icmp slt i64 %32, %34
br label %36
36: ; preds = %31, %24
%37 = phi i1 [ false, %24 ], [ %35, %31 ]
br i1 %37, label %38, label %41
38: ; preds = %36
%39 = load i64, i64* %7
%40 = add nsw i64 %39, 1
store i64 %40, i64* %7
br label %24
41: ; preds = %36
br label %42
42: ; preds = %41, %19
%43 = load i32, i32* %5
%44 = and i32 %43, 2
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %65
46: ; preds = %42
br label %47
47: ; preds = %61, %46
%48 = load i8*, i8** %4
%49 = load i64, i64* %8
%50 = sub nsw i64 %49, 1
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = call i64 @chars_is_white_space(i8 signext %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %47
%56 = load i64, i64* %8
%57 = load i64, i64* %7
%58 = icmp sgt i64 %56, %57
br label %59
59: ; preds = %55, %47
%60 = phi i1 [ false, %47 ], [ %58, %55 ]
br i1 %60, label %61, label %64
61: ; preds = %59
%62 = load i64, i64* %8
%63 = add nsw i64 %62, -1
store i64 %63, i64* %8
br label %47
64: ; preds = %59
br label %65
65: ; preds = %64, %42
%66 = load i8*, i8** %4
%67 = load i64, i64* %7
%68 = trunc i64 %67 to i32
%69 = load i64, i64* %8
%70 = trunc i64 %69 to i32
%71 = call i8* @strings_substring(i8* %66, i32 %68, i32 %70)
store i8* %71, i8** %3
br label %72
72: ; preds = %65, %17
%73 = load i8*, i8** %3
ret i8* %73
}

LLVM IR (-O2):
define i8* @strings_trim_imp(i8* %0, i32 %1) {
%3 = tail call i32 @assert(i8* %0)
%4 = tail call i32 @strings_length(i8* %0)
%5 = zext i32 %4 to i64
%6 = icmp eq i32 %4, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i8* (...) @strings_empty()
br label %39
9: ; preds = %2
%10 = and i32 %1, 1
%11 = icmp eq i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %9, %12
%13 = phi i64 [ %20, %12 ], [ 0, %9 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = tail call i64 @chars_is_white_space(i8 signext %15)
%17 = icmp ne i64 %16, 0
%18 = icmp ult i64 %13, %5
%19 = select i1 %17, i1 %18, i1 false
%20 = add nuw nsw i64 %13, 1
br i1 %19, label %12, label %21
21: ; preds = %12, %9
%22 = phi i64 [ 0, %9 ], [ %13, %12 ]
%23 = and i32 %1, 2
%24 = icmp eq i32 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %21, %25
%26 = phi i64 [ %27, %25 ], [ %5, %21 ]
%27 = add nsw i64 %26, -1
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = load i8, i8* %28
%30 = tail call i64 @chars_is_white_space(i8 signext %29)
%31 = icmp ne i64 %30, 0
%32 = icmp sgt i64 %26, %22
%33 = select i1 %31, i1 %32, i1 false
br i1 %33, label %25, label %34
34: ; preds = %25, %21
%35 = phi i64 [ %5, %21 ], [ %26, %25 ]
%36 = trunc i64 %22 to i32
%37 = trunc i64 %35 to i32
%38 = tail call i8* @strings_substring(i8* %0, i32 %36, i32 %37)
br label %39
39: ; preds = %34, %7
%40 = phi i8* [ %8, %7 ], [ %38, %34 ]
ret i8* %40
}