#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*/ fix_backwards_sequences(char *);
int strnlen(char *, size_t);
int /*<<< orphan*/ swap(char *, char *);
bool reverse(char *string, size_t max_len) {
  char *p = string;
  char *q = p + strnlen(p, max_len) - 1;
  while (p < q) {
    swap(p, q);
    p++;
    q--;
  }
  fix_backwards_sequences(string);
  return true;
}


x86 assembly (clang, -O0):
<reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11a9 <reverse+0x79>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <swap@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1171 <reverse+0x41>
mov    -0x8(%rbp),%rdi
call   1040 <fix_backwards_sequences@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strnlen@plt>
cltq
lea    (%r14,%rax,1),%r15
add    $0xffffffffffffffff,%r15
cmp    %r14,%r15
jbe    1168 <reverse+0x38>
mov    %r14,%rbx
nop
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <swap@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r15
cmp    %r15,%rbx
jb     1150 <reverse+0x20>
mov    %r14,%rdi
call   1040 <fix_backwards_sequences@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @reverse(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = load i8*, i8** %5
%10 = load i64, i64* %4
%11 = call i32 @strnlen(i8* %9, i64 %10)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = getelementptr inbounds i8, i8* %13, i64 -1
store i8* %14, i8** %6
br label %15
15: ; preds = %19, %2
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = icmp ult i8* %16, %17
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = load i8*, i8** %6
%22 = call i32 @swap(i8* %20, i8* %21)
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %6
br label %15
27: ; preds = %15
%28 = load i8*, i8** %3
%29 = call i32 @fix_backwards_sequences(i8* %28)
ret i32 1
}

LLVM IR (-O2):
define i32 @reverse(i8* %0, i64 %1) {
%3 = tail call i32 @strnlen(i8* %0, i64 %1)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = getelementptr inbounds i8, i8* %5, i64 -1
%7 = icmp ugt i8* %6, %0
br i1 %7, label %8, label %15
8: ; preds = %2, %8
%9 = phi i8* [ %13, %8 ], [ %6, %2 ]
%10 = phi i8* [ %12, %8 ], [ %0, %2 ]
%11 = tail call i32 @swap(i8* %10, i8* %9)
%12 = getelementptr inbounds i8, i8* %10, i64 1
%13 = getelementptr inbounds i8, i8* %9, i64 -1
%14 = icmp ult i8* %12, %13
br i1 %14, label %8, label %15
15: ; preds = %8, %2
%16 = tail call i32 @fix_backwards_sequences(i8* %0)
ret i32 1
}