#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 BUFSIZ;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int write(int, char *, int);
int wu_clear(int fd) {
  int ret;
  char abort[] = {0x18, 0x0};
  char hard_reset_packet[] = "#V,W,0;";
  char reset_packet[] = "#R,W,0;";
  char read_packet[BUFSIZ];
  ret = write(fd, abort, strlen(abort));
  if (ret != strlen(abort)) {
    fprintf(stderr, "Error Clearing memory!\n");
  } else {
    sleep(1);
  }
  ret = write(fd, hard_reset_packet, strlen(hard_reset_packet));
  if (ret != strlen(hard_reset_packet)) {
    fprintf(stderr, "Error Clearing memory!\n");
  } else {
    sleep(1);
  }
  while (1) {
    ret = write(fd, reset_packet, strlen(reset_packet));
    if (ret != strlen(reset_packet)) {
      fprintf(stderr, "Error Clearing memory!\n");
    } else {
      sleep(1);
    }
    read(fd, read_packet, BUFSIZ);
    sleep(1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<wu_clear>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
movw   $0x18,-0xa(%rbp)
movabs $0x3b302c572c5623,%rax
mov    %rax,-0x18(%rbp)
movabs $0x3b302c572c5223,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0xa(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0xa(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0xa(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
je     120a <wu_clear+0xba>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1214 <wu_clear+0xc4>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x64(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
cmp    %ecx,%eax
je     1272 <wu_clear+0x122>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
call   1060 <fprintf@plt>
jmp    127c <wu_clear+0x12c>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1281 <wu_clear+0x131>
jmp    1286 <wu_clear+0x136>
mov    -0x4(%rbp),%eax
mov    %eax,-0x74(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x70(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x68(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%eax
cmp    %ecx,%eax
je     12e4 <wu_clear+0x194>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    0xd38(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12ee <wu_clear+0x19e>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x50(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    0x2cdc(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1281 <wu_clear+0x131>

x86 assembly (clang, -O2):
<wu_clear>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
movw   $0x18,-0x2a(%rbp)
movabs $0x3b302c572c5223,%rax
lea    0x400(%rax),%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,-0x38(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    -0x2a(%rbp),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebx
jne    11d0 <wu_clear+0x80>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    11e5 <wu_clear+0x95>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1060 <fprintf@plt>
lea    -0x40(%rbp),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebx
jne    1218 <wu_clear+0xc8>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    122d <wu_clear+0xdd>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1060 <fprintf@plt>
lea    -0x38(%rbp),%r13
lea    0xdc8(%rip),%r12
jmp    1268 <wu_clear+0x118>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r14,%rsi
call   1050 <read@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebx
je     1240 <wu_clear+0xf0>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
call   1060 <fprintf@plt>
jmp    124a <wu_clear+0xfa>

LLVM IR (-O0):
@__const.wu_clear.abort = [2 x i8] c"\18\00"
@__const.wu_clear.hard_reset_packet = [8 x i8] c"#V,W,0;\00"
@__const.wu_clear.reset_packet = [8 x i8] c"#R,W,0;\00"
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"Error Clearing memory!\0A\00"
define i32 @wu_clear(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i8]
%5 = alloca [8 x i8]
%6 = alloca [8 x i8]
%7 = alloca i8*
%8 = alloca i64
store i32 %0, i32* %2
%9 = bitcast [2 x i8]* %4 to i8*
%10 = bitcast [8 x i8]* %5 to i8*
%11 = bitcast [8 x i8]* %6 to i8*
%12 = load i32, i32* @BUFSIZ
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
%16 = load i32, i32* %2
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @write(i32 %16, i8* %17, i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = icmp ne i32 %21, %23
br i1 %24, label %25, label %28
25: ; preds = %1
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %30
28: ; preds = %1
%29 = call i32 @sleep(i32 1)
br label %30
30: ; preds = %28, %25
%31 = load i32, i32* %2
%32 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = icmp ne i32 %36, %38
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %45
43: ; preds = %30
%44 = call i32 @sleep(i32 1)
br label %45
45: ; preds = %43, %40
br label %46
46: ; preds = %62, %45
br label %47
47: ; preds = %46
%48 = load i32, i32* %2
%49 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%50 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @write(i32 %48, i8* %49, i32 %51)
store i32 %52, i32* %3
%53 = load i32, i32* %3
%54 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%55 = call i32 @strlen(i8* %54)
%56 = icmp ne i32 %53, %55
br i1 %56, label %57, label %60
57: ; preds = %47
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %62
60: ; preds = %47
%61 = call i32 @sleep(i32 1)
br label %62
62: ; preds = %60, %57
%63 = load i32, i32* %2
%64 = load i32, i32* @BUFSIZ
%65 = call i32 @read(i32 %63, i8* %15, i32 %64)
%66 = call i32 @sleep(i32 1)
br label %46
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"Error Clearing memory!\0A\00"
define i32 @wu_clear(i32 %0) {
%2 = alloca i16
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i16* %2 to i8*
store i16 24, i16* %2
%6 = bitcast i64* %3 to i8*
store i64 16659990625146403, i64* %3
%7 = bitcast i64* %4 to i8*
store i64 16659990625145379, i64* %4
%8 = load i32, i32* @BUFSIZ
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = call i32 @strlen(i8* %5)
%12 = call i32 @write(i32 %0, i8* %5, i32 %11)
%13 = call i32 @strlen(i8* %5)
%14 = icmp eq i32 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %20
18: ; preds = %1
%19 = call i32 @sleep(i32 1)
br label %20
20: ; preds = %18, %15
%21 = call i32 @strlen(i8* %6)
%22 = call i32 @write(i32 %0, i8* %6, i32 %21)
%23 = call i32 @strlen(i8* %6)
%24 = icmp eq i32 %22, %23
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %30
28: ; preds = %20
%29 = call i32 @sleep(i32 1)
br label %30
30: ; preds = %28, %25
br label %31
31: ; preds = %30, %41
%32 = call i32 @strlen(i8* %7)
%33 = call i32 @write(i32 %0, i8* %7, i32 %32)
%34 = call i32 @strlen(i8* %7)
%35 = icmp eq i32 %33, %34
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %41
39: ; preds = %31
%40 = call i32 @sleep(i32 1)
br label %41
41: ; preds = %39, %36
%42 = load i32, i32* @BUFSIZ
%43 = call i32 @read(i32 %0, i8* %10, i32 %42)
%44 = call i32 @sleep(i32 1)
br label %31
}