#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 /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char *);
int strspn(char *, char const *);
void normalize_space(char *s) {
  const char *spaces = " \r\n\t";
  int i, n, m = strlen(s) - 1;
  for (i = m; i >= 0; i--) {
    if (isspace((unsigned char)s[i])) {
      s[i] = 0;
    } else {
      break;
    }
  }
  while (*s) {
    n = strspn(s, spaces);
    if (n > 0) {
      *s = ' ';
      if (n > 1) {
        m = strlen(s + n);
        memmove(s + 1, s + n, m + 1);
      }
    }
    s++;
  }
}


x86 assembly (clang, -O0):
<normalize_space>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xead(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11ba <normalize_space+0x7a>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     11a2 <normalize_space+0x62>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a7 <normalize_space+0x67>
jmp    11ba <normalize_space+0x7a>
jmp    11ac <normalize_space+0x6c>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    116c <normalize_space+0x2c>
jmp    11bf <normalize_space+0x7f>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     123e <normalize_space+0xfe>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strspn@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    122d <normalize_space+0xed>
mov    -0x8(%rbp),%rax
movb   $0x20,(%rax)
cmpl   $0x1,-0x18(%rbp)
jle    1228 <normalize_space+0xe8>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1060 <memmove@plt>
jmp    122d <normalize_space+0xed>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11bf <normalize_space+0x7f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalize_space>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    1182 <normalize_space+0x42>
mov    %eax,%r14d
add    $0x1,%r14
nopl   0x0(%rax,%rax,1)
lea    -0x2(%r14),%r15d
movzbl (%rbx,%r15,1),%edi
call   1050 <isspace@plt>
test   %rax,%rax
je     1182 <normalize_space+0x42>
movb   $0x0,(%rbx,%r15,1)
add    $0xffffffffffffffff,%r14
cmp    $0x1,%r14
ja     1160 <normalize_space+0x20>
cmpb   $0x0,(%rbx)
je     11d0 <normalize_space+0x90>
lea    0xe72(%rip),%r14
jmp    119a <normalize_space+0x5a>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     11d0 <normalize_space+0x90>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strspn@plt>
test   %eax,%eax
jle    1190 <normalize_space+0x50>
movb   $0x20,(%rbx)
cmp    $0x1,%eax
je     1190 <normalize_space+0x50>
mov    %eax,%r15d
add    %rbx,%r15
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    0x1(%rbx),%rdi
lea    0x1(%rax),%edx
mov    %r15,%rsi
call   1060 <memmove@plt>
jmp    1190 <normalize_space+0x50>
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c" \0D\0A\09\00"
define void @normalize_space(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %16
4: ; preds = %1
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %4, %14
%7 = phi i64 [ %5, %4 ], [ %8, %14 ]
%8 = add nsw i64 %7, -1
%9 = and i64 %8, 4294967295
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i64 @isspace(i8 zeroext %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %6
store i8 0, i8* %10
%15 = icmp ugt i64 %7, 1
br i1 %15, label %6, label %16
16: ; preds = %14, %6, %1
%17 = load i8, i8* %0
%18 = icmp eq i8 %17, 0
br i1 %18, label %36, label %19
19: ; preds = %16, %32
%20 = phi i8* [ %33, %32 ], [ %0, %16 ]
%21 = tail call i32 @strspn(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %19
store i8 32, i8* %20
%24 = icmp eq i32 %21, 1
br i1 %24, label %32, label %25
25: ; preds = %23
%26 = zext i32 %21 to i64
%27 = getelementptr inbounds i8, i8* %20, i64 %26
%28 = tail call i32 @strlen(i8* %27)
%29 = getelementptr inbounds i8, i8* %20, i64 1
%30 = add nsw i32 %28, 1
%31 = tail call i32 @memmove(i8* %29, i8* %27, i32 %30)
br label %32
32: ; preds = %23, %25, %19
%33 = getelementptr inbounds i8, i8* %20, i64 1
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 0
br i1 %35, label %36, label %19
36: ; preds = %32, %16
ret void
}