#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_post(int);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/);
int main() {
  create_semaphore();
  create_shared_memory();
  int *x = attach_shared_memory1();
  int *y = attach_shared_memory2();
  while (1) {
    sem_wait(0);
    printf("p2: X = %d\n", *x);
    *y = *x + 1;
    sem_post(1);
  }
  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   1090 <create_shared_memory@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)
xor    %edi,%edi
call   1030 <sem_wait@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x1,%edi
call   1050 <sem_post@plt>
jmp    11a3 <main+0x33>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <create_semaphore@plt>
xor    %eax,%eax
call   1090 <create_shared_memory@plt>
xor    %eax,%eax
call   1060 <attach_shared_memory1@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <attach_shared_memory2@plt>
mov    %rax,%r14
lea    0xe62(%rip),%r15
xchg   %ax,%ax
xor    %edi,%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)
mov    $0x1,%edi
call   1050 <sem_post@plt>
jmp    11a0 <main+0x30>

LLVM IR (-O0):
@.str = [12 x i8] c"p2: X = %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* (...) @attach_shared_memory1()
store i32* %6, i32** %2
%7 = call i32* (...) @attach_shared_memory2()
store i32* %7, i32** %3
br label %8
8: ; preds = %0, %8
%9 = call i32 @sem_wait(i32 0)
%10 = load i32*, i32** %2
%11 = load i32, i32* %10
%12 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32*, i32** %2
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 1
%16 = load i32*, i32** %3
store i32 %15, i32* %16
%17 = call i32 @sem_post(i32 1)
br label %8
}

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