#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
void reverse_word(char *str, char *sep) {
  int i = 0, length = strlen(str);
  char *p = NULL;
  char *s[100] = {};
  char *q = (char *)malloc(sizeof(char) * length);
  for (p = strtok(str, sep); p; p = strtok(NULL, sep)) {
    s[i++] = p;
  }
  for (i--; i > 0; i--) {
    strcat(q, s[i]);
    strcat(q, sep);
  }
  strcat(q, s[0]);
  strcat(q, "\0");
  strcpy(str, q);
  free(q);
}


x86 assembly (clang, -O0):
<reverse_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x340(%rbp),%rdi
xor    %esi,%esi
mov    $0x320,%edx
call   1060 <memset@plt>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x348(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1212 <reverse_word+0xa2>
mov    -0x20(%rbp),%rcx
mov    -0x14(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
cltq
mov    %rcx,-0x340(%rbp,%rax,8)
ff
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1080 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11d8 <reverse_word+0x68>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    125b <reverse_word+0xeb>
mov    -0x348(%rbp),%rdi
movslq -0x14(%rbp),%rax
mov    -0x340(%rbp,%rax,8),%rsi
ff
call   1090 <strcat@plt>
mov    -0x348(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    121b <reverse_word+0xab>
mov    -0x348(%rbp),%rdi
mov    -0x340(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x348(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x348(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x348(%rbp),%rdi
call   1030 <free@plt>
add    $0x350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_word>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x328,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %rsp,%rdi
mov    $0x320,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     1214 <reverse_word+0xa4>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,(%rsp,%rbp,8)
add    $0x1,%rbp
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
jne    11c0 <reverse_word+0x50>
cmp    $0x2,%ebp
jl     1214 <reverse_word+0xa4>
mov    %ebp,%ebp
add    $0x1,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    -0x2(%rbp),%eax
mov    (%rsp,%rax,8),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
add    $0xffffffffffffffff,%rbp
cmp    $0x2,%rbp
ja     11f0 <reverse_word+0x80>
mov    (%rsp),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
lea    0xdd9(%rip),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
add    $0x328,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] zeroinitializer
define void @reverse_word(i8* %0, i8* %1) {
%3 = alloca [100 x i8*]
%4 = tail call i32 @strlen(i8* %0)
%5 = bitcast [100 x i8*]* %3 to i8*
%6 = tail call i64 @malloc(i32 %4)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i8* @strtok(i8* %0, i8* %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %31, label %15
10: ; preds = %15
%11 = trunc i64 %18 to i32
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = and i64 %18, 4294967295
br label %22
15: ; preds = %2, %15
%16 = phi i64 [ %18, %15 ], [ 0, %2 ]
%17 = phi i8* [ %20, %15 ], [ %8, %2 ]
%18 = add nuw i64 %16, 1
%19 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 %16
store i8* %17, i8** %19
%20 = tail call i8* @strtok(i8* null, i8* %1)
%21 = icmp eq i8* %20, null
br i1 %21, label %10, label %15
22: ; preds = %13, %22
%23 = phi i64 [ %14, %13 ], [ %24, %22 ]
%24 = add nsw i64 %23, -1
%25 = and i64 %24, 4294967295
%26 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 %25
%27 = load i8*, i8** %26
%28 = tail call i32 @strcat(i8* %7, i8* %27)
%29 = tail call i32 @strcat(i8* %7, i8* %1)
%30 = icmp ugt i64 %23, 2
br i1 %30, label %22, label %31
31: ; preds = %22, %2, %10
%32 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 0
%33 = load i8*, i8** %32
%34 = tail call i32 @strcat(i8* %7, i8* %33)
%35 = tail call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%36 = tail call i32 @strcpy(i8* %0, i8* %7)
%37 = tail call i32 @free(i8* %7)
ret void
}