#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int flush_buf(int, char *, int, int);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int write_pattern(int fd, char *pattern, int write_size) {
  char *buf, *ptr;
  size_t size;
  int r, count, nblocks;
  nblocks = write_size / 4096;
  if (!nblocks)
    nblocks = 1;
  size = nblocks * 4096;
  if ((buf = malloc(size)) == NULL) {
    perror("malloc");
    return EXIT_FAILURE;
  }
  count = 0;
  do {
    ptr = &buf[count * 4096];
    switch (*pattern) {
    case 'A':
    case 'B':
    case 'C':
    case 'D':
    case 'U':
      memset(ptr, *pattern, 4096);
      break;
    case '0':
      memset(ptr, 0, 4096);
      break;
    case '\0':
      memset(ptr, 0, 4096);
      ptr[0] = 'E';
      ptr[1] = 'O';
      ptr[2] = 'F';
    }
    if (++count == nblocks) {
      if ((r = flush_buf(fd, buf, size, write_size)) != EXIT_SUCCESS) {
        free(buf);
        return r;
      }
      count = 0;
    }
  } while (*pattern++);
  if (count > 0)
    r = flush_buf(fd, buf, count * 4096, write_size);
  else
    r = EXIT_SUCCESS;
  free(buf);
  return r;
}


