#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*/ printf(char *, int);
int strlen(char *);
int /*<<< orphan*/ system(char *);
int trimSample(void) {
  char *p = "      abcdefg      ";
  int begin = 0;
  int end = strlen(p) - 1;
  printf("first begin = %d \n", begin);
  printf("first end = %d \n", end);
  int n = 0;
  while (p[begin] == ' ' && p[begin] != 0) {
    begin++;
  }
  printf("last begin = %d \n", begin);
  while (p[end] == ' ' && p[end] != 0) {
    end--;
  }
  printf("last end = %d \n", end);
  n = end - begin + 1;
  printf("n = %d\n", n);
  system("pause");
  return 0;
}


x86 assembly (clang, -O0):
<trimSample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rax
mov    %rax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%esi
lea    0xeb1(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xeb5(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x20,%ecx
mov    %al,-0x15(%rbp)
jne    11ad <trimSample+0x7d>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11bd <trimSample+0x8d>
jmp    11cb <trimSample+0x9b>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117e <trimSample+0x4e>
mov    -0xc(%rbp),%esi
lea    0xe63(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x20,%ecx
mov    %al,-0x16(%rbp)
jne    1209 <trimSample+0xd9>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    1219 <trimSample+0xe9>
jmp    1227 <trimSample+0xf7>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11da <trimSample+0xaa>
mov    -0x10(%rbp),%esi
lea    0xe19(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1050 <printf@plt>
lea    0xe0a(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trimSample>:
push   %rbp
push   %rbx
push   %rax
lea    0xec6(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
lea    -0x1(%rbx),%ebp
lea    0xec9(%rip),%rdi
xor    %esi,%esi
call   1050 <printf@plt>
lea    0xece(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
lea    0xed1(%rip),%rdi
mov    $0x6,%esi
call   1050 <printf@plt>
mov    $0x7e03f,%eax
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
bt     %rbx,%rax
jb     1180 <trimSample+0x50>
lea    0xeba(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
add    $0xfffffffb,%ebx
lea    0xeb9(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
lea    0xeb3(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c" abcdefg \00"
@.str.1 = [19 x i8] c"first begin = %d \0A\00"
@.str.2 = [17 x i8] c"first end = %d \0A\00"
@.str.3 = [18 x i8] c"last begin = %d \0A\00"
@.str.4 = [16 x i8] c"last end = %d \0A\00"
@.str.5 = [8 x i8] c"n = %d\0A\00"
@.str.6 = [6 x i8] c"pause\00"
define i32 @trimSample() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %1
store i32 0, i32* %2
%5 = load i8*, i8** %1
%6 = call i32 @strlen(i8* %5)
%7 = sub nsw i32 %6, 1
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* %3
%11 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %10)
store i32 0, i32* %4
br label %12
12: ; preds = %30, %0
%13 = load i8*, i8** %1
%14 = load i32, i32* %2
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 32
br i1 %19, label %20, label %28
20: ; preds = %12
%21 = load i8*, i8** %1
%22 = load i32, i32* %2
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br label %28
28: ; preds = %20, %12
%29 = phi i1 [ false, %12 ], [ %27, %20 ]
br i1 %29, label %30, label %33
30: ; preds = %28
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
br label %12
33: ; preds = %28
%34 = load i32, i32* %2
%35 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %54, %33
%37 = load i8*, i8** %1
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 32
br i1 %43, label %44, label %52
44: ; preds = %36
%45 = load i8*, i8** %1
%46 = load i32, i32* %3
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 0
br label %52
52: ; preds = %44, %36
%53 = phi i1 [ false, %36 ], [ %51, %44 ]
br i1 %53, label %54, label %57
54: ; preds = %52
%55 = load i32, i32* %3
%56 = add nsw i32 %55, -1
store i32 %56, i32* %3
br label %36
57: ; preds = %52
%58 = load i32, i32* %3
%59 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %3
%61 = load i32, i32* %2
%62 = sub nsw i32 %60, %61
%63 = add nsw i32 %62, 1
store i32 %63, i32* %4
%64 = load i32, i32* %4
%65 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %64)
%66 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c" abcdefg \00"
@.str.1 = [19 x i8] c"first begin = %d \0A\00"
@.str.2 = [17 x i8] c"first end = %d \0A\00"
@.str.3 = [18 x i8] c"last begin = %d \0A\00"
@.str.4 = [16 x i8] c"last end = %d \0A\00"
@.str.5 = [8 x i8] c"n = %d\0A\00"
@.str.6 = [6 x i8] c"pause\00"
define i32 @trimSample() {
%1 = tail call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%2 = add nsw i32 %1, -1
%3 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 0)
%4 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 6)
br label %6
6: ; preds = %6, %0
%7 = phi i32 [ %2, %0 ], [ %12, %6 ]
%8 = zext i32 %7 to i64
%9 = lshr i64 516159, %8
%10 = and i64 %9, 1
%11 = icmp eq i64 %10, 0
%12 = add nsw i32 %7, -1
br i1 %11, label %13, label %6
13: ; preds = %6
%14 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %7)
%15 = add i32 %7, -5
%16 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %15)
%17 = tail call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}