#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 *, char *);
int /*<<< orphan*/ reduceSpace(char *);
int /*<<< orphan*/ reverseWordsWithRange(char *, int, int);
int strlen(char *);
void reverseWords(char *s) {
  int length = strlen(s);
  reverseWordsWithRange(s, 0, length);
  int startPos = 0;
  int endPos = 0;
  for (int i = 0; i < length; i++) {
    if (s[i] == ' ' || i == length - 1) {
      endPos = i;
      if (i == length - 1) {
        endPos = length;
      }
      reverseWordsWithRange(s, startPos, (endPos - startPos));
      startPos = endPos + 1;
    }
  }
  printf("%s\n", s);
  reduceSpace(s);
  printf("%s\n", s);
}


x86 assembly (clang, -O0):
<reverseWords>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1060 <reverseWordsWithRange@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f8 <reverseWords+0xb8>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
je     11ad <reverseWords+0x6d>
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11e5 <reverseWords+0xa5>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11ca <reverseWords+0x8a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
sub    -0x10(%rbp),%edx
call   1060 <reverseWordsWithRange@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ea <reverseWords+0xaa>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117b <reverseWords+0x3b>
mov    -0x8(%rbp),%rsi
lea    0xdfd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <reduceSpace@plt>
mov    -0x8(%rbp),%rsi
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverseWords>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebp
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1060 <reverseWordsWithRange@plt>
test   %ebp,%ebp
jle    11b6 <reverseWords+0x76>
mov    %ebp,%r13d
lea    -0x1(%r13),%r15d
xor    %ebx,%ebx
jmp    11a8 <reverseWords+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %rbx,%r15
mov    %r13d,%ebp
cmovne %ebx,%ebp
mov    %ebp,%edx
sub    %r12d,%edx
mov    %r14,%rdi
mov    %r12d,%esi
call   1060 <reverseWordsWithRange@plt>
add    $0x1,%ebp
mov    %ebp,%r12d
add    $0x1,%rbx
cmp    %rbx,%r13
je     11b6 <reverseWords+0x76>
cmpb   $0x20,(%r14,%rbx,1)
je     1180 <reverseWords+0x40>
cmp    %rbx,%r15
jne    119f <reverseWords+0x5f>
jmp    1180 <reverseWords+0x40>
lea    0xe43(%rip),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <reduceSpace@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
define void @reverseWords(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %3
%9 = load i8*, i8** %2
%10 = load i32, i32* %3
%11 = call i32 @reverseWordsWithRange(i8* %9, i32 0, i32 %10)
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %12
12: ; preds = %47, %1
%13 = load i32, i32* %6
%14 = load i32, i32* %3
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %50
16: ; preds = %12
%17 = load i8*, i8** %2
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 32
br i1 %23, label %29, label %24
24: ; preds = %16
%25 = load i32, i32* %6
%26 = load i32, i32* %3
%27 = sub nsw i32 %26, 1
%28 = icmp eq i32 %25, %27
br i1 %28, label %29, label %46
29: ; preds = %24, %16
%30 = load i32, i32* %6
store i32 %30, i32* %5
%31 = load i32, i32* %6
%32 = load i32, i32* %3
%33 = sub nsw i32 %32, 1
%34 = icmp eq i32 %31, %33
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = load i32, i32* %3
store i32 %36, i32* %5
br label %37
37: ; preds = %35, %29
%38 = load i8*, i8** %2
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = load i32, i32* %4
%42 = sub nsw i32 %40, %41
%43 = call i32 @reverseWordsWithRange(i8* %38, i32 %39, i32 %42)
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %46
46: ; preds = %37, %24
br label %47
47: ; preds = %46
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
br label %12
50: ; preds = %12
%51 = load i8*, i8** %2
%52 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %51)
%53 = load i8*, i8** %2
%54 = call i32 @reduceSpace(i8* %53)
%55 = load i8*, i8** %2
%56 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %55)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
define void @reverseWords(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i32 @reverseWordsWithRange(i8* %0, i32 0, i32 %2)
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = add nsw i32 %2, -1
%7 = zext i32 %6 to i64
%8 = zext i32 %2 to i64
br label %13
9: ; preds = %27, %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = tail call i32 @reduceSpace(i8* %0)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
ret void
13: ; preds = %5, %27
%14 = phi i64 [ 0, %5 ], [ %29, %27 ]
%15 = phi i32 [ 0, %5 ], [ %28, %27 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %14
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 32
%19 = icmp eq i64 %14, %7
%20 = select i1 %18, i1 true, i1 %19
br i1 %20, label %21, label %27
21: ; preds = %13
%22 = trunc i64 %14 to i32
%23 = select i1 %19, i32 %2, i32 %22
%24 = sub nsw i32 %23, %15
%25 = tail call i32 @reverseWordsWithRange(i8* %0, i32 %15, i32 %24)
%26 = add nsw i32 %23, 1
br label %27
27: ; preds = %13, %21
%28 = phi i32 [ %26, %21 ], [ %15, %13 ]
%29 = add nuw nsw i64 %14, 1
%30 = icmp eq i64 %29, %8
br i1 %30, label %9, label %13
}