#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 */
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int semget(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ set_sem(int, int);
void *shmat(int, void *, int /*<<< orphan*/);
int shmdt(void *);
int shmget(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
void receive_it(void) {
  int shmid, semid, i;
  void *shm_area;
  if ((shmid = shmget((key_t)26, 8196, 0)) == -1) {
    perror("shmget");
    exit(errno);
  };
  if ((shm_area = shmat(shmid, (void *)0, 0)) == (void *)-1) {
    perror("shmat");
    exit(errno);
  };
  printf("RCV: Memory attached at %lX\n", (unsigned long)shm_area);
  if ((semid = semget((key_t)26, 1, 0)) == -1) {
    perror("semget");
    exit(errno);
  };
  for (i = 0; i < 4; i++) {
    set_sem(semid, -1);
    fprintf(stdout, "%s\n", (char *)shm_area);
    set_sem(semid, 1);
    usleep(1000);
  }
  printf("RCV has successfully completed\n");
  if (shmdt(shm_area) == -1) {
    perror("shmdt");
    exit(errno);
  };
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<receive_it>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1a,%edi
mov    $0x2004,%esi
xor    %edx,%edx
call   10a0 <shmget@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11df <receive_it+0x3f>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    121b <receive_it+0x7b>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1a,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1264 <receive_it+0xc4>
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    12c0 <receive_it+0x120>
mov    -0x8(%rbp),%edi
mov    $0xffffffff,%esi
call   1050 <set_sem@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd9b(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1050 <set_sem@plt>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    126b <receive_it+0xcb>
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    12fa <receive_it+0x15a>
lea    0xd6e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<receive_it>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1a,%edi
mov    $0x2004,%esi
xor    %edx,%edx
call   10b0 <shmget@plt>
cmp    $0xffffffff,%eax
jne    11d9 <receive_it+0x29>
lea    0xe2c(%rip),%rdi
jmp    131c <receive_it+0x16c>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11f6 <receive_it+0x46>
lea    0xe16(%rip),%rdi
jmp    131c <receive_it+0x16c>
mov    %rax,%rbx
lea    0xe0d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1a,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
jne    122c <receive_it+0x7c>
lea    0xe03(%rip),%rdi
jmp    131c <receive_it+0x16c>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0xffffffff,%esi
call   1060 <set_sem@plt>
mov    0x2d97(%rip),%r15
mov    (%r15),%edi
lea    0xde6(%rip),%r14
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <set_sem@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1060 <set_sem@plt>
mov    (%r15),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <set_sem@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1060 <set_sem@plt>
mov    (%r15),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <set_sem@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1060 <set_sem@plt>
mov    (%r15),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <set_sem@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
lea    0xd38(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    132f <receive_it+0x17f>
lea    0xd19(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"shmget\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [29 x i8] c"RCV: Memory attached at %lX\0A\00"
@.str.3 = [7 x i8] c"semget\00"
@stdout = global i32 0
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [32 x i8] c"RCV has successfully completed\0A\00"
@.str.6 = [6 x i8] c"shmdt\00"
@EXIT_SUCCESS = global i32 0
define void @receive_it() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = call i32 @shmget(i32 26, i32 8196, 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 ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* %1
%13 = call i8* @shmat(i32 %12, i8* null, i32 0)
store i8* %13, i8** %4
%14 = icmp eq i8* %13, inttoptr (i64 -1 to i8*)
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @errno
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = ptrtoint i8* %20 to i64
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %21)
%23 = call i32 @semget(i32 26, i32 1, i32 0)
store i32 %23, i32* %2
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* @errno
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
store i32 0, i32* %3
br label %30
30: ; preds = %42, %29
%31 = load i32, i32* %3
%32 = icmp slt i32 %31, 4
br i1 %32, label %33, label %45
33: ; preds = %30
%34 = load i32, i32* %2
%35 = call i32 @set_sem(i32 %34, i32 -1)
%36 = load i32, i32* @stdout
%37 = load i8*, i8** %4
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %37)
%39 = load i32, i32* %2
%40 = call i32 @set_sem(i32 %39, i32 1)
%41 = call i32 @usleep(i32 1000)
br label %42
42: ; preds = %33
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
br label %30
45: ; preds = %30
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
%47 = load i8*, i8** %4
%48 = call i32 @shmdt(i8* %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%52 = load i32, i32* @errno
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %45
%55 = load i32, i32* @EXIT_SUCCESS
%56 = call i32 @exit(i32 %55)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [29 x i8] c"RCV: Memory attached at %lX\0A\00"
@.str.3 = [7 x i8] c"semget\00"
@stdout = global i32 0
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.6 = [6 x i8] c"shmdt\00"
@EXIT_SUCCESS = global i32 0
@str = [31 x i8] c"RCV has successfully completed\00"
define void @receive_it() {
%1 = tail call i32 @shmget(i32 26, i32 8196, i32 0)
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @errno
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = tail call i8* @shmat(i32 %1, i8* null, i32 0)
%9 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @errno
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %7
%15 = ptrtoint i8* %8 to i64
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %15)
%17 = tail call i32 @semget(i32 26, i32 1, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %43, label %19
19: ; preds = %14
%20 = tail call i32 @set_sem(i32 %17, i32 -1)
%21 = load i32, i32* @stdout
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%23 = tail call i32 @set_sem(i32 %17, i32 1)
%24 = tail call i32 @usleep(i32 1000)
%25 = tail call i32 @set_sem(i32 %17, i32 -1)
%26 = load i32, i32* @stdout
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%28 = tail call i32 @set_sem(i32 %17, i32 1)
%29 = tail call i32 @usleep(i32 1000)
%30 = tail call i32 @set_sem(i32 %17, i32 -1)
%31 = load i32, i32* @stdout
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%33 = tail call i32 @set_sem(i32 %17, i32 1)
%34 = tail call i32 @usleep(i32 1000)
%35 = tail call i32 @set_sem(i32 %17, i32 -1)
%36 = load i32, i32* @stdout
%37 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%38 = tail call i32 @set_sem(i32 %17, i32 1)
%39 = tail call i32 @usleep(i32 1000)
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%41 = tail call i32 @shmdt(i8* %8)
%42 = icmp eq i32 %41, -1
br i1 %42, label %47, label %51
43: ; preds = %14
%44 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* @errno
%46 = tail call i32 @exit(i32 %45)
unreachable
47: ; preds = %19
%48 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%49 = load i32, i32* @errno
%50 = tail call i32 @exit(i32 %49)
unreachable
51: ; preds = %19
%52 = load i32, i32* @EXIT_SUCCESS
%53 = tail call i32 @exit(i32 %52)
unreachable
}