#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
size_t next_word_end(char *);
size_t sprintf(char *, char *, char *);
char *strndup(char *, size_t);
size_t reverse_words_aux(char *str, size_t curr) {
  size_t next_end = next_word_end(&str[curr]);
  if (str[curr + next_end] == '\0') {
    return sprintf(str, "%s", &str[curr]);
  }
  char *word = strndup(&str[curr], next_end);
  assert(word != NULL);
  curr = reverse_words_aux(str, curr + next_end + 1);
  size_t ret = curr + sprintf(str + curr, " %s", word);
  free(word);
  return ret;
}


x86 assembly (clang, -O0):
<reverse_words_aux>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1080 <next_word_end@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11bb <reverse_words_aux+0x5b>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rdx
add    -0x18(%rbp),%rdx
lea    0xe54(%rip),%rsi
call   1070 <sprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    123a <reverse_words_aux+0xda>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strndup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1060 <reverse_words_aux@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdea(%rip),%rsi
call   1070 <sprintf@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_words_aux>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    (%rdi,%rsi,1),%r15
mov    %r15,%rdi
call   1080 <next_word_end@plt>
add    %rax,%rbx
cmpb   $0x0,(%r14,%rbx,1)
je     11e4 <reverse_words_aux+0x84>
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <strndup@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
add    $0x1,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <reverse_words_aux@plt>
mov    %rax,%r12
add    %rax,%r14
lea    0xe44(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
call   1070 <sprintf@plt>
mov    %rax,%rbx
add    %r12,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe16(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <sprintf@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c" %s\00"
define i64 @reverse_words_aux(i8* %0, i64 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 %1
%4 = tail call i64 @next_word_end(i8* %3)
%5 = add i64 %4, %1
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = tail call i64 @sprintf(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %11
11: ; preds = %9, %13
%12 = phi i64 [ %22, %13 ], [ %10, %9 ]
ret i64 %12
13: ; preds = %2
%14 = tail call i8* @strndup(i8* %3, i64 %4)
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = add i64 %5, 1
%19 = tail call i64 @reverse_words_aux(i8* %0, i64 %18)
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = tail call i64 @sprintf(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%22 = add i64 %21, %19
%23 = tail call i32 @free(i8* %14)
br label %11
}