#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*/ sem_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memcpy(void *, int *, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int shm_open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sleep(int);
void writer() {
  sem_t *rw_bin_sem = sem_open("/rw_bin_sem", O_RDWR);
  if (rw_bin_sem == SEM_FAILED) {
    die("sem_open()");
  }
  int shm_fd;
  if ((shm_fd = shm_open("/buffer", O_RDWR, 0666)) < 0) {
    die("shm_open()");
  }
  void *buffer_addr =
      mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  int value = 0;
  while (1) {
    if (value == 10) {
      munmap(buffer_addr, sizeof(int));
      exit(0);
    }
    sleep(1);
    sem_wait(rw_bin_sem);
    memcpy(&value, buffer_addr, sizeof(int));
    printf("The writer (%lu) writes the value %d\n", getpid(), value);
    value++;
    memcpy(buffer_addr, &value, sizeof(int));
    sem_post(rw_bin_sem);
  }
}


x86 assembly (clang, -O0):
<writer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe28(%rip),%rdi
call   1080 <sem_open@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
jne    1201 <writer+0x41>
lea    0xe10(%rip),%rdi
call   10b0 <die@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
mov    $0x1b6,%edx
call   10d0 <shm_open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1233 <writer+0x73>
lea    0xdf1(%rip),%rdi
call   10b0 <die@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    0x2d7d(%rip),%rax
or     (%rax),%edx
mov    0x2d9c(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jne    128d <writer+0xcd>
mov    -0x18(%rbp),%rdi
mov    $0x4,%esi
call   10a0 <munmap@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    -0x8(%rbp),%rdi
call   1030 <sem_wait@plt>
lea    -0x1c(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x1c(%rbp),%edx
lea    0xd65(%rip),%rdi
call   1060 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1070 <sem_post@plt>
jmp    126e <writer+0xae>

x86 assembly (clang, -O2):
<writer>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e09(%rip),%rbx
mov    (%rbx),%esi
lea    0xe28(%rip),%rdi
call   1080 <sem_open@plt>
mov    %rax,%r14
mov    0x2de9(%rip),%rax
cmp    (%rax),%r14
jne    11f8 <writer+0x38>
lea    0xe19(%rip),%rdi
call   10b0 <die@plt>
mov    (%rbx),%esi
lea    0xe16(%rip),%rdi
mov    $0x1b6,%edx
call   10d0 <shm_open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    121d <writer+0x5d>
lea    0xe07(%rip),%rdi
call   10b0 <die@plt>
mov    0x2dd4(%rip),%rax
mov    0x2d95(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2db2(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%r15
lea    0xdc9(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    %r14,%rdi
call   1030 <sem_wait@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x4(%rsp),%edx
mov    %r12,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
addl   $0x1,0x4(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    %r14,%rdi
call   1070 <sem_post@plt>
cmpl   $0xa,0x4(%rsp)
jne    1270 <writer+0xb0>
mov    %rbx,%rdi
mov    $0x4,%esi
call   10a0 <munmap@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/rw_bin_sem\00"
@O_RDWR = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [11 x i8] c"sem_open()\00"
@.str.2 = [8 x i8] c"/buffer\00"
@.str.3 = [11 x i8] c"shm_open()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [38 x i8] c"The writer (%lu) writes the value %d\0A\00"
define void @writer() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = load i32, i32* @O_RDWR
%6 = call i32* @sem_open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32* %6, i32** %1
%7 = load i32*, i32** %1
%8 = load i32*, i32** @SEM_FAILED
%9 = icmp eq i32* %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* @O_RDWR
%14 = call i32 @shm_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 438)
store i32 %14, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %2
%24 = call i8* @mmap(i32* null, i32 4, i32 %21, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %3
store i32 0, i32* %4
br label %25
25: ; preds = %18, %32
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i8*, i8** %3
%30 = call i32 @munmap(i8* %29, i32 4)
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %25
%33 = call i32 @sleep(i32 1)
%34 = load i32*, i32** %1
%35 = call i32 @sem_wait(i32* %34)
%36 = bitcast i32* %4 to i8*
%37 = load i8*, i8** %3
%38 = bitcast i8* %37 to i32*
%39 = call i32 @memcpy(i8* %36, i32* %38, i32 4)
%40 = call i32 (...) @getpid()
%41 = load i32, i32* %4
%42 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 %41)
%43 = load i32, i32* %4
%44 = add nsw i32 %43, 1
store i32 %44, i32* %4
%45 = load i8*, i8** %3
%46 = call i32 @memcpy(i8* %45, i32* %4, i32 4)
%47 = load i32*, i32** %1
%48 = call i32 @sem_post(i32* %47)
br label %25
}

LLVM IR (-O2):
@.str = [12 x i8] c"/rw_bin_sem\00"
@O_RDWR = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [11 x i8] c"sem_open()\00"
@.str.2 = [8 x i8] c"/buffer\00"
@.str.3 = [11 x i8] c"shm_open()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [38 x i8] c"The writer (%lu) writes the value %d\0A\00"
define void @writer() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = tail call i32* @sem_open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = load i32*, i32** @SEM_FAILED
%5 = icmp eq i32* %3, %4
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @shm_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 438)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = tail call i8* @mmap(i32* null, i32 4, i32 %17, i32 %18, i32 %10, i32 0)
%20 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%21 = bitcast i8* %19 to i32*
br label %25
22: ; preds = %25
%23 = call i32 @munmap(i8* %19, i32 4)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %14, %25
%26 = call i32 @sleep(i32 1)
%27 = call i32 @sem_wait(i32* %3)
%28 = call i32 @memcpy(i8* %20, i32* %21, i32 4)
%29 = call i32 (...) @getpid()
%30 = load i32, i32* %1
%31 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i32, i32* %1
%33 = add nsw i32 %32, 1
store i32 %33, i32* %1
%34 = call i32 @memcpy(i8* %19, i32* %1, i32 4)
%35 = call i32 @sem_post(i32* %3)
%36 = load i32, i32* %1
%37 = icmp eq i32 %36, 10
br i1 %37, label %22, label %25
}