#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 = 0;
  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)
movb   $0x0,-0x9(%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     119e <memtest+0x4e>
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     11db <memtest+0x8b>
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     1213 <memtest+0xc3>
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   %r14
push   %rbx
mov    %esi,%r14d
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>
mov    %eax,%ebx
mov    %eax,%edi
and    $0xfffbffff,%edi
call   1070 <io_store_eflags@plt>
test   $0x40000,%ebx
jne    1198 <memtest+0x48>
mov    %ebp,%edi
mov    %r14d,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <memtest_sub@plt>
xor    %eax,%eax
call   1030 <load_cr0@plt>
or     $0x60000000,%eax
mov    %eax,%edi
call   1060 <store_cr0@plt>
mov    %ebp,%edi
mov    %r14d,%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
pop    %rbx
pop    %r14
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
store i8 0, i8* %5
%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, 262144
%8 = icmp eq i32 %7, 0
%9 = and i32 %6, -262145
%10 = tail call i32 @io_store_eflags(i32 %9)
br i1 %8, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @memtest_sub(i32 %0, i32 %1)
br label %21
13: ; preds = %2
%14 = tail call i32 (...) @load_cr0()
%15 = or i32 %14, 1610612736
%16 = tail call i32 @store_cr0(i32 %15)
%17 = tail call i32 @memtest_sub(i32 %0, i32 %1)
%18 = tail call i32 (...) @load_cr0()
%19 = and i32 %18, -1610612737
%20 = tail call i32 @store_cr0(i32 %19)
br label %21
21: ; preds = %11, %13
%22 = phi i32 [ %12, %11 ], [ %17, %13 ]
ret i32 %22
}