#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int flip(char *buffer) {
  int ah, al;
  int i, j, t, u;
  char *temp;
  temp = malloc(sizeof(char) * 32768);
  memcpy(temp, buffer, 32768);
  for (i = 0; i < 32768; i++) {
    ah = i & 0xff00;
    al = i & 0x00ff;
    u = 0;
    for (j = 0; j < 8; j++) {
      t = al & 1;
      u = (u << 1) | t;
      al = al >> 1;
    }
    al = u;
    buffer[ah + al] = temp[i];
  }
  free(temp);
}


x86 assembly (clang, -O0):
<flip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x8000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8000,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x8000,-0x1c(%rbp)
jge    11f9 <flip+0xc9>
mov    -0x1c(%rbp),%eax
and    $0xff00,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x8,-0x20(%rbp)
jge    11ca <flip+0x9a>
mov    -0x18(%rbp),%eax
and    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1194 <flip+0x64>
mov    -0x28(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    -0x18(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1163 <flip+0x33>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flip>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x8000,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x8000,%edx
call   1040 <memcpy@plt>
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
mov    %eax,%ecx
rol    $0x4,%cl
mov    %ecx,%edx
and    $0x33,%dl
shl    $0x2,%dl
shr    $0x2,%cl
and    $0x33,%cl
or     %dl,%cl
mov    %ecx,%edx
and    $0x55,%dl
add    %dl,%dl
shr    %cl
and    $0x55,%cl
or     %dl,%cl
movzbl %cl,%ecx
mov    %eax,%edx
and    $0xff00,%edx
movzbl (%r15,%rax,1),%ebx
or     %rcx,%rdx
mov    %bl,(%r14,%rdx,1)
add    $0x1,%rax
cmp    $0x8000,%rax
jne    1160 <flip+0x30>
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
define i32 @flip(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %3
%11 = call i8* @malloc(i32 32768)
store i8* %11, i8** %10
%12 = load i8*, i8** %10
%13 = load i8*, i8** %3
%14 = call i32 @memcpy(i8* %12, i8* %13, i32 32768)
store i32 0, i32* %6
br label %15
15: ; preds = %51, %1
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 32768
br i1 %17, label %18, label %54
18: ; preds = %15
%19 = load i32, i32* %6
%20 = and i32 %19, 65280
store i32 %20, i32* %4
%21 = load i32, i32* %6
%22 = and i32 %21, 255
store i32 %22, i32* %5
store i32 0, i32* %9
store i32 0, i32* %7
br label %23
23: ; preds = %35, %18
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 8
br i1 %25, label %26, label %38
26: ; preds = %23
%27 = load i32, i32* %5
%28 = and i32 %27, 1
store i32 %28, i32* %8
%29 = load i32, i32* %9
%30 = shl i32 %29, 1
%31 = load i32, i32* %8
%32 = or i32 %30, %31
store i32 %32, i32* %9
%33 = load i32, i32* %5
%34 = ashr i32 %33, 1
store i32 %34, i32* %5
br label %35
35: ; preds = %26
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %23
38: ; preds = %23
%39 = load i32, i32* %9
store i32 %39, i32* %5
%40 = load i8*, i8** %10
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = load i8*, i8** %3
%46 = load i32, i32* %4
%47 = load i32, i32* %5
%48 = add nsw i32 %46, %47
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %45, i64 %49
store i8 %44, i8* %50
br label %51
51: ; preds = %38
%52 = load i32, i32* %6
%53 = add nsw i32 %52, 1
store i32 %53, i32* %6
br label %15
54: ; preds = %15
%55 = load i8*, i8** %10
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
define i32 @flip(i8* %0) {
%2 = tail call dereferenceable_or_null(32768) i8* @malloc(i32 32768)
%3 = tail call i32 @memcpy(i8* %2, i8* %0, i32 32768)
br label %4
4: ; preds = %1, %4
%5 = phi i64 [ 0, %1 ], [ %14, %4 ]
%6 = trunc i64 %5 to i8
%7 = call i8 @llvm.bitreverse.i8(i8 %6)
%8 = zext i8 %7 to i64
%9 = and i64 %5, 65280
%10 = getelementptr inbounds i8, i8* %2, i64 %5
%11 = load i8, i8* %10
%12 = or i64 %9, %8
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 %11, i8* %13
%14 = add nuw nsw i64 %5, 1
%15 = icmp eq i64 %14, 32768
br i1 %15, label %16, label %4
16: ; preds = %4
%17 = tail call i32 @free(i8* %2)
ret i32 undef
}