#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 */
size_t string_length(char *);
size_t string_reverse(char *string) {
  size_t length = string_length(string);
  if (length) {
    size_t i = 0, j = length - 1;
    for (; i < j; ++i, --j) {
      char swap = string[i];
      string[i] = string[j];
      string[j] = swap;
    }
  }
  return length;
}


x86 assembly (clang, -O0):
<string_reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <string_length@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11aa <string_reverse+0x9a>
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11a5 <string_reverse+0x95>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x21(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1148 <string_reverse+0x38>
jmp    11aa <string_reverse+0x9a>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_reverse>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <string_length@plt>
cmp    $0x2,%rax
jb     1154 <string_reverse+0x44>
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    $0x1,%edx
xchg   %ax,%ax
movzbl -0x1(%r14,%rdx,1),%esi
movzbl (%r14,%rcx,1),%ebx
mov    %bl,-0x1(%r14,%rdx,1)
mov    %sil,(%r14,%rcx,1)
add    $0xffffffffffffffff,%rcx
lea    0x1(%rdx),%rsi
cmp    %rcx,%rdx
mov    %rsi,%rdx
jb     1130 <string_reverse+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @string_reverse(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i64 @string_length(i8* %7)
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %40
11: ; preds = %1
store i64 0, i64* %4
%12 = load i64, i64* %3
%13 = sub i64 %12, 1
store i64 %13, i64* %5
br label %14
14: ; preds = %34, %11
%15 = load i64, i64* %4
%16 = load i64, i64* %5
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = load i8*, i8** %2
%20 = load i64, i64* %4
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i8, i8* %21
store i8 %22, i8* %6
%23 = load i8*, i8** %2
%24 = load i64, i64* %5
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = load i8, i8* %25
%27 = load i8*, i8** %2
%28 = load i64, i64* %4
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8 %26, i8* %29
%30 = load i8, i8* %6
%31 = load i8*, i8** %2
%32 = load i64, i64* %5
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 %30, i8* %33
br label %34
34: ; preds = %18
%35 = load i64, i64* %4
%36 = add i64 %35, 1
store i64 %36, i64* %4
%37 = load i64, i64* %5
%38 = add i64 %37, -1
store i64 %38, i64* %5
br label %14
39: ; preds = %14
br label %40
40: ; preds = %39, %1
%41 = load i64, i64* %3
ret i64 %41
}

LLVM IR (-O2):
define i64 @string_reverse(i8* %0) {
%2 = tail call i64 @string_length(i8* %0)
%3 = icmp ugt i64 %2, 1
br i1 %3, label %4, label %16
4: ; preds = %1
%5 = add i64 %2, -1
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ %14, %6 ], [ %5, %4 ]
%8 = phi i64 [ %13, %6 ], [ 0, %4 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = getelementptr inbounds i8, i8* %0, i64 %7
%12 = load i8, i8* %11
store i8 %12, i8* %9
store i8 %10, i8* %11
%13 = add nuw i64 %8, 1
%14 = add i64 %7, -1
%15 = icmp ult i64 %13, %14
br i1 %15, label %6, label %16
16: ; preds = %6, %1
ret i64 %2
}