#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 */
scalar_t__ EAGAIN;
int TEMP_FAILURE_RETRY(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ puts(char *);
int sem_destroy(int /*<<< orphan*/ *);
int sem_init(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_trywait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int do_test(void) {
  sem_t s;
  if (sem_init(&s, 0, 1) == -1) {
    puts("init failed");
    return 1;
  }
  if (TEMP_FAILURE_RETRY(sem_wait(&s)) == -1) {
    puts("1st wait failed");
    return 1;
  }
  if (sem_post(&s) == -1) {
    puts("1st post failed");
    return 1;
  }
  if (TEMP_FAILURE_RETRY(sem_trywait(&s)) == -1) {
    puts("1st trywait failed");
    return 1;
  }
  errno = 0;
  if (TEMP_FAILURE_RETRY(sem_trywait(&s)) != -1) {
    puts("2nd trywait succeeded");
    return 1;
  } else if (errno != EAGAIN) {
    puts("2nd trywait did not set errno to EAGAIN");
    return 1;
  }
  if (sem_post(&s) == -1) {
    puts("2nd post failed");
    return 1;
  }
  if (TEMP_FAILURE_RETRY(sem_wait(&s)) == -1) {
    puts("2nd wait failed");
    return 1;
  }
  if (sem_destroy(&s) == -1) {
    puts("destroy failed");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11a9 <do_test+0x39>
lea    0xe68(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
lea    -0x8(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,%edi
call   1080 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
jne    11da <do_test+0x6a>
lea    0xe43(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
lea    -0x8(%rbp),%rdi
call   1060 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    1204 <do_test+0x94>
lea    0xe29(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
lea    -0x8(%rbp),%rdi
call   1050 <sem_trywait@plt>
mov    %eax,%edi
call   1080 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
jne    1235 <do_test+0xc5>
lea    0xe08(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
mov    0x2da4(%rip),%rax
movq   $0x0,(%rax)
lea    -0x8(%rbp),%rdi
call   1050 <sem_trywait@plt>
mov    %eax,%edi
call   1080 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1274 <do_test+0x104>
lea    0xddc(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
mov    0x2d65(%rip),%rax
mov    (%rax),%rax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
je     12a6 <do_test+0x136>
lea    0xdc0(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
jmp    12ab <do_test+0x13b>
lea    -0x8(%rbp),%rdi
call   1060 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    12d5 <do_test+0x165>
lea    0xdb9(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
lea    -0x8(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,%edi
call   1080 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
jne    1306 <do_test+0x196>
lea    0xd98(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
lea    -0x8(%rbp),%rdi
call   1090 <sem_destroy@plt>
cmp    $0xffffffff,%eax
jne    1330 <do_test+0x1c0>
lea    0xd7e(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <do_test+0x1c7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
sub    $0x10,%rsp
lea    0xc(%rsp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
je     11f6 <do_test+0x86>
lea    0xc(%rsp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,%edi
call   1080 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1202 <do_test+0x92>
lea    0xc(%rsp),%rdi
call   1060 <sem_post@plt>
cmp    $0xffffffff,%eax
je     120b <do_test+0x9b>
lea    0xc(%rsp),%rdi
call   1050 <sem_trywait@plt>
mov    %eax,%edi
call   1080 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1214 <do_test+0xa4>
mov    0x2e13(%rip),%rbx
movq   $0x0,(%rbx)
lea    0xc(%rsp),%rdi
call   1050 <sem_trywait@plt>
mov    %eax,%edi
call   1080 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     121d <do_test+0xad>
lea    0xe4e(%rip),%rdi
jmp    1286 <do_test+0x116>
lea    0xe03(%rip),%rdi
jmp    1286 <do_test+0x116>
lea    0xe03(%rip),%rdi
jmp    1286 <do_test+0x116>
lea    0xe0a(%rip),%rdi
jmp    1286 <do_test+0x116>
lea    0xe11(%rip),%rdi
jmp    1286 <do_test+0x116>
mov    (%rbx),%rax
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%rax
jne    126d <do_test+0xfd>
lea    0xc(%rsp),%rdi
call   1060 <sem_post@plt>
cmp    $0xffffffff,%eax
je     1276 <do_test+0x106>
lea    0xc(%rsp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,%edi
call   1080 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     127f <do_test+0x10f>
lea    0xc(%rsp),%rdi
call   1090 <sem_destroy@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1290 <do_test+0x120>
lea    0xe32(%rip),%rdi
jmp    1286 <do_test+0x116>
lea    0xde1(%rip),%rdi
jmp    1286 <do_test+0x116>
lea    0xe00(%rip),%rdi
jmp    1286 <do_test+0x116>
lea    0xe07(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"init failed\00"
@.str.1 = [16 x i8] c"1st wait failed\00"
@.str.2 = [16 x i8] c"1st post failed\00"
@.str.3 = [19 x i8] c"1st trywait failed\00"
@errno = global i64 0
@.str.4 = [22 x i8] c"2nd trywait succeeded\00"
@EAGAIN = global i64 0
@.str.5 = [40 x i8] c"2nd trywait did not set errno to EAGAIN\00"
@.str.6 = [16 x i8] c"2nd post failed\00"
@.str.7 = [16 x i8] c"2nd wait failed\00"
@.str.8 = [15 x i8] c"destroy failed\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @sem_init(i32* %2, i32 0, i32 1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %54
7: ; preds = %0
%8 = call i32 @sem_wait(i32* %2)
%9 = call i32 @TEMP_FAILURE_RETRY(i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %54
13: ; preds = %7
%14 = call i32 @sem_post(i32* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %54
18: ; preds = %13
%19 = call i32 @sem_trywait(i32* %2)
%20 = call i32 @TEMP_FAILURE_RETRY(i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %54
24: ; preds = %18
store i64 0, i64* @errno
%25 = call i32 @sem_trywait(i32* %2)
%26 = call i32 @TEMP_FAILURE_RETRY(i32 %25)
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %54
30: ; preds = %24
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %54
36: ; preds = %30
br label %37
37: ; preds = %36
%38 = call i32 @sem_post(i32* %2)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %54
42: ; preds = %37
%43 = call i32 @sem_wait(i32* %2)
%44 = call i32 @TEMP_FAILURE_RETRY(i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %54
48: ; preds = %42
%49 = call i32 @sem_destroy(i32* %2)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %54
53: ; preds = %48
store i32 0, i32* %1
br label %54
54: ; preds = %53, %51, %46, %40, %34, %28, %22, %16, %11, %5
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str = [12 x i8] c"init failed\00"
@.str.1 = [16 x i8] c"1st wait failed\00"
@.str.2 = [16 x i8] c"1st post failed\00"
@.str.3 = [19 x i8] c"1st trywait failed\00"
@errno = global i64 0
@.str.4 = [22 x i8] c"2nd trywait succeeded\00"
@EAGAIN = global i64 0
@.str.5 = [40 x i8] c"2nd trywait did not set errno to EAGAIN\00"
@.str.6 = [16 x i8] c"2nd post failed\00"
@.str.7 = [16 x i8] c"2nd wait failed\00"
@.str.8 = [15 x i8] c"destroy failed\00"
define i32 @do_test() {
%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 %34, label %5
5: ; preds = %0
%6 = call i32 @sem_wait(i32* %1)
%7 = call i32 @TEMP_FAILURE_RETRY(i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %34, label %9
9: ; preds = %5
%10 = call i32 @sem_post(i32* %1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %34, label %12
12: ; preds = %9
%13 = call i32 @sem_trywait(i32* %1)
%14 = call i32 @TEMP_FAILURE_RETRY(i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %34, label %16
16: ; preds = %12
store i64 0, i64* @errno
%17 = call i32 @sem_trywait(i32* %1)
%18 = call i32 @TEMP_FAILURE_RETRY(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %34
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %34
24: ; preds = %20
%25 = call i32 @sem_post(i32* %1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %34, label %27
27: ; preds = %24
%28 = call i32 @sem_wait(i32* %1)
%29 = call i32 @TEMP_FAILURE_RETRY(i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = call i32 @sem_destroy(i32* %1)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %31, %27, %24, %20, %16, %12, %9, %5, %0
%35 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %5 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), %12 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), %16 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), %20 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), %24 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), %27 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), %31 ]
%36 = call i32 @puts(i8* dereferenceable(1) %35)
br label %37
37: ; preds = %34, %31
%38 = phi i32 [ 0, %31 ], [ 1, %34 ]
ret i32 %38
}