#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);
int /*<<< orphan*/ strncpy(char *, char *, int);
void replace_spaces(char *str) {
  int len = 0, num_spaces = 0;
  for (; str[len] != 0; len++) {
    if (str[len] == ' ')
      num_spaces++;
  }
  assert(num_spaces / 3 != 0);
  int trailing = num_spaces / 3 * 2;
  int j = len - 1;
  for (int i = len - 1 - trailing; i > 0; i--) {
    if (str[i] == ' ') {
      strncpy(str + j - 2, "%20", 3);
      j -= 3;
    } else {
      str[j] = str[i];
      j--;
    }
  }
}


x86 assembly (clang, -O0):
<replace_spaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1180 <replace_spaces+0x60>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    116d <replace_spaces+0x4d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1172 <replace_spaces+0x52>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113a <replace_spaces+0x1a>
mov    -0x10(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1240 <replace_spaces+0x120>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    120e <replace_spaces+0xee>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xe0a(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x18(%rbp)
jmp    122d <replace_spaces+0x10d>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1232 <replace_spaces+0x112>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c1 <replace_spaces+0xa1>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_spaces>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %eax,%eax
mov    $0xffffffff,%r13d
mov    %rdi,%rcx
jmp    114b <replace_spaces+0x2b>
nopl   0x0(%rax,%rax,1)
add    $0x1,%eax
add    $0x1,%r13d
add    $0x1,%rcx
movzbl (%rcx),%edx
cmp    $0x20,%dl
je     1140 <replace_spaces+0x20>
test   %dl,%dl
jne    1143 <replace_spaces+0x23>
movslq %eax,%rcx
imul   $0x55555555,%rcx,%rbp
shr    $0x20,%rbp
sub    %eax,%ebp
mov    %ebp,%ecx
shr    $0x1f,%ecx
shr    %ebp
add    %ecx,%ebp
add    $0xfffffffd,%eax
xor    %edi,%edi
cmp    $0xfffffffb,%eax
setb   %dil
call   1040 <assert@plt>
lea    0x0(%r13,%rbp,2),%eax
test   %eax,%eax
jle    11e9 <replace_spaces+0xc9>
add    %ebp,%ebp
lea    -0x2(%r15),%r12
mov    %ebp,%ebx
add    %r13d,%ebx
add    %r13d,%ebp
add    $0x1,%ebp
add    %r15,%rbx
lea    0xe5b(%rip),%r14
jmp    11c8 <replace_spaces+0xa8>
nopw   0x0(%rax,%rax,1)
mov    %al,(%r15,%rdi,1)
mov    $0xffffffff,%eax
add    %eax,%r13d
add    $0xffffffff,%ebp
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%ebp
jle    11e9 <replace_spaces+0xc9>
movzbl (%rbx),%eax
movslq %r13d,%rdi
cmp    $0x20,%al
jne    11b0 <replace_spaces+0x90>
add    %r12,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    $0xfffffffd,%eax
jmp    11b9 <replace_spaces+0x99>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%20\00"
define void @replace_spaces(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %8
8: ; preds = %28, %1
%9 = load i8*, i8** %2
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %8
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%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 %24, label %27
24: ; preds = %16
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %27
27: ; preds = %24, %16
br label %28
28: ; preds = %27
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %8
31: ; preds = %8
%32 = load i32, i32* %4
%33 = sdiv i32 %32, 3
%34 = icmp ne i32 %33, 0
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i32, i32* %4
%38 = sdiv i32 %37, 3
%39 = mul nsw i32 %38, 2
store i32 %39, i32* %5
%40 = load i32, i32* %3
%41 = sub nsw i32 %40, 1
store i32 %41, i32* %6
%42 = load i32, i32* %3
%43 = sub nsw i32 %42, 1
%44 = load i32, i32* %5
%45 = sub nsw i32 %43, %44
store i32 %45, i32* %7
br label %46
46: ; preds = %79, %31
%47 = load i32, i32* %7
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %82
49: ; preds = %46
%50 = load i8*, i8** %2
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 32
br i1 %56, label %57, label %66
57: ; preds = %49
%58 = load i8*, i8** %2
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = getelementptr inbounds i8, i8* %61, i64 -2
%63 = call i32 @strncpy(i8* %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%64 = load i32, i32* %6
%65 = sub nsw i32 %64, 3
store i32 %65, i32* %6
br label %78
66: ; preds = %49
%67 = load i8*, i8** %2
%68 = load i32, i32* %7
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
%71 = load i8, i8* %70
%72 = load i8*, i8** %2
%73 = load i32, i32* %6
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %72, i64 %74
store i8 %71, i8* %75
%76 = load i32, i32* %6
%77 = add nsw i32 %76, -1
store i32 %77, i32* %6
br label %78
78: ; preds = %66, %57
br label %79
79: ; preds = %78
%80 = load i32, i32* %7
%81 = add nsw i32 %80, -1
store i32 %81, i32* %7
br label %46
82: ; preds = %46
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%20\00"
define void @replace_spaces(i8* %0) {
br label %2
2: ; preds = %9, %1
%3 = phi i64 [ %11, %9 ], [ 0, %1 ]
%4 = phi i32 [ %10, %9 ], [ 0, %1 ]
%5 = getelementptr inbounds i8, i8* %0, i64 %3
%6 = load i8, i8* %5
switch i8 %6, label %9 [
i8 0, label %12
i8 32, label %7
]
7: ; preds = %2
%8 = add nsw i32 %4, 1
br label %9
9: ; preds = %2, %7
%10 = phi i32 [ %8, %7 ], [ %4, %2 ]
%11 = add nuw i64 %3, 1
br label %2
12: ; preds = %2
%13 = trunc i64 %3 to i32
%14 = sdiv i32 %4, -3
%15 = add i32 %4, -3
%16 = icmp ult i32 %15, -5
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = shl nsw i32 %14, 1
%20 = add nsw i32 %13, -1
%21 = add i32 %19, %20
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = zext i32 %21 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 -2
br label %27
26: ; preds = %39, %12
ret void
27: ; preds = %23, %39
%28 = phi i64 [ %24, %23 ], [ %42, %39 ]
%29 = phi i32 [ %20, %23 ], [ %41, %39 ]
%30 = getelementptr inbounds i8, i8* %0, i64 %28
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 32
%33 = sext i32 %29 to i64
br i1 %32, label %34, label %37
34: ; preds = %27
%35 = getelementptr inbounds i8, i8* %25, i64 %33
%36 = tail call i32 @strncpy(i8* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
br label %39
37: ; preds = %27
%38 = getelementptr inbounds i8, i8* %0, i64 %33
store i8 %31, i8* %38
br label %39
39: ; preds = %34, %37
%40 = phi i32 [ -3, %34 ], [ -1, %37 ]
%41 = add nsw i32 %29, %40
%42 = add nsw i64 %28, -1
%43 = trunc i64 %28 to i32
%44 = icmp sgt i32 %43, 1
br i1 %44, label %27, label %26
}