#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 strlen(char *);
int mouse_buf_trim(char *buf) {
  int i;
  ;
  while (buf[0] == ' ') {
    int len = strlen(buf);
    for (i = 0; i < len - 1; i++)
      buf[i] = buf[i + 1];
  }
  while (buf[strlen(buf) - 1] == ' ' || buf[strlen(buf) - 1] == '\n')
    buf[strlen(buf) - 1] = '\0';
  return 42;
}


x86 assembly (clang, -O0):
<mouse_buf_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    117e <mouse_buf_trim+0x6e>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1179 <mouse_buf_trim+0x69>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113f <mouse_buf_trim+0x2f>
jmp    111c <mouse_buf_trim+0xc>
jmp    1183 <mouse_buf_trim+0x73>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x11(%rbp)
je     11dc <mouse_buf_trim+0xcc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    11ec <mouse_buf_trim+0xdc>
jmp    1212 <mouse_buf_trim+0x102>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1183 <mouse_buf_trim+0x73>
mov    $0x2a,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mouse_buf_trim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x20,(%rdi)
jne    117f <mouse_buf_trim+0x5f>
lea    0x1(%rbx),%r14
jmp    1145 <mouse_buf_trim+0x25>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpb   $0x20,(%rbx)
jne    117f <mouse_buf_trim+0x5f>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x2,%eax
jl     1140 <mouse_buf_trim+0x20>
add    $0xffffffff,%eax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <memmove@plt>
jmp    1140 <mouse_buf_trim+0x20>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0x20,-0x1(%rax,%rbx,1)
je     1170 <mouse_buf_trim+0x50>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     1170 <mouse_buf_trim+0x50>
mov    $0x2a,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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