#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 */
void alloc_test_overwrite(void *ptr, size_t length, unsigned int pattern) {
  unsigned char *byte_ptr;
  int pattern_seq;
  unsigned char b;
  size_t i;
  byte_ptr = ptr;
  for (i = 0; i < length; ++i) {
    pattern_seq = (int)(i & 3);
    b = (unsigned char)((pattern >> (8 * pattern_seq)) & 0xff);
    byte_ptr[i] = b;
  }
}


x86 assembly (clang, -O0):
<alloc_test_overwrite>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    116a <alloc_test_overwrite+0x6a>
mov    -0x30(%rbp),%rax
and    $0x3,%rax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x24(%rbp),%ecx
shl    $0x3,%ecx
shr    %cl,%eax
and    $0xff,%eax
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    111f <alloc_test_overwrite+0x1f>
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_test_overwrite>:
test   %rsi,%rsi
je     14db <alloc_test_overwrite+0x3db>
cmp    $0x10,%rsi
jae    1117 <alloc_test_overwrite+0x17>
xor    %r10d,%r10d
jmp    14b1 <alloc_test_overwrite+0x3b1>
mov    %rsi,%r10
and    $0xfffffffffffffff0,%r10
movd   %edx,%xmm0
pshufd $0x0,%xmm0,%xmm6
lea    -0x10(%r10),%rax
mov    %rax,%r8
shr    $0x4,%r8
add    $0x1,%r8
test   %rax,%rax
je     14dc <alloc_test_overwrite+0x3dc>
mov    %r8,%rcx
and    $0xfffffffffffffffe,%rcx
movdqa 0xec1(%rip),%xmm14
movdqa 0xec8(%rip),%xmm13
movdqa 0xecf(%rip),%xmm12
movdqa 0xed6(%rip),%xmm11
xor    %r9d,%r9d
movdqa 0xeda(%rip),%xmm8
movaps 0xee2(%rip),%xmm9
movdqa 0xee9(%rip),%xmm10
nopw   0x0(%rax,%rax,1)
movdqa %xmm14,%xmm5
pslld  $0x3,%xmm5
movdqa %xmm13,%xmm3
pslld  $0x3,%xmm3
movdqa %xmm12,%xmm2
pslld  $0x3,%xmm2
movdqa %xmm11,%xmm1
pslld  $0x3,%xmm1
pand   %xmm8,%xmm1
pand   %xmm8,%xmm2
pand   %xmm8,%xmm3
pand   %xmm8,%xmm5
pshuflw $0xfe,%xmm5,%xmm0
movdqa %xmm6,%xmm7
psrld  %xmm0,%xmm7
pshuflw $0x54,%xmm5,%xmm4
movdqa %xmm6,%xmm0
psrld  %xmm4,%xmm0
punpcklqdq %xmm7,%xmm0
pshufd $0xee,%xmm5,%xmm4
pshuflw $0xfe,%xmm4,%xmm5
movdqa %xmm6,%xmm7
psrld  %xmm5,%xmm7
pshuflw $0x54,%xmm4,%xmm4
movdqa %xmm6,%xmm5
psrld  %xmm4,%xmm5
punpckhqdq %xmm7,%xmm5
shufps $0xcc,%xmm5,%xmm0
pshuflw $0xfe,%xmm3,%xmm4
movdqa %xmm6,%xmm7
psrld  %xmm4,%xmm7
pshuflw $0x54,%xmm3,%xmm4
movdqa %xmm6,%xmm5
psrld  %xmm4,%xmm5
punpcklqdq %xmm7,%xmm5
pshufd $0xee,%xmm3,%xmm3
pshuflw $0xfe,%xmm3,%xmm4
movdqa %xmm6,%xmm7
psrld  %xmm4,%xmm7
pshuflw $0x54,%xmm3,%xmm3
movdqa %xmm6,%xmm4
psrld  %xmm3,%xmm4
punpckhqdq %xmm7,%xmm4
shufps $0xcc,%xmm4,%xmm5
pshuflw $0xfe,%xmm2,%xmm3
movdqa %xmm6,%xmm4
psrld  %xmm3,%xmm4
pshuflw $0x54,%xmm2,%xmm7
movdqa %xmm6,%xmm3
psrld  %xmm7,%xmm3
punpcklqdq %xmm4,%xmm3
pshufd $0xee,%xmm2,%xmm2
pshuflw $0xfe,%xmm2,%xmm4
movdqa %xmm6,%xmm7
psrld  %xmm4,%xmm7
pshuflw $0x54,%xmm2,%xmm2
movdqa %xmm6,%xmm4
psrld  %xmm2,%xmm4
punpckhqdq %xmm7,%xmm4
shufps $0xcc,%xmm4,%xmm3
pshuflw $0xfe,%xmm1,%xmm2
movdqa %xmm6,%xmm4
psrld  %xmm2,%xmm4
pshuflw $0x54,%xmm1,%xmm2
movdqa %xmm6,%xmm7
psrld  %xmm2,%xmm7
punpcklqdq %xmm4,%xmm7
pshufd $0xee,%xmm1,%xmm1
pshuflw $0xfe,%xmm1,%xmm2
movdqa %xmm6,%xmm4
psrld  %xmm2,%xmm4
pshuflw $0x54,%xmm1,%xmm1
movdqa %xmm6,%xmm2
psrld  %xmm1,%xmm2
punpckhqdq %xmm4,%xmm2
shufps $0xcc,%xmm2,%xmm7
andps  %xmm9,%xmm7
andps  %xmm9,%xmm3
packuswb %xmm3,%xmm7
andps  %xmm9,%xmm5
andps  %xmm9,%xmm0
packuswb %xmm0,%xmm5
packuswb %xmm5,%xmm7
movdqu %xmm7,(%rdi,%r9,1)
movdqu %xmm7,0x10(%rdi,%r9,1)
add    $0x20,%r9
paddd  %xmm10,%xmm11
paddd  %xmm10,%xmm12
paddd  %xmm10,%xmm13
paddd  %xmm10,%xmm14
add    $0xfffffffffffffffe,%rcx
jne    1190 <alloc_test_overwrite+0x90>
pslld  $0x3,%xmm14
pslld  $0x3,%xmm13
pslld  $0x3,%xmm12
pslld  $0x3,%xmm11
pand   %xmm8,%xmm11
pand   %xmm8,%xmm12
pand   %xmm8,%xmm13
pand   %xmm8,%xmm14
test   $0x1,%r8b
je     14ac <alloc_test_overwrite+0x3ac>
pshuflw $0xfe,%xmm14,%xmm0
movdqa %xmm6,%xmm1
psrld  %xmm0,%xmm1
pshuflw $0x54,%xmm14,%xmm2
movdqa %xmm6,%xmm9
psrld  %xmm2,%xmm9
pshufd $0xee,%xmm14,%xmm2
pshuflw $0xfe,%xmm2,%xmm3
movdqa %xmm6,%xmm4
psrld  %xmm3,%xmm4
pshuflw $0x54,%xmm2,%xmm2
movdqa %xmm6,%xmm3
psrld  %xmm2,%xmm3
punpcklqdq %xmm1,%xmm9
pshuflw $0xfe,%xmm13,%xmm1
movdqa %xmm6,%xmm2
psrld  %xmm1,%xmm2
punpckhqdq %xmm4,%xmm3
pshuflw $0x54,%xmm13,%xmm4
movdqa %xmm6,%xmm1
psrld  %xmm4,%xmm1
pshufd $0xee,%xmm13,%xmm4
pshuflw $0xfe,%xmm4,%xmm5
movdqa %xmm6,%xmm7
psrld  %xmm5,%xmm7
shufps $0xcc,%xmm3,%xmm9
pshuflw $0x54,%xmm4,%xmm3
movdqa %xmm6,%xmm4
psrld  %xmm3,%xmm4
punpcklqdq %xmm2,%xmm1
pshuflw $0xfe,%xmm12,%xmm2
movdqa %xmm6,%xmm3
psrld  %xmm2,%xmm3
punpckhqdq %xmm7,%xmm4
pshuflw $0x54,%xmm12,%xmm5
movdqa %xmm6,%xmm2
psrld  %xmm5,%xmm2
pshufd $0xee,%xmm12,%xmm5
pshuflw $0xfe,%xmm5,%xmm8
movdqa %xmm6,%xmm7
psrld  %xmm8,%xmm7
shufps $0xcc,%xmm4,%xmm1
pshuflw $0x54,%xmm5,%xmm4
movdqa %xmm6,%xmm5
psrld  %xmm4,%xmm5
punpcklqdq %xmm3,%xmm2
pshuflw $0xfe,%xmm11,%xmm3
movdqa %xmm6,%xmm4
psrld  %xmm3,%xmm4
punpckhqdq %xmm7,%xmm5
pshuflw $0x54,%xmm11,%xmm3
movdqa %xmm6,%xmm7
psrld  %xmm3,%xmm7
pshufd $0xee,%xmm11,%xmm3
pshuflw $0xfe,%xmm3,%xmm8
movdqa %xmm6,%xmm0
psrld  %xmm8,%xmm0
shufps $0xcc,%xmm5,%xmm2
pshuflw $0x54,%xmm3,%xmm3
psrld  %xmm3,%xmm6
punpcklqdq %xmm4,%xmm7
punpckhqdq %xmm0,%xmm6
shufps $0xcc,%xmm6,%xmm7
movaps 0xbd4(%rip),%xmm0
andps  %xmm0,%xmm7
andps  %xmm0,%xmm2
packuswb %xmm2,%xmm7
andps  %xmm0,%xmm1
andps  %xmm0,%xmm9
packuswb %xmm9,%xmm1
packuswb %xmm1,%xmm7
movdqu %xmm7,(%rdi,%r9,1)
cmp    %rsi,%r10
je     14db <alloc_test_overwrite+0x3db>
lea    0x0(,%r10,8),%eax
nopl   0x0(%rax)
mov    %eax,%ecx
and    $0x18,%cl
mov    %edx,%r8d
shr    %cl,%r8d
mov    %r8b,(%rdi,%r10,1)
add    $0x1,%r10
add    $0x8,%eax
cmp    %r10,%rsi
jne    14c0 <alloc_test_overwrite+0x3c0>
ret
movdqa 0xb1b(%rip),%xmm11
xor    %r9d,%r9d
movdqa %xmm11,%xmm12
movdqa %xmm11,%xmm13
movdqa %xmm11,%xmm14
test   $0x1,%r8b
jne    1361 <alloc_test_overwrite+0x261>
jmp    14ac <alloc_test_overwrite+0x3ac>

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

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