#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 */
size_t next_word_end(char *);
int /*<<< orphan*/ reverse_str(char *, char *);
void reverse_words_2(char *str) {
  size_t next_end = 0;
  size_t curr = 0;
  while (str[curr] != '\0') {
    next_end = next_word_end(&str[curr]);
    reverse_str(&str[curr], &str[curr + next_end]);
    curr += next_end;
    if (str[curr] != '\0') {
      curr++;
    }
  }
  reverse_str(str, &str[curr]);
}


x86 assembly (clang, -O0):
<reverse_words_2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11b0 <reverse_words_2+0x90>
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1040 <next_word_end@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
add    %rax,%rsi
call   1030 <reverse_str@plt>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11ab <reverse_words_2+0x8b>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    113c <reverse_words_2+0x1c>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    -0x18(%rbp),%rsi
call   1030 <reverse_str@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_words_2>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
mov    %rdi,%r15
je     1171 <reverse_words_2+0x51>
xor    %r12d,%r12d
mov    %r14,%r15
nopl   0x0(%rax)
mov    %r15,%rdi
call   1040 <next_word_end@plt>
mov    %rax,%rbx
add    %r12,%rbx
lea    (%r14,%rbx,1),%rsi
mov    %r15,%rdi
call   1030 <reverse_str@plt>
cmpb   $0x1,(%r14,%rbx,1)
sbb    $0xffffffffffffffff,%rbx
lea    (%r14,%rbx,1),%r15
cmpb   $0x0,(%r14,%rbx,1)
mov    %rbx,%r12
jne    1140 <reverse_words_2+0x20>
mov    %r14,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <reverse_str@plt>

LLVM IR (-O0):
define void @reverse_words_2(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %3
store i64 0, i64* %4
br label %5
5: ; preds = %38, %1
%6 = load i8*, i8** %2
%7 = load i64, i64* %4
%8 = getelementptr inbounds i8, i8* %6, i64 %7
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %39
12: ; preds = %5
%13 = load i8*, i8** %2
%14 = load i64, i64* %4
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = call i64 @next_word_end(i8* %15)
store i64 %16, i64* %3
%17 = load i8*, i8** %2
%18 = load i64, i64* %4
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i8*, i8** %2
%21 = load i64, i64* %4
%22 = load i64, i64* %3
%23 = add i64 %21, %22
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = call i32 @reverse_str(i8* %19, i8* %24)
%26 = load i64, i64* %3
%27 = load i64, i64* %4
%28 = add i64 %27, %26
store i64 %28, i64* %4
%29 = load i8*, i8** %2
%30 = load i64, i64* %4
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %12
%36 = load i64, i64* %4
%37 = add i64 %36, 1
store i64 %37, i64* %4
br label %38
38: ; preds = %35, %12
br label %5
39: ; preds = %5
%40 = load i8*, i8** %2
%41 = load i8*, i8** %2
%42 = load i64, i64* %4
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = call i32 @reverse_str(i8* %40, i8* %43)
ret void
}

LLVM IR (-O2):
define void @reverse_words_2(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %18, label %4
4: ; preds = %1, %4
%5 = phi i8* [ %15, %4 ], [ %0, %1 ]
%6 = phi i64 [ %14, %4 ], [ 0, %1 ]
%7 = tail call i64 @next_word_end(i8* %5)
%8 = add i64 %7, %6
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = tail call i32 @reverse_str(i8* %5, i8* %9)
%11 = load i8, i8* %9
%12 = icmp ne i8 %11, 0
%13 = zext i1 %12 to i64
%14 = add i64 %8, %13
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %4
18: ; preds = %4, %1
%19 = phi i8* [ %0, %1 ], [ %15, %4 ]
%20 = tail call i32 @reverse_str(i8* %0, i8* %19)
ret void
}