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

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int semget(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ set_sem(int, int);
int /*<<< orphan*/ sleep(int);
void protect_it(void) {
  int semid, i;
  if ((semid = semget((key_t)261, 1, 0)) == -1) {
    perror("semget");
    exit(errno);
  };
  for (i = 0; i < 4; i++) {
    set_sem(semid, -1);
    printf("Entering critical section, %d, pid=%d\n", i, getpid());
    sleep(3);
    printf(" Leaving critical section, %d, pid=%d\n", i, getpid());
    set_sem(semid, 1);
    sleep(3);
  }
  printf("PROTECT region exited for pid=%d\n", getpid());
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<protect_it>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x105,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11af <protect_it+0x3f>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    123c <protect_it+0xcc>
mov    -0x4(%rbp),%edi
mov    $0xffffffff,%esi
call   1050 <set_sem@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1050 <set_sem@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11b6 <protect_it+0x46>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<protect_it>:
push   %r15
push   %r14
push   %rbx
mov    $0x105,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
jne    11a5 <protect_it+0x35>
lea    0xe6e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0xffffffff,%esi
call   1050 <set_sem@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe46(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe47(%rip),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <set_sem@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    $0xffffffff,%esi
call   1050 <set_sem@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <set_sem@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    $0xffffffff,%esi
call   1050 <set_sem@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    $0x2,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    $0x2,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <set_sem@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    $0xffffffff,%esi
call   1050 <set_sem@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    $0x3,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    $0x3,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <set_sem@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd28(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"semget\00"
@errno = 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"
@EXIT_SUCCESS = global i32 0
define void @protect_it() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @semget(i32 261, i32 1, i32 0)
store i32 %3, i32* %1
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @errno
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
store i32 0, i32* %2
br label %10
10: ; preds = %26, %9
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 4
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = load i32, i32* %1
%15 = call i32 @set_sem(i32 %14, i32 -1)
%16 = load i32, i32* %2
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17)
%19 = call i32 @sleep(i32 3)
%20 = load i32, i32* %2
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
%23 = load i32, i32* %1
%24 = call i32 @set_sem(i32 %23, i32 1)
%25 = call i32 @sleep(i32 3)
br label %26
26: ; preds = %13
%27 = load i32, i32* %2
%28 = add nsw i32 %27, 1
store i32 %28, i32* %2
br label %10
29: ; preds = %10
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = load i32, i32* @EXIT_SUCCESS
%33 = call i32 @exit(i32 %32)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"semget\00"
@errno = 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"
@EXIT_SUCCESS = global i32 0
define void @protect_it() {
%1 = tail call i32 @semget(i32 261, i32 1, i32 0)
%2 = icmp eq i32 %1, -1
br i1 %2, label %40, label %3
3: ; preds = %0
%4 = tail call i32 @set_sem(i32 %1, i32 -1)
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %5)
%7 = tail call i32 @sleep(i32 3)
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %8)
%10 = tail call i32 @set_sem(i32 %1, i32 1)
%11 = tail call i32 @sleep(i32 3)
%12 = tail call i32 @set_sem(i32 %1, i32 -1)
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %13)
%15 = tail call i32 @sleep(i32 3)
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %16)
%18 = tail call i32 @set_sem(i32 %1, i32 1)
%19 = tail call i32 @sleep(i32 3)
%20 = tail call i32 @set_sem(i32 %1, i32 -1)
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 %21)
%23 = tail call i32 @sleep(i32 3)
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %24)
%26 = tail call i32 @set_sem(i32 %1, i32 1)
%27 = tail call i32 @sleep(i32 3)
%28 = tail call i32 @set_sem(i32 %1, i32 -1)
%29 = tail call i32 (...) @getpid()
%30 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 3, i32 %29)
%31 = tail call i32 @sleep(i32 3)
%32 = tail call i32 (...) @getpid()
%33 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 3, i32 %32)
%34 = tail call i32 @set_sem(i32 %1, i32 1)
%35 = tail call i32 @sleep(i32 3)
%36 = tail call i32 (...) @getpid()
%37 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @EXIT_SUCCESS
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %0
%41 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%42 = load i32, i32* @errno
%43 = tail call i32 @exit(i32 %42)
unreachable
}