#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 *, int);
int strlen(char *);
void reverseStr(char *originStr) {
  char *head = originStr;
  char *end = originStr;
  int strLength = strlen(originStr);
  printf("string length is %d", strLength);
  for (int i = 0; i < strLength - 1; i++) {
    end++;
  }
  printf("\n string length derived by ptr diff is: %d", end - head + 1);
  while (head < end) {
    *head = *head ^ *end;
    *end = *head ^ *end;
    *head = *head ^ *end;
    head++;
    end--;
  }
}


x86 assembly (clang, -O0):
<reverseStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xeae(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1189 <reverseStr+0x69>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    115e <reverseStr+0x3e>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%esi
lea    0xe73(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1219 <reverseStr+0xf9>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
xor    %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
xor    %ecx,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
xor    %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11a6 <reverseStr+0x86>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverseStr>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r14d
lea    0xec9(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    -0x2(%r14),%eax
add    $0x1,%rax
xor    %r15d,%r15d
cmp    $0x2,%r14d
cmovge %rax,%r15
lea    (%rbx,%r15,1),%r14
lea    0x1(%r15),%esi
lea    0xeb4(%rip),%rdi
call   1040 <printf@plt>
cmp    %rbx,%r14
jbe    11a0 <reverseStr+0x80>
lea    (%r15,%rbx,1),%rax
add    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x1(%rax),%ecx
xor    (%rbx),%cl
mov    %cl,(%rbx)
xor    0x1(%rax),%cl
mov    %cl,0x1(%rax)
xor    %cl,(%rbx)
add    $0x1,%rbx
lea    -0x1(%rax),%rcx
cmp    %rax,%rbx
mov    %rcx,%rax
jb     1180 <reverseStr+0x60>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [20 x i8] c"string length is %d\00"
@.str.1 = [43 x i8] c"\0A string length derived by ptr diff is: %d\00"
define void @reverseStr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
store i8* %7, i8** %3
%8 = load i8*, i8** %2
store i8* %8, i8** %4
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 0, i32* %6
br label %13
13: ; preds = %21, %1
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = icmp slt i32 %14, %16
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %4
br label %21
21: ; preds = %18
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
br label %13
24: ; preds = %13
%25 = load i8*, i8** %4
%26 = load i8*, i8** %3
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = add nsw i64 %29, 1
%31 = trunc i64 %30 to i32
%32 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %37, %24
%34 = load i8*, i8** %3
%35 = load i8*, i8** %4
%36 = icmp ult i8* %34, %35
br i1 %36, label %37, label %69
37: ; preds = %33
%38 = load i8*, i8** %3
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = load i8*, i8** %4
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = xor i32 %40, %43
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %3
store i8 %45, i8* %46
%47 = load i8*, i8** %3
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = load i8*, i8** %4
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = xor i32 %49, %52
%54 = trunc i32 %53 to i8
%55 = load i8*, i8** %4
store i8 %54, i8* %55
%56 = load i8*, i8** %3
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = load i8*, i8** %4
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = xor i32 %58, %61
%63 = trunc i32 %62 to i8
%64 = load i8*, i8** %3
store i8 %63, i8* %64
%65 = load i8*, i8** %3
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %3
%67 = load i8*, i8** %4
%68 = getelementptr inbounds i8, i8* %67, i32 -1
store i8* %68, i8** %4
br label %33
69: ; preds = %33
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"string length is %d\00"
@.str.1 = [43 x i8] c"\0A string length derived by ptr diff is: %d\00"
define void @reverseStr(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp sgt i32 %2, 1
%5 = add nsw i32 %2, -2
%6 = zext i32 %5 to i64
%7 = add nuw nsw i64 %6, 1
%8 = select i1 %4, i64 %7, i64 0
%9 = getelementptr i8, i8* %0, i64 %8
%10 = trunc i64 %8 to i32
%11 = add i32 %10, 1
%12 = tail call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp ugt i8* %9, %0
br i1 %13, label %14, label %27
14: ; preds = %1, %14
%15 = phi i8* [ %24, %14 ], [ %0, %1 ]
%16 = phi i8* [ %25, %14 ], [ %9, %1 ]
%17 = load i8, i8* %15
%18 = load i8, i8* %16
%19 = xor i8 %18, %17
store i8 %19, i8* %15
%20 = load i8, i8* %16
%21 = xor i8 %20, %19
store i8 %21, i8* %16
%22 = load i8, i8* %15
%23 = xor i8 %22, %21
store i8 %23, i8* %15
%24 = getelementptr inbounds i8, i8* %15, i64 1
%25 = getelementptr inbounds i8, i8* %16, i64 -1
%26 = icmp ult i8* %24, %25
br i1 %26, label %14, label %27
27: ; preds = %14, %1
ret void
}