#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 */
unsigned int io_load_eflags();
int /*<<< orphan*/ io_store_eflags(unsigned int);
unsigned int load_cr0();
unsigned int memtest_sub(unsigned int, unsigned int);
int /*<<< orphan*/ store_cr0(unsigned int);
unsigned int memtest(unsigned int start, unsigned int end) {
  char flg486;
  unsigned int eflg, cr0, i;
  eflg = io_load_eflags();
  eflg |= 0x00040000;
  io_store_eflags(eflg);
  eflg = io_load_eflags();
  if ((eflg & 0x00040000) != 0) {
    flg486 = 1;
  }
  eflg &= ~0x00040000;
  io_store_eflags(eflg);
  if (flg486 != 0) {
    cr0 = load_cr0();
    cr0 |= 0x60000000;
    store_cr0(cr0);
  }
  i = memtest_sub(start, end);
  if (flg486 != 0) {
    cr0 = load_cr0();
    cr0 &= ~0x60000000;
    store_cr0(cr0);
  }
  return i;
}


x86 assembly (clang, -O0):
<memtest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <io_load_eflags@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
or     $0x40000,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <io_store_eflags@plt>
mov    $0x0,%al
call   1040 <io_load_eflags@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
and    $0x40000,%eax
cmp    $0x0,%eax
je     119a <memtest+0x4a>
movb   $0x1,-0x9(%rbp)
mov    -0x10(%rbp),%eax
and    $0xfffbffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <io_store_eflags@plt>
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
je     11d7 <memtest+0x87>
mov    $0x0,%al
call   1030 <load_cr0@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
or     $0x60000000,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <store_cr0@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <memtest_sub@plt>
mov    %eax,-0x18(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
je     120f <memtest+0xbf>
mov    $0x0,%al
call   1030 <load_cr0@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0x9fffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <store_cr0@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memtest>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <io_load_eflags@plt>
or     $0x40000,%eax
mov    %eax,%edi
call   1070 <io_store_eflags@plt>
xor    %eax,%eax
call   1040 <io_load_eflags@plt>
and    $0xfffbffff,%eax
mov    %eax,%edi
call   1070 <io_store_eflags@plt>
xor    %eax,%eax
call   1030 <load_cr0@plt>
or     $0x60000000,%eax
mov    %eax,%edi
call   1060 <store_cr0@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1050 <memtest_sub@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <load_cr0@plt>
and    $0x9fffffff,%eax
mov    %eax,%edi
call   1060 <store_cr0@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @memtest(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = call i32 (...) @io_load_eflags()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = or i32 %10, 262144
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = call i32 @io_store_eflags(i32 %12)
%14 = call i32 (...) @io_load_eflags()
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = and i32 %15, 262144
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i8 1, i8* %5
br label %19
19: ; preds = %18, %2
%20 = load i32, i32* %6
%21 = and i32 %20, -262145
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = call i32 @io_store_eflags(i32 %22)
%24 = load i8, i8* %5
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %19
%28 = call i32 (...) @load_cr0()
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = or i32 %29, 1610612736
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = call i32 @store_cr0(i32 %31)
br label %33
33: ; preds = %27, %19
%34 = load i32, i32* %3
%35 = load i32, i32* %4
%36 = call i32 @memtest_sub(i32 %34, i32 %35)
store i32 %36, i32* %8
%37 = load i8, i8* %5
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = call i32 (...) @load_cr0()
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = and i32 %42, -1610612737
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = call i32 @store_cr0(i32 %44)
br label %46
46: ; preds = %40, %33
%47 = load i32, i32* %8
ret i32 %47
}

LLVM IR (-O2):
define i32 @memtest(i32 %0, i32 %1) {
%3 = tail call i32 (...) @io_load_eflags()
%4 = or i32 %3, 262144
%5 = tail call i32 @io_store_eflags(i32 %4)
%6 = tail call i32 (...) @io_load_eflags()
%7 = and i32 %6, -262145
%8 = tail call i32 @io_store_eflags(i32 %7)
%9 = tail call i32 (...) @load_cr0()
%10 = or i32 %9, 1610612736
%11 = tail call i32 @store_cr0(i32 %10)
%12 = tail call i32 @memtest_sub(i32 %0, i32 %1)
%13 = tail call i32 (...) @load_cr0()
%14 = and i32 %13, -1610612737
%15 = tail call i32 @store_cr0(i32 %14)
ret i32 %12
}