#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;
scalar_t__ errno;
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int sem_trywait(int /*<<< orphan*/ *);
void wait_until_everyone_is_finished(sem_t *sem) {
  while (sem_trywait(sem) != -1 && errno != EAGAIN) {
    sem_post(sem);
  }
}


x86 assembly (clang, -O0):
<wait_until_everyone_is_finished>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <sem_trywait@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x9(%rbp)
je     115f <wait_until_everyone_is_finished+0x3f>
mov    0x2e94(%rip),%rax
mov    (%rax),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
test   $0x1,%al
jne    116f <wait_until_everyone_is_finished+0x4f>
jmp    117d <wait_until_everyone_is_finished+0x5d>
mov    -0x8(%rbp),%rdi
call   1040 <sem_post@plt>
jmp    112c <wait_until_everyone_is_finished+0xc>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_until_everyone_is_finished>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <sem_trywait@plt>
cmp    $0xffffffff,%eax
je     115d <wait_until_everyone_is_finished+0x3d>
mov    0x2ea7(%rip),%r14
mov    0x2e90(%rip),%r15
mov    (%r15),%rax
cmp    %rax,(%r14)
je     115d <wait_until_everyone_is_finished+0x3d>
mov    %rbx,%rdi
call   1040 <sem_post@plt>
mov    %rbx,%rdi
call   1030 <sem_trywait@plt>
cmp    $0xffffffff,%eax
jne    1140 <wait_until_everyone_is_finished+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define void @wait_until_everyone_is_finished(i32* %0) {
%2 = alloca i32*
store i32* %0, i32** %2
br label %3
3: ; preds = %13, %1
%4 = load i32*, i32** %2
%5 = call i32 @sem_trywait(i32* %4)
%6 = icmp ne i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = load i64, i64* @errno
%9 = load i64, i64* @EAGAIN
%10 = icmp ne i64 %8, %9
br label %11
11: ; preds = %7, %3
%12 = phi i1 [ false, %3 ], [ %10, %7 ]
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i32*, i32** %2
%15 = call i32 @sem_post(i32* %14)
br label %3
16: ; preds = %11
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define void @wait_until_everyone_is_finished(i32* %0) {
%2 = tail call i32 @sem_trywait(i32* %0)
%3 = icmp ne i32 %2, -1
%4 = load i64, i64* @errno
%5 = load i64, i64* @EAGAIN
%6 = icmp ne i64 %4, %5
%7 = select i1 %3, i1 %6, i1 false
br i1 %7, label %8, label %16
8: ; preds = %1, %8
%9 = tail call i32 @sem_post(i32* %0)
%10 = tail call i32 @sem_trywait(i32* %0)
%11 = icmp ne i32 %10, -1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp ne i64 %12, %13
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %8, label %16
16: ; preds = %8, %1
ret void
}