void reverse(char *str) {
  char *end = str;
  char *beg = str;
  char t;
  printf("%s %s\n", __FUNCTION__, str);
  while (*str) {
    end = str;
    str++;
  }
  printf("End is %c\n", *end);
  while (end > beg) {
    printf("beg %c end %c\n", *beg, *end);
    t = *end;
    *end = *beg;
    *beg = t;
    printf("beg %c end %c\n\n", *beg, *end);
    end--;
    beg++;
  }
}


x86 assembly (clang, -O0):
<reverse>:
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),%rdx
lea    0xec9(%rip),%rdi
lea    0xec9(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     116b <reverse+0x5b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1145 <reverse+0x35>
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1201 <reverse+0xf1>
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
mov    -0x10(%rbp),%rax
movsbl (%rax),%edx
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
mov    -0x10(%rbp),%rax
movsbl (%rax),%edx
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1180 <reverse+0x70>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
lea    0xede(%rip),%rdi
lea    0xede(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r12,%rcx
mov    %r12,%rax
nopl   0x0(%rax)
mov    %rcx,%rbx
cmpb   $0x0,(%rax)
mov    %rax,%rcx
lea    0x1(%rax),%rax
jne    1140 <reverse+0x30>
movsbl (%rbx),%esi
lea    0xeb6(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    %r12,%rbx
jbe    11c1 <reverse+0xb1>
add    $0xffffffffffffffff,%rbx
lea    0xeaa(%rip),%r14
lea    0xeb2(%rip),%r15
nopw   0x0(%rax,%rax,1)
movsbl (%r12),%esi
movsbl 0x1(%rbx),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movsbl 0x1(%rbx),%esi
movzbl (%r12),%eax
mov    %al,0x1(%rbx)
mov    %sil,(%r12)
movsbl 0x1(%rbx),%edx
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%r12
lea    -0x1(%rbx),%rax
cmp    %r12,%rbx
mov    %rax,%rbx
ja     1180 <reverse+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"%s %s\0A\00"
@__FUNCTION__.reverse = [8 x i8] c"reverse\00"
@.str.1 = [11 x i8] c"End is %c\0A\00"
@.str.2 = [15 x i8] c"beg %c end %c\0A\00"
@.str.3 = [16 x i8] c"beg %c end %c\0A\0A\00"
define void @reverse(i8* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @__FUNCTION__.reverse, i64 0, i64 0), i8* %0)
br label %3
3: ; preds = %3, %1
%4 = phi i8* [ %0, %1 ], [ %5, %3 ]
%5 = phi i8* [ %0, %1 ], [ %8, %3 ]
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 0
%8 = getelementptr inbounds i8, i8* %5, i64 1
br i1 %7, label %9, label %3
9: ; preds = %3
%10 = load i8, i8* %4
%11 = sext i8 %10 to i32
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp ugt i8* %4, %0
br i1 %13, label %14, label %31
14: ; preds = %9, %14
%15 = phi i8* [ %29, %14 ], [ %0, %9 ]
%16 = phi i8* [ %28, %14 ], [ %4, %9 ]
%17 = load i8, i8* %15
%18 = sext i8 %17 to i32
%19 = load i8, i8* %16
%20 = sext i8 %19 to i32
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %20)
%22 = load i8, i8* %16
%23 = load i8, i8* %15
store i8 %23, i8* %16
store i8 %22, i8* %15
%24 = sext i8 %22 to i32
%25 = load i8, i8* %16
%26 = sext i8 %25 to i32
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %26)
%28 = getelementptr inbounds i8, i8* %16, i64 -1
%29 = getelementptr inbounds i8, i8* %15, i64 1
%30 = icmp ugt i8* %28, %29
br i1 %30, label %14, label %31
31: ; preds = %14, %9
ret void
}