#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*/ fprintf(int /*<<< orphan*/, char *, char, char);
int /*<<< orphan*/ stdout;
void swap_bytes(char *bytes, int num_bytes) {
  int i;
  char tmp;
  for (i = 0; i < num_bytes / 2; i++) {
    fprintf(stdout, "Swapping %d and %d\n", bytes[i], bytes[num_bytes - i - 1]);
    tmp = bytes[i];
    bytes[i] = bytes[num_bytes - i - 1];
    bytes[num_bytes - i - 1] = tmp;
  }
}


x86 assembly (clang, -O0):
<swap_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    11cb <swap_bytes+0xbb>
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
lea    0xe91(%rip),%rsi
movsbl %dl,%edx
movsbl (%rax,%rcx,1),%ecx
call   1030 <fprintf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <swap_bytes+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swap_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%esi
jl     1176 <swap_bytes+0x66>
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%r12d
shr    %r12d
add    $0xffffffff,%ebx
xor    %r13d,%r13d
mov    0x2ea8(%rip),%rbp
lea    0xec1(%rip),%r15
nop
mov    0x0(%rbp),%edi
movsbl (%r14,%r13,1),%edx
movslq %ebx,%rbx
movsbl (%r14,%rbx,1),%ecx
mov    %r15,%rsi
call   1030 <fprintf@plt>
movzbl (%r14,%r13,1),%eax
movzbl (%r14,%rbx,1),%ecx
mov    %cl,(%r14,%r13,1)
mov    %al,(%r14,%rbx,1)
add    $0x1,%r13
add    $0xffffffff,%ebx
cmp    %r13,%r12
jne    1140 <swap_bytes+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [20 x i8] c"Swapping %d and %d\0A\00"
define void @swap_bytes(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %53, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = sdiv i32 %9, 2
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %56
12: ; preds = %7
%13 = load i32, i32* @stdout
%14 = load i8*, i8** %3
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = sub nsw i32 %20, %21
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %19, i64 %24
%26 = load i8, i8* %25
%27 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8 signext %18, i8 signext %26)
%28 = load i8*, i8** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
store i8 %32, i8* %6
%33 = load i8*, i8** %3
%34 = load i32, i32* %4
%35 = load i32, i32* %5
%36 = sub nsw i32 %34, %35
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %33, i64 %38
%40 = load i8, i8* %39
%41 = load i8*, i8** %3
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 %40, i8* %44
%45 = load i8, i8* %6
%46 = load i8*, i8** %3
%47 = load i32, i32* %4
%48 = load i32, i32* %5
%49 = sub nsw i32 %47, %48
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %46, i64 %51
store i8 %45, i8* %52
br label %53
53: ; preds = %12
%54 = load i32, i32* %5
%55 = add nsw i32 %54, 1
store i32 %55, i32* %5
br label %7
56: ; preds = %7
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [20 x i8] c"Swapping %d and %d\0A\00"
define void @swap_bytes(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %23
4: ; preds = %2
%5 = lshr i32 %1, 1
%6 = zext i32 %5 to i64
br label %7
7: ; preds = %4, %7
%8 = phi i64 [ 0, %4 ], [ %21, %7 ]
%9 = load i32, i32* @stdout
%10 = getelementptr inbounds i8, i8* %0, i64 %8
%11 = load i8, i8* %10
%12 = trunc i64 %8 to i32
%13 = xor i32 %12, -1
%14 = add i32 %13, %1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8 signext %11, i8 signext %17)
%19 = load i8, i8* %10
%20 = load i8, i8* %16
store i8 %20, i8* %10
store i8 %19, i8* %16
%21 = add nuw nsw i64 %8, 1
%22 = icmp eq i64 %21, %6
br i1 %22, label %23, label %7
23: ; preds = %7, %2
ret void
}