#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*/ print_buffer(void *, unsigned int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ zeromem(void *, unsigned int);
void reverse_buffer(void *buf, unsigned size) {
  unsigned char tmp = 0x0;
  unsigned char *i = (unsigned char *)buf;
  unsigned char *j = (unsigned char *)buf + size - 1;
  zeromem(buf, size);
  printf("ZEROMEM\n");
  print_buffer(buf, size);
  int k = 0;
  for (k = 0; k < size; k++) {
    i[k] = k;
  }
  printf("BEFORE\n");
  print_buffer(buf, size);
  while (i < j) {
    tmp = *j;
    *j-- = *i;
    *i++ = tmp;
  }
  printf("AFTER\n");
  print_buffer(buf, size);
}


x86 assembly (clang, -O0):
<reverse_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movb   $0x0,-0xd(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <zeromem@plt>
lea    0xe90(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <print_buffer@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11b9 <reverse_buffer+0x89>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118f <reverse_buffer+0x5f>
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <print_buffer@plt>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1218 <reverse_buffer+0xe8>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0xd(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0xd(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    11d1 <reverse_buffer+0xa1>
lea    0xdf2(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <print_buffer@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %esi,%r15d
lea    (%rdi,%r15,1),%rbp
call   1050 <zeromem@plt>
lea    0xf40(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <print_buffer@plt>
test   %r14d,%r14d
je     12bc <reverse_buffer+0x18c>
cmp    $0x10,%r14d
jae    1176 <reverse_buffer+0x46>
xor    %eax,%eax
jmp    12b0 <reverse_buffer+0x180>
mov    %r15d,%eax
and    $0xfffffff0,%eax
lea    -0x10(%rax),%rdx
mov    %rdx,%rsi
shr    $0x4,%rsi
add    $0x1,%rsi
mov    %esi,%ecx
and    $0x7,%ecx
cmp    $0x70,%rdx
jae    11a5 <reverse_buffer+0x75>
movdqa 0xe62(%rip),%xmm2
xor    %edx,%edx
jmp    1277 <reverse_buffer+0x147>
and    $0xfffffffffffffff8,%rsi
movdqa 0xe4f(%rip),%xmm2
xor    %edx,%edx
movdqa 0xe54(%rip),%xmm8
movdqa 0xe5b(%rip),%xmm9
movdqa 0xe63(%rip),%xmm3
movdqa 0xe6b(%rip),%xmm4
movdqa 0xe73(%rip),%xmm5
movdqa 0xe7b(%rip),%xmm6
movdqa 0xe83(%rip),%xmm7
movdqa 0xe8b(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
nop
movdqu %xmm2,(%rbx,%rdx,1)
movdqa %xmm2,%xmm0
paddb  %xmm8,%xmm0
movdqu %xmm0,0x10(%rbx,%rdx,1)
movdqa %xmm2,%xmm0
paddb  %xmm9,%xmm0
movdqu %xmm0,0x20(%rbx,%rdx,1)
movdqa %xmm2,%xmm0
paddb  %xmm3,%xmm0
movdqu %xmm0,0x30(%rbx,%rdx,1)
movdqa %xmm2,%xmm0
paddb  %xmm4,%xmm0
movdqu %xmm0,0x40(%rbx,%rdx,1)
movdqa %xmm2,%xmm0
paddb  %xmm5,%xmm0
movdqu %xmm0,0x50(%rbx,%rdx,1)
movdqa %xmm2,%xmm0
paddb  %xmm6,%xmm0
movdqu %xmm0,0x60(%rbx,%rdx,1)
movdqa %xmm2,%xmm0
paddb  %xmm7,%xmm0
movdqu %xmm0,0x70(%rbx,%rdx,1)
sub    $0xffffffffffffff80,%rdx
pxor   %xmm1,%xmm2
add    $0xfffffffffffffff8,%rsi
jne    1200 <reverse_buffer+0xd0>
test   %rcx,%rcx
je     12a2 <reverse_buffer+0x172>
add    %rbx,%rdx
shl    $0x4,%rcx
xor    %esi,%esi
movdqa 0xd83(%rip),%xmm1
nopl   (%rax)
movdqu %xmm2,(%rdx,%rsi,1)
paddb  %xmm1,%xmm2
add    $0x10,%rsi
cmp    %rsi,%rcx
jne    1290 <reverse_buffer+0x160>
cmp    %r15,%rax
je     12bc <reverse_buffer+0x18c>
nopw   0x0(%rax,%rax,1)
mov    %al,(%rbx,%rax,1)
add    $0x1,%rax
cmp    %rax,%r15
jne    12b0 <reverse_buffer+0x180>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <print_buffer@plt>
add    $0xffffffffffffffff,%rbp
cmp    %rbx,%rbp
jbe    12fe <reverse_buffer+0x1ce>
lea    0x1(%rbx),%rax
movzbl 0x0(%rbp),%ecx
movzbl -0x1(%rax),%edx
mov    %dl,0x0(%rbp)
mov    %cl,-0x1(%rax)
add    $0xffffffffffffffff,%rbp
lea    0x1(%rax),%rcx
cmp    %rbp,%rax
mov    %rcx,%rax
jb     12e0 <reverse_buffer+0x1b0>
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <print_buffer@plt>

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

LLVM IR (-O2):
@str = [8 x i8] c"ZEROMEM\00"
@str.3 = [7 x i8] c"BEFORE\00"
@str.4 = [6 x i8] c"AFTER\00"
define void @reverse_buffer(i8* %0, i32 %1) {
%3 = zext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = tail call i32 @zeromem(i8* %0, i32 %1)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @print_buffer(i8* %0, i32 %1)
%8 = icmp eq i32 %1, 0
br i1 %8, label %82, label %9
9: ; preds = %2
%10 = icmp ult i32 %1, 16
br i1 %10, label %74, label %11
11: ; preds = %9
%12 = and i64 %3, 4294967280
%13 = add nsw i64 %12, -16
%14 = lshr exact i64 %13, 4
%15 = add nuw nsw i64 %14, 1
%16 = and i64 %15, 7
%17 = icmp ult i64 %13, 112
br i1 %17, label %58, label %18
18: ; preds = %11
%19 = and i64 %15, 2305843009213693944
br label %20
20: ; preds = %20, %18
%21 = phi i64 [ 0, %18 ], [ %54, %20 ]
%22 = phi <16 x i8> [ <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 8, i8 9, i8 10, i8 11, i8 12, i8 13, i8 14, i8 15>, %18 ], [ %55, %20 ]
%23 = phi i64 [ 0, %18 ], [ %56, %20 ]
%24 = getelementptr inbounds i8, i8* %0, i64 %21
%25 = bitcast i8* %24 to <16 x i8>*
store <16 x i8> %22, <16 x i8>* %25
%26 = or i64 %21, 16
%27 = add <16 x i8> %22, <i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16>
%28 = getelementptr inbounds i8, i8* %0, i64 %26
%29 = bitcast i8* %28 to <16 x i8>*
store <16 x i8> %27, <16 x i8>* %29
%30 = or i64 %21, 32
%31 = add <16 x i8> %22, <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>
%32 = getelementptr inbounds i8, i8* %0, i64 %30
%33 = bitcast i8* %32 to <16 x i8>*
store <16 x i8> %31, <16 x i8>* %33
%34 = or i64 %21, 48
%35 = add <16 x i8> %22, <i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48>
%36 = getelementptr inbounds i8, i8* %0, i64 %34
%37 = bitcast i8* %36 to <16 x i8>*
store <16 x i8> %35, <16 x i8>* %37
%38 = or i64 %21, 64
%39 = add <16 x i8> %22, <i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64>
%40 = getelementptr inbounds i8, i8* %0, i64 %38
%41 = bitcast i8* %40 to <16 x i8>*
store <16 x i8> %39, <16 x i8>* %41
%42 = or i64 %21, 80
%43 = add <16 x i8> %22, <i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80>
%44 = getelementptr inbounds i8, i8* %0, i64 %42
%45 = bitcast i8* %44 to <16 x i8>*
store <16 x i8> %43, <16 x i8>* %45
%46 = or i64 %21, 96
%47 = add <16 x i8> %22, <i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96>
%48 = getelementptr inbounds i8, i8* %0, i64 %46
%49 = bitcast i8* %48 to <16 x i8>*
store <16 x i8> %47, <16 x i8>* %49
%50 = or i64 %21, 112
%51 = add <16 x i8> %22, <i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112>
%52 = getelementptr inbounds i8, i8* %0, i64 %50
%53 = bitcast i8* %52 to <16 x i8>*
store <16 x i8> %51, <16 x i8>* %53
%54 = add nuw i64 %21, 128
%55 = xor <16 x i8> %22, <i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128>
%56 = add i64 %23, 8
%57 = icmp eq i64 %56, %19
br i1 %57, label %58, label %20
58: ; preds = %20, %11
%59 = phi i64 [ 0, %11 ], [ %54, %20 ]
%60 = phi <16 x i8> [ <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 8, i8 9, i8 10, i8 11, i8 12, i8 13, i8 14, i8 15>, %11 ], [ %55, %20 ]
%61 = icmp eq i64 %16, 0
br i1 %61, label %72, label %62
62: ; preds = %58, %62
%63 = phi i64 [ %68, %62 ], [ %59, %58 ]
%64 = phi <16 x i8> [ %69, %62 ], [ %60, %58 ]
%65 = phi i64 [ %70, %62 ], [ 0, %58 ]
%66 = getelementptr inbounds i8, i8* %0, i64 %63
%67 = bitcast i8* %66 to <16 x i8>*
store <16 x i8> %64, <16 x i8>* %67
%68 = add nuw i64 %63, 16
%69 = add <16 x i8> %64, <i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16>
%70 = add i64 %65, 1
%71 = icmp eq i64 %70, %16
br i1 %71, label %72, label %62
72: ; preds = %62, %58
%73 = icmp eq i64 %12, %3
br i1 %73, label %82, label %74
74: ; preds = %9, %72
%75 = phi i64 [ 0, %9 ], [ %12, %72 ]
br label %76
76: ; preds = %74, %76
%77 = phi i64 [ %80, %76 ], [ %75, %74 ]
%78 = trunc i64 %77 to i8
%79 = getelementptr inbounds i8, i8* %0, i64 %77
store i8 %78, i8* %79
%80 = add nuw nsw i64 %77, 1
%81 = icmp eq i64 %80, %3
br i1 %81, label %82, label %76
82: ; preds = %76, %72, %2
%83 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.3, i64 0, i64 0))
%84 = tail call i32 @print_buffer(i8* %0, i32 %1)
%85 = getelementptr inbounds i8, i8* %4, i64 -1
%86 = icmp ugt i8* %85, %0
br i1 %86, label %87, label %95
87: ; preds = %82, %87
%88 = phi i8* [ %93, %87 ], [ %85, %82 ]
%89 = phi i8* [ %92, %87 ], [ %0, %82 ]
%90 = load i8, i8* %88
%91 = load i8, i8* %89
store i8 %91, i8* %88
%92 = getelementptr inbounds i8, i8* %89, i64 1
store i8 %90, i8* %89
%93 = getelementptr inbounds i8, i8* %88, i64 -1
%94 = icmp ult i8* %92, %93
br i1 %94, label %87, label %95
95: ; preds = %87, %82
%96 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.4, i64 0, i64 0))
%97 = tail call i32 @print_buffer(i8* %0, i32 %1)
ret void
}