x86 assembly (clang, -O0):
<write_pattern>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1181 <write_pattern+0x31>
movl   $0x1,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
shl    $0xc,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c1 <write_pattern+0x71>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1334 <write_pattern+0x1e4>
movl   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%ecx
shl    $0xc,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x40(%rbp)
test   %eax,%eax
je     125a <write_pattern+0x10a>
jmp    11f3 <write_pattern+0xa3>
mov    -0x40(%rbp),%eax
sub    $0x30,%eax
je     1245 <write_pattern+0xf5>
jmp    1204 <write_pattern+0xb4>
mov    -0x40(%rbp),%eax
add    $0xffffffbf,%eax
sub    $0x4,%eax
jb     1229 <write_pattern+0xd9>
jmp    1218 <write_pattern+0xc8>
mov    -0x40(%rbp),%eax
sub    $0x55,%eax
jne    1281 <write_pattern+0x131>
jmp    1229 <write_pattern+0xd9>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%al
movsbl %al,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
jmp    1281 <write_pattern+0x131>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
jmp    1281 <write_pattern+0x131>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    -0x28(%rbp),%rax
movb   $0x45,(%rax)
mov    -0x28(%rbp),%rax
movb   $0x4f,0x1(%rax)
mov    -0x28(%rbp),%rax
movb   $0x46,0x2(%rax)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
cmp    -0x3c(%rbp),%eax
jne    12d5 <write_pattern+0x185>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
call   1050 <flush_buf@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d26(%rip),%rcx
cmp    (%rcx),%eax
je     12ce <write_pattern+0x17e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1334 <write_pattern+0x1e4>
movl   $0x0,-0x38(%rbp)
jmp    12da <write_pattern+0x18a>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
cmpb   $0x0,(%rax)
jne    11c8 <write_pattern+0x78>
cmpl   $0x0,-0x38(%rbp)
jle    1319 <write_pattern+0x1c9>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%edx
shl    $0xc,%edx
mov    -0x14(%rbp),%ecx
call   1050 <flush_buf@plt>
mov    %eax,-0x34(%rbp)
jmp    1325 <write_pattern+0x1d5>
mov    0x2cb8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pattern>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,0xc(%rsp)
mov    %rdx,0x10(%rsp)
lea    0xfff(%rdx),%eax
mov    $0x1,%r12d
cmp    $0x1fff,%eax
jb     118d <write_pattern+0x3d>
mov    0x10(%rsp),%rcx
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0xc,%eax
mov    %eax,%r12d
mov    %r12d,%r14d
shl    $0xc,%r14d
movslq %r14d,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     129a <write_pattern+0x14a>
mov    %rax,%r13
xor    %r15d,%r15d
jmp    11c0 <write_pattern+0x70>
nopl   (%rax)
cmpb   $0x0,(%rbx)
je     125b <write_pattern+0x10b>
add    $0x1,%rbx
mov    %edx,%r15d
mov    %r15d,%eax
shl    $0xc,%eax
movslq %eax,%rbp
add    %r13,%rbp
movsbl (%rbx),%esi
cmp    $0x40,%esi
jle    1230 <write_pattern+0xe0>
lea    -0x41(%rsi),%eax
cmp    $0x4,%eax
jb     11f0 <write_pattern+0xa0>
cmp    $0x55,%esi
jne    11fd <write_pattern+0xad>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x1000,%edx
call   1040 <memset@plt>
lea    0x1(%r15),%edx
cmp    %r12d,%edx
jne    11b0 <write_pattern+0x60>
mov    0xc(%rsp),%edi
mov    %r13,%rsi
mov    %r14d,%edx
mov    0x10(%rsp),%rcx
call   1050 <flush_buf@plt>
mov    %eax,%ebp
mov    0x2db5(%rip),%rax
cmp    (%rax),%ebp
jne    1281 <write_pattern+0x131>
xor    %edx,%edx
cmpb   $0x0,(%rbx)
jne    11b9 <write_pattern+0x69>
jmp    1281 <write_pattern+0x131>
test   %esi,%esi
je     1240 <write_pattern+0xf0>
cmp    $0x30,%esi
jne    11fd <write_pattern+0xad>
mov    %rbp,%rdi
xor    %esi,%esi
jmp    11f3 <write_pattern+0xa3>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
movw   $0x4f45,0x0(%rbp)
movb   $0x46,0x2(%rbp)
jmp    11fd <write_pattern+0xad>
test   %r15d,%r15d
js     1278 <write_pattern+0x128>
shl    $0xc,%edx
mov    0xc(%rsp),%edi
mov    %r13,%rsi
mov    0x10(%rsp),%rcx
call   1050 <flush_buf@plt>
mov    %eax,%ebp
jmp    1281 <write_pattern+0x131>
mov    0x2d59(%rip),%rax
mov    (%rax),%ebp
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%ebp
jmp    1289 <write_pattern+0x139>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @write_pattern(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%14 = load i32, i32* %7
%15 = sdiv i32 %14, 4096
store i32 %15, i32* %13
%16 = load i32, i32* %13
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %3
store i32 1, i32* %13
br label %19
19: ; preds = %18, %3
%20 = load i32, i32* %13
%21 = mul nsw i32 %20, 4096
%22 = sext i32 %21 to i64
store i64 %22, i64* %10
%23 = load i64, i64* %10
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
store i32 %28, i32* %4
br label %97
29: ; preds = %19
store i32 0, i32* %12
br label %30
30: ; preds = %76, %29
%31 = load i8*, i8** %8
%32 = load i32, i32* %12
%33 = mul nsw i32 %32, 4096
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8* %35, i8** %9
%36 = load i8*, i8** %6
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
switch i32 %38, label %56 [
i32 65, label %39
i32 66, label %39
i32 67, label %39
i32 68, label %39
i32 85, label %39
i32 48, label %44
i32 0, label %47
]
39: ; preds = %30, %30, %30, %30, %30
%40 = load i8*, i8** %9
%41 = load i8*, i8** %6
%42 = load i8, i8* %41
%43 = call i32 @memset(i8* %40, i8 signext %42, i32 4096)
br label %56
44: ; preds = %30
%45 = load i8*, i8** %9
%46 = call i32 @memset(i8* %45, i8 signext 0, i32 4096)
br label %56
47: ; preds = %30
%48 = load i8*, i8** %9
%49 = call i32 @memset(i8* %48, i8 signext 0, i32 4096)
%50 = load i8*, i8** %9
%51 = getelementptr inbounds i8, i8* %50, i64 0
store i8 69, i8* %51
%52 = load i8*, i8** %9
%53 = getelementptr inbounds i8, i8* %52, i64 1
store i8 79, i8* %53
%54 = load i8*, i8** %9
%55 = getelementptr inbounds i8, i8* %54, i64 2
store i8 70, i8* %55
br label %56
56: ; preds = %47, %30, %44, %39
%57 = load i32, i32* %12
%58 = add nsw i32 %57, 1
store i32 %58, i32* %12
%59 = load i32, i32* %13
%60 = icmp eq i32 %58, %59
br i1 %60, label %61, label %75
61: ; preds = %56
%62 = load i32, i32* %5
%63 = load i8*, i8** %8
%64 = load i64, i64* %10
%65 = trunc i64 %64 to i32
%66 = load i32, i32* %7
%67 = call i32 @flush_buf(i32 %62, i8* %63, i32 %65, i32 %66)
store i32 %67, i32* %11
%68 = load i32, i32* @EXIT_SUCCESS
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %74
70: ; preds = %61
%71 = load i8*, i8** %8
%72 = call i32 @free(i8* %71)
%73 = load i32, i32* %11
store i32 %73, i32* %4
br label %97
74: ; preds = %61
store i32 0, i32* %12
br label %75
75: ; preds = %74, %56
br label %76
76: ; preds = %75
%77 = load i8*, i8** %6
%78 = getelementptr inbounds i8, i8* %77, i32 1
store i8* %78, i8** %6
%79 = load i8, i8* %77
%80 = icmp ne i8 %79, 0
br i1 %80, label %30, label %81
81: ; preds = %76
%82 = load i32, i32* %12
%83 = icmp sgt i32 %82, 0
br i1 %83, label %84, label %91
84: ; preds = %81
%85 = load i32, i32* %5
%86 = load i8*, i8** %8
%87 = load i32, i32* %12
%88 = mul nsw i32 %87, 4096
%89 = load i32, i32* %7
%90 = call i32 @flush_buf(i32 %85, i8* %86, i32 %88, i32 %89)
store i32 %90, i32* %11
br label %93
91: ; preds = %81
%92 = load i32, i32* @EXIT_SUCCESS
store i32 %92, i32* %11
br label %93
93: ; preds = %91, %84
%94 = load i8*, i8** %8
%95 = call i32 @free(i8* %94)
%96 = load i32, i32* %11
store i32 %96, i32* %4
br label %97
97: ; preds = %93, %70, %26
%98 = load i32, i32* %4
ret i32 %98
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @write_pattern(i32 %0, i8* nocapture %1, i32 %2) {
%4 = sdiv i32 %2, 4096
%5 = add i32 %2, 4095
%6 = icmp ult i32 %5, 8191
%7 = select i1 %6, i32 1, i32 %4
%8 = shl nsw i32 %7, 12
%9 = sext i32 %8 to i64
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
br label %59
15: ; preds = %3, %43
%16 = phi i8* [ %45, %43 ], [ %1, %3 ]
%17 = phi i32 [ %44, %43 ], [ 0, %3 ]
%18 = shl nsw i32 %17, 12
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %10, i64 %19
%21 = load i8, i8* %16
%22 = sext i8 %21 to i32
switch i32 %22, label %31 [
i32 65, label %23
i32 66, label %23
i32 67, label %23
i32 68, label %23
i32 85, label %23
i32 48, label %25
i32 0, label %27
]
23: ; preds = %15, %15, %15, %15, %15
%24 = tail call i32 @memset(i8* %20, i8 signext %21, i32 4096)
br label %31
25: ; preds = %15
%26 = tail call i32 @memset(i8* %20, i8 signext 0, i32 4096)
br label %31
27: ; preds = %15
%28 = tail call i32 @memset(i8* %20, i8 signext 0, i32 4096)
store i8 69, i8* %20
%29 = getelementptr inbounds i8, i8* %20, i64 1
store i8 79, i8* %29
%30 = getelementptr inbounds i8, i8* %20, i64 2
store i8 70, i8* %30
br label %31
31: ; preds = %27, %15, %25, %23
%32 = add nsw i32 %17, 1
%33 = icmp eq i32 %32, %7
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = tail call i32 @flush_buf(i32 %0, i8* %10, i32 %8, i32 %2)
%36 = load i32, i32* @EXIT_SUCCESS
%37 = icmp eq i32 %35, %36
br i1 %37, label %46, label %38
38: ; preds = %34
%39 = tail call i32 @free(i8* %10)
br label %59
40: ; preds = %31
%41 = load i8, i8* %16
%42 = icmp eq i8 %41, 0
br i1 %42, label %49, label %43
43: ; preds = %40, %46
%44 = phi i32 [ %32, %40 ], [ 0, %46 ]
%45 = getelementptr inbounds i8, i8* %16, i64 1
br label %15
46: ; preds = %34
%47 = load i8, i8* %16
%48 = icmp eq i8 %47, 0
br i1 %48, label %56, label %43
49: ; preds = %40
%50 = icmp sgt i32 %17, -1
br i1 %50, label %53, label %51
51: ; preds = %49
%52 = load i32, i32* @EXIT_SUCCESS
br label %56
53: ; preds = %49
%54 = shl nsw i32 %32, 12
%55 = tail call i32 @flush_buf(i32 %0, i8* %10, i32 %54, i32 %2)
br label %56
56: ; preds = %46, %51, %53
%57 = phi i32 [ %55, %53 ], [ %52, %51 ], [ %35, %46 ]
%58 = tail call i32 @free(i8* %10)
br label %59
59: ; preds = %56, %38, %12
%60 = phi i32 [ %14, %12 ], [ %35, %38 ], [ %57, %56 ]
ret i32 %60
}