#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 *attach_shared_memory1();
int *attach_shared_memory2();
int /*<<< orphan*/ create_semaphore();
int /*<<< orphan*/ create_shared_memory();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sem_init();
int /*<<< orphan*/ sem_post(int /*<<< orphan*/);
int /*<<< orphan*/ sem_wait(int);
int main() {
  create_semaphore();
  create_shared_memory();
  sem_init();
  int *x = attach_shared_memory1();
  int *y = attach_shared_memory2();
  *x = 0;
  sem_post(0);
  while (1) {
    sem_wait(1);
    printf("p1: Y = %d\n", *y);
    *x = *y + 1;
    sem_post(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <create_semaphore@plt>
mov    $0x0,%al
call   10a0 <create_shared_memory@plt>
mov    $0x0,%al
call   1090 <sem_init@plt>
mov    $0x0,%al
call   1060 <attach_shared_memory1@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1070 <attach_shared_memory2@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
xor    %edi,%edi
call   1050 <sem_post@plt>
mov    $0x1,%edi
call   1030 <sem_wait@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
xor    %edi,%edi
call   1050 <sem_post@plt>
jmp    11cb <main+0x4b>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <create_semaphore@plt>
xor    %eax,%eax
call   10a0 <create_shared_memory@plt>
xor    %eax,%eax
call   1090 <sem_init@plt>
xor    %eax,%eax
call   1060 <attach_shared_memory1@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1070 <attach_shared_memory2@plt>
mov    %rax,%rbx
movl   $0x0,(%r14)
xor    %edi,%edi
call   1050 <sem_post@plt>
lea    0xe3d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
mov    %r15,%rdi
call   1040 <printf@plt>
mov    (%rbx),%eax
add    $0x1,%eax
mov    %eax,(%r14)
xor    %edi,%edi
call   1050 <sem_post@plt>
jmp    11d0 <main+0x50>

LLVM IR (-O0):
@.str = [12 x i8] c"p1: Y = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32 (...) @create_semaphore()
%5 = call i32 (...) @create_shared_memory()
%6 = call i32 (...) @sem_init()
%7 = call i32* (...) @attach_shared_memory1()
store i32* %7, i32** %2
%8 = call i32* (...) @attach_shared_memory2()
store i32* %8, i32** %3
%9 = load i32*, i32** %2
store i32 0, i32* %9
%10 = call i32 @sem_post(i32 0)
br label %11
11: ; preds = %0, %11
%12 = call i32 @sem_wait(i32 1)
%13 = load i32*, i32** %3
%14 = load i32, i32* %13
%15 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32*, i32** %3
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
%19 = load i32*, i32** %2
store i32 %18, i32* %19
%20 = call i32 @sem_post(i32 0)
br label %11
}

LLVM IR (-O2):
@.str = [12 x i8] c"p1: Y = %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @create_semaphore()
%2 = tail call i32 (...) @create_shared_memory()
%3 = tail call i32 (...) @sem_init()
%4 = tail call i32* (...) @attach_shared_memory1()
%5 = tail call i32* (...) @attach_shared_memory2()
store i32 0, i32* %4
%6 = tail call i32 @sem_post(i32 0)
br label %7
7: ; preds = %7, %0
%8 = tail call i32 @sem_wait(i32 1)
%9 = load i32, i32* %5
%10 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
store i32 %12, i32* %4
%13 = tail call i32 @sem_post(i32 0)
br label %7
}