#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 MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
scalar_t__ memcmp(int *, int *, int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int read_mem(int mb_count, int value) {
  int i;
  char *ptr = NULL;
  int *cur = NULL;
  int buf[4] = {value};
  do {
    ptr = mmap(NULL, mb_count * (1024 * 1024) * sizeof(char),
               PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  } while (ptr == MAP_FAILED);
  cur = (int *)ptr;
  for (i = 0; i < (mb_count * (1024 * 1024) * sizeof(char)) / (4 * sizeof(int));
       i += 4) {
    if (memcmp(cur, buf, 4 * sizeof(int)) == 0) {
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x30(%rbp),%rcx
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    %rcx,%rax
add    $0x4,%rax
add    $0x10,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
movl   $0x0,(%rax)
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
cmp    %rcx,%rax
mov    %rax,-0x38(%rbp)
jne    115b <read_mem+0x3b>
jmp    1183 <read_mem+0x63>
mov    -0x8(%rbp),%eax
shl    $0x14,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
mov    0x2e2f(%rip),%rax
or     (%rax),%edx
mov    0x2e36(%rip),%rax
mov    (%rax),%ecx
mov    0x2e15(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
je     1183 <read_mem+0x63>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
movslq -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
shl    $0x14,%ecx
movslq %ecx,%rcx
shl    $0x0,%rcx
shr    $0x4,%rcx
cmp    %rcx,%rax
jae    1247 <read_mem+0x127>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    1234 <read_mem+0x114>
movl   $0x1,-0x4(%rbp)
jmp    124e <read_mem+0x12e>
jmp    1239 <read_mem+0x119>
mov    -0x10(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x10(%rbp)
jmp    11ee <read_mem+0xce>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    %esi,0x10(%rsp)
movq   $0x0,0x14(%rsp)
movl   $0x0,0x1c(%rsp)
mov    %edi,0xc(%rsp)
shl    $0x14,%ebx
mov    0x2e7d(%rip),%r12
mov    0x2e86(%rip),%r13
mov    0x2e67(%rip),%r14
mov    0x2e58(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    0x2e7d(%rip),%rax
or     (%rax),%edx
mov    (%r14),%ecx
or     0x0(%r13),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    (%r15),%rax
je     1170 <read_mem+0x50>
cmpl   $0x0,0xc(%rsp)
je     11de <read_mem+0xbe>
mov    %rax,%rbp
movslq %ebx,%r15
shr    $0x4,%r15
xor    %ebx,%ebx
lea    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     11e2 <read_mem+0xc2>
add    $0x4,%rbx
cmp    %rbx,%r15
ja     11c0 <read_mem+0xa0>
xor    %eax,%eax
jmp    11e7 <read_mem+0xc7>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i32 @read_mem(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [4 x i32]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* null, i8** %7
store i32* null, i32** %8
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%11 = load i32, i32* %5
store i32 %11, i32* %10
%12 = getelementptr inbounds i32, i32* %10, i64 1
%13 = getelementptr inbounds i32, i32* %10, i64 4
br label %14
14: ; preds = %14, %2
%15 = phi i32* [ %12, %2 ], [ %16, %14 ]
store i32 0, i32* %15
%16 = getelementptr inbounds i32, i32* %15, i64 1
%17 = icmp eq i32* %16, %13
br i1 %17, label %18, label %14
18: ; preds = %14
br label %19
19: ; preds = %32, %18
%20 = load i32, i32* %4
%21 = mul nsw i32 %20, 1048576
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 1
%24 = trunc i64 %23 to i32
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_ANONYMOUS
%29 = load i32, i32* @MAP_PRIVATE
%30 = or i32 %28, %29
%31 = call i8* @mmap(i32* null, i32 %24, i32 %27, i32 %30, i32 -1, i32 0)
store i8* %31, i8** %7
br label %32
32: ; preds = %19
%33 = load i8*, i8** %7
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %19, label %36
36: ; preds = %32
%37 = load i8*, i8** %7
%38 = bitcast i8* %37 to i32*
store i32* %38, i32** %8
store i32 0, i32* %6
br label %39
39: ; preds = %55, %36
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = load i32, i32* %4
%43 = mul nsw i32 %42, 1048576
%44 = sext i32 %43 to i64
%45 = mul i64 %44, 1
%46 = udiv i64 %45, 16
%47 = icmp ult i64 %41, %46
br i1 %47, label %48, label %58
48: ; preds = %39
%49 = load i32*, i32** %8
%50 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%51 = call i64 @memcmp(i32* %49, i32* %50, i32 16)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %48
store i32 1, i32* %3
br label %59
54: ; preds = %48
br label %55
55: ; preds = %54
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 4
store i32 %57, i32* %6
br label %39
58: ; preds = %39
store i32 0, i32* %3
br label %59
59: ; preds = %58, %53
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i32 @read_mem(i32 %0, i32 %1) {
%3 = alloca [4 x i32]
%4 = bitcast [4 x i32]* %3 to i8*
%5 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
store i32 %1, i32* %5
%6 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%7 = bitcast i32* %6 to i8*
%8 = shl nsw i32 %0, 20
br label %9
9: ; preds = %2, %9
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_ANONYMOUS
%14 = load i32, i32* @MAP_PRIVATE
%15 = or i32 %14, %13
%16 = call i8* @mmap(i32* null, i32 %8, i32 %12, i32 %15, i32 -1, i32 0)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %9, label %19
19: ; preds = %9
%20 = sext i32 %8 to i64
%21 = bitcast i8* %16 to i32*
%22 = lshr exact i64 %20, 4
%23 = icmp eq i32 %0, 0
br i1 %23, label %31, label %26
24: ; preds = %26
%25 = icmp ugt i64 %22, %30
br i1 %25, label %26, label %31
26: ; preds = %19, %24
%27 = phi i64 [ %30, %24 ], [ 0, %19 ]
%28 = call i64 @memcmp(i32* %21, i32* %5, i32 16)
%29 = icmp eq i64 %28, 0
%30 = add nuw nsw i64 %27, 4
br i1 %29, label %31, label %24
31: ; preds = %26, %24, %19
%32 = phi i32 [ 0, %19 ], [ 0, %24 ], [ 1, %26 ]
ret i32 %32
}