#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 O_CREAT;
int O_EXCL;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int, int, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_unlink(char *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  sem_t *mysem;
  int i;
  mysem = sem_open("/my_sem", O_CREAT | O_EXCL, 0666, 1);
  for (i = 0; i < 4; i++) {
    sem_wait(mysem);
    printf("Entering critical section, %d, pid=%d\n", i, getpid());
    sleep(3);
    printf(" Leaving critical section, %d, pid=%d\n", i, getpid());
    sem_post(mysem);
    sleep(3);
  }
  printf("PROTECT region exited for pid=%d\n", getpid());
  sem_close(mysem);
  sem_unlink("/my_sem");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
lea    0xe41(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x1,%ecx
call   1070 <sem_open@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    1257 <main+0xc7>
mov    -0x18(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <sem_post@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d9 <main+0x49>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <sem_close@plt>
lea    0xd82(%rip),%rdi
call   10b0 <sem_unlink@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e59(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4f(%rip),%r14
mov    %r14,%rdi
mov    $0x1b6,%edx
mov    $0x1,%ecx
call   1070 <sem_open@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <sem_wait@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe2c(%rip),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe2d(%rip),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    $0x2,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    $0x2,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    $0x3,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    $0x3,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd2a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <sem_close@plt>
mov    %r14,%rdi
call   10b0 <sem_unlink@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/my_sem\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [39 x i8] c"Entering critical section, %d, pid=%d\0A\00"
@.str.2 = [39 x i8] c" Leaving critical section, %d, pid=%d\0A\00"
@.str.3 = [34 x i8] c"PROTECT region exited for pid=%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %10, i32 438, i32 1)
store i32* %11, i32** %6
store i32 0, i32* %7
br label %12
12: ; preds = %28, %2
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %31
15: ; preds = %12
%16 = load i32*, i32** %6
%17 = call i32 @sem_wait(i32* %16)
%18 = load i32, i32* %7
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = call i32 @sleep(i32 3)
%22 = load i32, i32* %7
%23 = call i32 (...) @getpid()
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i32*, i32** %6
%26 = call i32 @sem_post(i32* %25)
%27 = call i32 @sleep(i32 3)
br label %28
28: ; preds = %15
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %12
31: ; preds = %12
%32 = call i32 (...) @getpid()
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = load i32*, i32** %6
%35 = call i32 @sem_close(i32* %34)
%36 = call i32 @sem_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"/my_sem\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [39 x i8] c"Entering critical section, %d, pid=%d\0A\00"
@.str.2 = [39 x i8] c" Leaving critical section, %d, pid=%d\0A\00"
@.str.3 = [34 x i8] c"PROTECT region exited for pid=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_EXCL
%5 = or i32 %4, %3
%6 = tail call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5, i32 438, i32 1)
%7 = tail call i32 @sem_wait(i32* %6)
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %8)
%10 = tail call i32 @sleep(i32 3)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %11)
%13 = tail call i32 @sem_post(i32* %6)
%14 = tail call i32 @sleep(i32 3)
%15 = tail call i32 @sem_wait(i32* %6)
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %16)
%18 = tail call i32 @sleep(i32 3)
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %19)
%21 = tail call i32 @sem_post(i32* %6)
%22 = tail call i32 @sleep(i32 3)
%23 = tail call i32 @sem_wait(i32* %6)
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 %24)
%26 = tail call i32 @sleep(i32 3)
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %27)
%29 = tail call i32 @sem_post(i32* %6)
%30 = tail call i32 @sleep(i32 3)
%31 = tail call i32 @sem_wait(i32* %6)
%32 = tail call i32 (...) @getpid()
%33 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 3, i32 %32)
%34 = tail call i32 @sleep(i32 3)
%35 = tail call i32 (...) @getpid()
%36 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 3, i32 %35)
%37 = tail call i32 @sem_post(i32* %6)
%38 = tail call i32 @sleep(i32 3)
%39 = tail call i32 (...) @getpid()
%40 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = tail call i32 @sem_close(i32* %6)
%42 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%43 = tail call i32 @exit(i32 0)
unreachable
}