#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
scalar_t__ sem_post(int /*<<< orphan*/ *);
scalar_t__ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
void protect_it(void) {
  int i;
  sem_t *mysem;
  printf("Attaching to the semaphore\n");
  if ((mysem = sem_open("/my_sem", 0)) == SEM_FAILED) {
    perror("sem_open");
    exit(errno);
  };
  for (i = 0; i < 4; i++) {
    if (sem_wait(mysem)) {
      perror("sem_wait");
      exit(errno);
    };
    printf("Entering critical section, %d, pid=%d\n", i, getpid());
    sleep(3);
    printf(" Leaving critical section, %d, pid=%d\n", i, getpid());
    if (sem_post(mysem)) {
      perror("sem_post");
      exit(errno);
    };
    sleep(3);
  }
  printf("PROTECT region exited for pid=%d\n", getpid());
  if ((sem_close(mysem)) == -1) {
    perror("sem_close");
    exit(errno);
  };
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<protect_it>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe6f(%rip),%rdi
xor    %esi,%esi
call   1070 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
jne    11e2 <protect_it+0x52>
lea    0xe55(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x4,-0x4(%rbp)
jge    12af <protect_it+0x11f>
mov    -0x10(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0x0,%rax
je     1220 <protect_it+0x90>
lea    0xe20(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <sem_post@plt>
cmp    $0x0,%rax
je     1297 <protect_it+0x107>
lea    0xe00(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11e9 <protect_it+0x59>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <sem_close@plt>
cmp    $0xffffffff,%eax
jne    12f2 <protect_it+0x162>
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<protect_it>:
push   %rbx
lea    0xef5(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4c(%rip),%rdi
xor    %esi,%esi
call   1080 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e23(%rip),%rax
cmp    (%rax),%rbx
jne    11d6 <protect_it+0x36>
lea    0xe37(%rip),%rdi
jmp    13c1 <protect_it+0x221>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
jne    13b1 <protect_it+0x211>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe25(%rip),%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe29(%rip),%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1070 <sem_post@plt>
test   %rax,%rax
jne    13ba <protect_it+0x21a>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
jne    13b1 <protect_it+0x211>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdbd(%rip),%rdi
mov    $0x1,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdbe(%rip),%rdi
mov    $0x1,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1070 <sem_post@plt>
test   %rax,%rax
jne    13ba <protect_it+0x21a>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
jne    13b1 <protect_it+0x211>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd4f(%rip),%rdi
mov    $0x2,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd50(%rip),%rdi
mov    $0x2,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1070 <sem_post@plt>
test   %rax,%rax
jne    13ba <protect_it+0x21a>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
jne    13b1 <protect_it+0x211>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xce1(%rip),%rdi
mov    $0x3,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xce2(%rip),%rdi
mov    $0x3,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1070 <sem_post@plt>
test   %rax,%rax
jne    13ba <protect_it+0x21a>
mov    $0x3,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xcdf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1090 <sem_close@plt>
cmp    $0xffffffff,%eax
jne    13d4 <protect_it+0x234>
lea    0xce4(%rip),%rdi
jmp    13c1 <protect_it+0x221>
lea    0xc59(%rip),%rdi
jmp    13c1 <protect_it+0x221>
lea    0xca7(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c0b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2bf5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"Attaching to the semaphore\0A\00"
@.str.1 = [8 x i8] c"/my_sem\00"
@SEM_FAILED = global i32* null
@.str.2 = [9 x i8] c"sem_open\00"
@errno = global i32 0
@.str.3 = [9 x i8] c"sem_wait\00"
@.str.4 = [39 x i8] c"Entering critical section, %d, pid=%d\0A\00"
@.str.5 = [39 x i8] c" Leaving critical section, %d, pid=%d\0A\00"
@.str.6 = [9 x i8] c"sem_post\00"
@.str.7 = [34 x i8] c"PROTECT region exited for pid=%d\0A\00"
@.str.8 = [10 x i8] c"sem_close\00"
@EXIT_SUCCESS = global i32 0
define void @protect_it() {
%1 = alloca i32
%2 = alloca i32*
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%4 = call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32* %4, i32** %2
%5 = load i32*, i32** @SEM_FAILED
%6 = icmp eq i32* %4, %5
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
store i32 0, i32* %1
br label %12
12: ; preds = %40, %11
%13 = load i32, i32* %1
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %43
15: ; preds = %12
%16 = load i32*, i32** %2
%17 = call i64 @sem_wait(i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @errno
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
%24 = load i32, i32* %1
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %24, i32 %25)
%27 = call i32 @sleep(i32 3)
%28 = load i32, i32* %1
%29 = call i32 (...) @getpid()
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 %29)
%31 = load i32*, i32** %2
%32 = call i64 @sem_post(i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %23
%35 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%36 = load i32, i32* @errno
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %23
%39 = call i32 @sleep(i32 3)
br label %40
40: ; preds = %38
%41 = load i32, i32* %1
%42 = add nsw i32 %41, 1
store i32 %42, i32* %1
br label %12
43: ; preds = %12
%44 = call i32 (...) @getpid()
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %44)
%46 = load i32*, i32** %2
%47 = call i32 @sem_close(i32* %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %53
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%51 = load i32, i32* @errno
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %43
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @exit(i32 %54)
unreachable
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"/my_sem\00"
@SEM_FAILED = global i32* null
@.str.2 = [9 x i8] c"sem_open\00"
@errno = global i32 0
@.str.3 = [9 x i8] c"sem_wait\00"
@.str.4 = [39 x i8] c"Entering critical section, %d, pid=%d\0A\00"
@.str.5 = [39 x i8] c" Leaving critical section, %d, pid=%d\0A\00"
@.str.6 = [9 x i8] c"sem_post\00"
@.str.7 = [34 x i8] c"PROTECT region exited for pid=%d\0A\00"
@.str.8 = [10 x i8] c"sem_close\00"
@EXIT_SUCCESS = global i32 0
@str = [27 x i8] c"Attaching to the semaphore\00"
define void @protect_it() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%2 = tail call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 0)
%3 = load i32*, i32** @SEM_FAILED
%4 = icmp eq i32* %2, %3
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = tail call i64 @sem_wait(i32* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %16, label %12
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @errno
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %52, %40, %28, %5
%13 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%14 = load i32, i32* @errno
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %5
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %17)
%19 = tail call i32 @sleep(i32 3)
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 0, i32 %20)
%22 = tail call i64 @sem_post(i32* %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %56, %44, %32, %16
%25 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%26 = load i32, i32* @errno
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %16
%29 = tail call i32 @sleep(i32 3)
%30 = tail call i64 @sem_wait(i32* %2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %12
32: ; preds = %28
%33 = tail call i32 (...) @getpid()
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 %33)
%35 = tail call i32 @sleep(i32 3)
%36 = tail call i32 (...) @getpid()
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 %36)
%38 = tail call i64 @sem_post(i32* %2)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %24
40: ; preds = %32
%41 = tail call i32 @sleep(i32 3)
%42 = tail call i64 @sem_wait(i32* %2)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %12
44: ; preds = %40
%45 = tail call i32 (...) @getpid()
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 2, i32 %45)
%47 = tail call i32 @sleep(i32 3)
%48 = tail call i32 (...) @getpid()
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 2, i32 %48)
%50 = tail call i64 @sem_post(i32* %2)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %24
52: ; preds = %44
%53 = tail call i32 @sleep(i32 3)
%54 = tail call i64 @sem_wait(i32* %2)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %12
56: ; preds = %52
%57 = tail call i32 (...) @getpid()
%58 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 3, i32 %57)
%59 = tail call i32 @sleep(i32 3)
%60 = tail call i32 (...) @getpid()
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 3, i32 %60)
%62 = tail call i64 @sem_post(i32* %2)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %24
64: ; preds = %56
%65 = tail call i32 @sleep(i32 3)
%66 = tail call i32 (...) @getpid()
%67 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %66)
%68 = tail call i32 @sem_close(i32* %2)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %74
70: ; preds = %64
%71 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%72 = load i32, i32* @errno
%73 = tail call i32 @exit(i32 %72)
unreachable
74: ; preds = %64
%75 = load i32, i32* @EXIT_SUCCESS
%76 = tail call i32 @exit(i32 %75)
unreachable
}