#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 /*<<< orphan*/ EXIT_SUCCESS;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int *, void *, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
scalar_t__ munmap(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int shm_open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
void receive_it(void) {
  int shm_fd, iflag = 8;
  void *shm_area;
  if ((shm_fd = shm_open("/my_shm", O_RDWR, 0)) == -1) {
    perror("shm_open");
    exit(errno);
  };
  shm_area = mmap(NULL, 8196, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  if (shm_area == MAP_FAILED) {
    perror("mmap");
    exit(errno);
  };
  printf("RCV: Memory attached at %lX\n", (unsigned long)shm_area);
  memcpy(shm_area, &iflag, 4);
  printf("iflag is now = %d\n", iflag);
  while (iflag == 8) {
    memcpy(&iflag, shm_area, 4);
    sleep(1);
  }
  printf("RCV has successfully completed\n");
  printf("iflag is now = %d\n", iflag);
  if (munmap(shm_area, 8196)) {
    perror("munmap");
    exit(errno);
  };
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<receive_it>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x8,-0x8(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
xor    %edx,%edx
call   1090 <shm_open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11cc <receive_it+0x4c>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
mov    0x2de4(%rip),%rax
or     (%rax),%edx
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2004,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%rax
jne    122e <receive_it+0xae>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x8,-0x8(%rbp)
jne    128e <receive_it+0x10e>
mov    -0x10(%rbp),%rsi
lea    -0x8(%rbp),%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1263 <receive_it+0xe3>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2004,%esi
call   1060 <munmap@plt>
cmp    $0x0,%rax
je     12df <receive_it+0x15f>
lea    0xd9a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<receive_it>:
push   %r14
push   %rbx
push   %rax
movl   $0x8,0x4(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdi
xor    %edx,%edx
call   10a0 <shm_open@plt>
cmp    $0xffffffff,%eax
jne    11c4 <receive_it+0x34>
lea    0xe49(%rip),%rdi
jmp    12b6 <receive_it+0x126>
mov    0x2e2d(%rip),%rcx
mov    0x2dee(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x2004,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    0x2db5(%rip),%rax
cmp    (%rax),%r14
jne    120c <receive_it+0x7c>
lea    0xe0a(%rip),%rdi
jmp    12b6 <receive_it+0x126>
lea    0xe03(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x4(%rsp),%esi
lea    0xdf9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x8,0x4(%rsp)
jne    1271 <receive_it+0xe1>
lea    0x4(%rsp),%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
cmpl   $0x8,0x4(%rsp)
je     1250 <receive_it+0xc0>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%esi
lea    0xdab(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x2004,%esi
call   1070 <munmap@plt>
test   %rax,%rax
jne    12af <receive_it+0x11f>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/my_shm\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"shm_open\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [29 x i8] c"RCV: Memory attached at %lX\0A\00"
@.str.4 = [19 x i8] c"iflag is now = %d\0A\00"
@.str.5 = [32 x i8] c"RCV has successfully completed\0A\00"
@.str.6 = [7 x i8] c"munmap\00"
@EXIT_SUCCESS = global i32 0
define void @receive_it() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 8, i32* %2
%4 = load i32, i32* @O_RDWR
%5 = call i32 @shm_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
store i32 %5, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* %1
%17 = call i8* @mmap(i32* null, i32 8196, i32 %14, i32 %15, i32 %16, i32 0)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %25
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @errno
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %11
%26 = load i8*, i8** %3
%27 = ptrtoint i8* %26 to i64
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %27)
%29 = load i8*, i8** %3
%30 = bitcast i8* %29 to i32*
%31 = bitcast i32* %2 to i8*
%32 = call i32 @memcpy(i32* %30, i8* %31, i32 4)
%33 = load i32, i32* %2
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %38, %25
%36 = load i32, i32* %2
%37 = icmp eq i32 %36, 8
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i8*, i8** %3
%40 = call i32 @memcpy(i32* %2, i8* %39, i32 4)
%41 = call i32 @sleep(i32 1)
br label %35
42: ; preds = %35
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
%44 = load i32, i32* %2
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i8*, i8** %3
%47 = call i64 @munmap(i8* %46, i32 8196)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %42
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* @errno
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %42
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @exit(i32 %54)
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"/my_shm\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"shm_open\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [29 x i8] c"RCV: Memory attached at %lX\0A\00"
@.str.4 = [19 x i8] c"iflag is now = %d\0A\00"
@.str.6 = [7 x i8] c"munmap\00"
@EXIT_SUCCESS = global i32 0
@str = [31 x i8] c"RCV has successfully completed\00"
define void @receive_it() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 8, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @shm_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @errno
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32* null, i32 8196, i32 %13, i32 %14, i32 %4, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %22
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @errno
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %10
%23 = ptrtoint i8* %15 to i64
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %23)
%25 = bitcast i8* %15 to i32*
%26 = call i32 @memcpy(i32* %25, i8* %2, i32 4)
%27 = load i32, i32* %1
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %1
%30 = icmp eq i32 %29, 8
br i1 %30, label %31, label %36
31: ; preds = %22, %31
%32 = call i32 @memcpy(i32* %1, i8* %15, i32 4)
%33 = call i32 @sleep(i32 1)
%34 = load i32, i32* %1
%35 = icmp eq i32 %34, 8
br i1 %35, label %31, label %36
36: ; preds = %31, %22
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%38 = load i32, i32* %1
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = call i64 @munmap(i8* %15, i32 8196)
%41 = icmp eq i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%44 = load i32, i32* @errno
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %36
%47 = load i32, i32* @EXIT_SUCCESS
%48 = call i32 @exit(i32 %47)
unreachable
}