#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*/ errno;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sem_destroy(int /*<<< orphan*/ *);
int sem_init(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
scalar_t__ sem_post(int /*<<< orphan*/ *);
scalar_t__ sem_wait(int /*<<< orphan*/ *);
char *strerror(int /*<<< orphan*/);
int main(void) {
  sem_t sem;
  if (sem_init(&sem, 0, 1) == -1)
    printf("%s\n", strerror(errno));
  if (sem_wait(&sem) != 0)
    printf("%s\n", strerror(errno));
  printf("*** Critical Section *** \n");
  if (sem_post(&sem) != 0)
    printf("%s\n", strerror(errno));
  printf("*** Non-Critical Section *** \n");
  if (sem_destroy(&sem) != 0)
    printf("%s\n", strerror(errno));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1060 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11a7 <main+0x47>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0x0,%rax
je     11d9 <main+0x79>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1050 <sem_post@plt>
cmp    $0x0,%rax
je     1219 <main+0xb9>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1080 <sem_destroy@plt>
cmp    $0x0,%rax
je     1259 <main+0xf9>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0x4(%rsp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1060 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    118d <main+0x2d>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
je     11b2 <main+0x52>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
lea    0xe47(%rip),%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%rdi
call   1050 <sem_post@plt>
test   %rax,%rax
je     11e3 <main+0x83>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
lea    0xe30(%rip),%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%rdi
call   1080 <sem_destroy@plt>
test   %rax,%rax
je     1214 <main+0xb4>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
@.str.1 = [27 x i8] c"*** Critical Section *** \0A\00"
@.str.2 = [31 x i8] c"*** Non-Critical Section *** \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @sem_init(i32* %2, i32 0, i32 1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = call i8* @strerror(i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %9
9: ; preds = %5, %0
%10 = call i64 @sem_wait(i32* %2)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %12, %9
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%18 = call i64 @sem_post(i32* %2)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %20, %16
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%26 = call i64 @sem_destroy(i32* %2)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %28, %24
ret i32 0
}

LLVM IR (-O2):
@errno = global i32 0
@str = [26 x i8] c"*** Critical Section *** \00"
@str.3 = [30 x i8] c"*** Non-Critical Section *** \00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @sem_init(i32* %1, i32 0, i32 1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = call i8* @strerror(i32 %6)
%8 = call i32 @puts(i8* dereferenceable(1) %7)
br label %9
9: ; preds = %5, %0
%10 = call i64 @sem_wait(i32* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @puts(i8* dereferenceable(1) %14)
br label %16
16: ; preds = %12, %9
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%18 = call i64 @sem_post(i32* %1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @puts(i8* dereferenceable(1) %22)
br label %24
24: ; preds = %20, %16
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.3, i64 0, i64 0))
%26 = call i64 @sem_destroy(i32* %1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @puts(i8* dereferenceable(1) %30)
br label %32
32: ; preds = %28, %24
ret i32 0
}