#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*/ strncpy(char *, char *, int);
void shuffle_bytes(char *buf, size_t length) {
  length = length - length % 4;
  for (size_t i = 0; i < length / 4; i++) {
    char *bufc = buf + i * 4;
    char chars[4] = {bufc[3], bufc[2], bufc[1], bufc[0]};
    strncpy(bufc, chars, 4);
  }
}


x86 assembly (clang, -O0):
<shuffle_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x10(%rbp),%rcx
and    $0x3,%rcx
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x2,%rcx
cmp    %rcx,%rax
jae    11ad <shuffle_bytes+0x9d>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x3(%rax),%al
mov    %al,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2(%rax),%al
mov    %al,-0x23(%rbp)
mov    -0x20(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x22(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    113b <shuffle_bytes+0x2b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shuffle_bytes>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x4,%rsi
jb     1169 <shuffle_bytes+0x59>
mov    %rsi,%r14
mov    %rdi,%rbx
shr    $0x2,%r14
lea    0xc(%rsp),%r15
xchg   %ax,%ax
movzbl 0x3(%rbx),%eax
mov    %al,0xc(%rsp)
movzbl 0x2(%rbx),%eax
mov    %al,0xd(%rsp)
movzbl 0x1(%rbx),%eax
mov    %al,0xe(%rsp)
movzbl (%rbx),%eax
mov    %al,0xf(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
add    $0x4,%rbx
add    $0xffffffffffffffff,%r14
jne    1130 <shuffle_bytes+0x20>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @shuffle_bytes(i8* %0, i64 %1) {
%3 = alloca [4 x i8]
%4 = icmp ult i64 %1, 4
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = lshr i64 %1, 2
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
br label %12
11: ; preds = %12, %2
ret void
12: ; preds = %5, %12
%13 = phi i64 [ 0, %5 ], [ %24, %12 ]
%14 = shl i64 %13, 2
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = getelementptr inbounds i8, i8* %15, i64 3
%17 = load i8, i8* %16
store i8 %17, i8* %7
%18 = getelementptr inbounds i8, i8* %15, i64 2
%19 = load i8, i8* %18
store i8 %19, i8* %8
%20 = getelementptr inbounds i8, i8* %15, i64 1
%21 = load i8, i8* %20
store i8 %21, i8* %9
%22 = load i8, i8* %15
store i8 %22, i8* %10
%23 = call i32 @strncpy(i8* %15, i8* %7, i32 4)
%24 = add nuw nsw i64 %13, 1
%25 = icmp eq i64 %24, %6
br i1 %25, label %11, label %12
}