#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 */
unsigned char *calloc(int, int);
unsigned char *reverse_endian(long length, unsigned char *buffer) {
  unsigned char *temp = calloc(1, sizeof(char) * length);
  int i;
  for (i = 0; i < length; i++) {
    temp[i] = buffer[length - i - 1];
  }
  return temp;
}


x86 assembly (clang, -O0):
<reverse_endian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x8(%rbp),%rax
jge    117c <reverse_endian+0x6c>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
sub    %rdx,%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113f <reverse_endian+0x2f>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_endian>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x1,%edi
mov    %ebx,%esi
call   1030 <calloc@plt>
test   %rbx,%rbx
jle    1262 <reverse_endian+0x152>
cmp    $0x8,%rbx
jae    113c <reverse_endian+0x2c>
xor    %ecx,%ecx
jmp    1242 <reverse_endian+0x132>
cmp    $0x20,%rbx
jae    1149 <reverse_endian+0x39>
xor    %ecx,%ecx
jmp    11eb <reverse_endian+0xdb>
mov    %rbx,%rcx
and    $0xffffffffffffffe0,%rcx
lea    (%rbx,%r14,1),%rdx
add    $0xfffffffffffffff0,%rdx
xor    %esi,%esi
pxor   %xmm0,%xmm0
xchg   %ax,%ax
movdqu -0x10(%rdx),%xmm1
movdqu (%rdx),%xmm2
movdqa %xmm2,%xmm3
punpcklbw %xmm0,%xmm3
pshufd $0x4e,%xmm3,%xmm3
pshuflw $0x1b,%xmm3,%xmm3
pshufhw $0x1b,%xmm3,%xmm3
punpckhbw %xmm0,%xmm2
pshufd $0x4e,%xmm2,%xmm2
pshuflw $0x1b,%xmm2,%xmm2
pshufhw $0x1b,%xmm2,%xmm2
packuswb %xmm3,%xmm2
movdqa %xmm1,%xmm3
punpcklbw %xmm0,%xmm3
pshufd $0x4e,%xmm3,%xmm3
pshuflw $0x1b,%xmm3,%xmm3
pshufhw $0x1b,%xmm3,%xmm3
punpckhbw %xmm0,%xmm1
pshufd $0x4e,%xmm1,%xmm1
pshuflw $0x1b,%xmm1,%xmm1
pshufhw $0x1b,%xmm1,%xmm1
packuswb %xmm3,%xmm1
movdqu %xmm2,(%rax,%rsi,1)
movdqu %xmm1,0x10(%rax,%rsi,1)
add    $0x20,%rsi
add    $0xffffffffffffffe0,%rdx
cmp    %rsi,%rcx
jne    1160 <reverse_endian+0x50>
cmp    %rbx,%rcx
je     1262 <reverse_endian+0x152>
test   $0x18,%bl
je     1242 <reverse_endian+0x132>
mov    %rcx,%rdx
mov    %rbx,%rcx
and    $0xfffffffffffffff8,%rcx
mov    %rbx,%rsi
sub    %rdx,%rsi
add    %r14,%rsi
add    $0xfffffffffffffff8,%rsi
pxor   %xmm0,%xmm0
cs nopw 0x0(%rax,%rax,1)
movq   (%rsi),%xmm1
punpcklbw %xmm0,%xmm1
pshufd $0x4e,%xmm1,%xmm1
pshuflw $0x1b,%xmm1,%xmm1
pshufhw $0x1b,%xmm1,%xmm1
packuswb %xmm1,%xmm1
movq   %xmm1,(%rax,%rdx,1)
add    $0x8,%rdx
add    $0xfffffffffffffff8,%rsi
cmp    %rdx,%rcx
jne    1210 <reverse_endian+0x100>
cmp    %rbx,%rcx
je     1262 <reverse_endian+0x152>
sub    %rcx,%rbx
mov    %rax,%rdx
add    %rcx,%rdx
nopl   0x0(%rax,%rax,1)
movzbl -0x1(%r14,%rbx,1),%ecx
mov    %cl,(%rdx)
add    $0x1,%rdx
add    $0xffffffffffffffff,%rbx
jne    1250 <reverse_endian+0x140>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @reverse_endian(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i64 %0, i64* %3
store i8* %1, i8** %4
%7 = load i64, i64* %3
%8 = mul i64 1, %7
%9 = trunc i64 %8 to i32
%10 = call i8* @calloc(i32 1, i32 %9)
store i8* %10, i8** %5
store i32 0, i32* %6
br label %11
11: ; preds = %29, %2
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = load i64, i64* %3
%15 = icmp slt i64 %13, %14
br i1 %15, label %16, label %32
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = load i64, i64* %3
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = sub nsw i64 %18, %20
%22 = sub nsw i64 %21, 1
%23 = getelementptr inbounds i8, i8* %17, i64 %22
%24 = load i8, i8* %23
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 %24, i8* %28
br label %29
29: ; preds = %16
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %11
32: ; preds = %11
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
define noalias i8* @reverse_endian(i64 %0, i8* nocapture %1) {
%3 = trunc i64 %0 to i32
%4 = tail call i8* @calloc(i32 1, i32 %3)
%5 = icmp sgt i64 %0, 0
br i1 %5, label %6, label %66
6: ; preds = %2
%7 = icmp ult i64 %0, 8
br i1 %7, label %55, label %8
8: ; preds = %6
%9 = icmp ult i64 %0, 32
br i1 %9, label %37, label %10
10: ; preds = %8
%11 = and i64 %0, -32
br label %12
12: ; preds = %12, %10
%13 = phi i64 [ 0, %10 ], [ %30, %12 ]
%14 = xor i64 %13, -1
%15 = add i64 %14, %0
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -15
%18 = bitcast i8* %17 to <16 x i8>*
%19 = load <16 x i8>, <16 x i8>* %18
%20 = shufflevector <16 x i8> %19, <16 x i8> poison, <16 x i32> <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
%21 = getelementptr inbounds i8, i8* %16, i64 -16
%22 = getelementptr inbounds i8, i8* %21, i64 -15
%23 = bitcast i8* %22 to <16 x i8>*
%24 = load <16 x i8>, <16 x i8>* %23
%25 = shufflevector <16 x i8> %24, <16 x i8> poison, <16 x i32> <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
%26 = getelementptr inbounds i8, i8* %4, i64 %13
%27 = bitcast i8* %26 to <16 x i8>*
store <16 x i8> %20, <16 x i8>* %27
%28 = getelementptr inbounds i8, i8* %26, i64 16
%29 = bitcast i8* %28 to <16 x i8>*
store <16 x i8> %25, <16 x i8>* %29
%30 = add nuw i64 %13, 32
%31 = icmp eq i64 %30, %11
br i1 %31, label %32, label %12
32: ; preds = %12
%33 = icmp eq i64 %11, %0
br i1 %33, label %66, label %34
34: ; preds = %32
%35 = and i64 %0, 24
%36 = icmp eq i64 %35, 0
br i1 %36, label %55, label %37
37: ; preds = %8, %34
%38 = phi i64 [ %11, %34 ], [ 0, %8 ]
%39 = and i64 %0, -8
%40 = getelementptr inbounds i8, i8* %1, i64 -7
br label %41
41: ; preds = %41, %37
%42 = phi i64 [ %38, %37 ], [ %51, %41 ]
%43 = xor i64 %42, -1
%44 = add i64 %43, %0
%45 = getelementptr inbounds i8, i8* %40, i64 %44
%46 = bitcast i8* %45 to <8 x i8>*
%47 = load <8 x i8>, <8 x i8>* %46
%48 = shufflevector <8 x i8> %47, <8 x i8> poison, <8 x i32> <i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
%49 = getelementptr inbounds i8, i8* %4, i64 %42
%50 = bitcast i8* %49 to <8 x i8>*
store <8 x i8> %48, <8 x i8>* %50
%51 = add nuw i64 %42, 8
%52 = icmp eq i64 %51, %39
br i1 %52, label %53, label %41
53: ; preds = %41
%54 = icmp eq i64 %39, %0
br i1 %54, label %66, label %55
55: ; preds = %6, %34, %53
%56 = phi i64 [ 0, %6 ], [ %11, %34 ], [ %39, %53 ]
br label %57
57: ; preds = %55, %57
%58 = phi i64 [ %64, %57 ], [ %56, %55 ]
%59 = xor i64 %58, -1
%60 = add i64 %59, %0
%61 = getelementptr inbounds i8, i8* %1, i64 %60
%62 = load i8, i8* %61
%63 = getelementptr inbounds i8, i8* %4, i64 %58
store i8 %62, i8* %63
%64 = add nuw nsw i64 %58, 1
%65 = icmp eq i64 %64, %0
br i1 %65, label %66, label %57
66: ; preds = %57, %32, %53, %2
ret i8* %4
}