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

/* Variables and functions */
int EINVAL;
int PTHREAD_BARRIER_SERIAL_THREAD;
scalar_t__ pthread_barrier_destroy(int /*<<< orphan*/ *);
int pthread_barrier_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int pthread_barrier_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int do_test(void) {
  pthread_barrier_t b;
  int e;
  int cnt;
  e = pthread_barrier_init(&b, NULL, 0);
  if (e == 0) {
    puts("barrier_init with count 0 succeeded");
    return 1;
  }
  if (e != EINVAL) {
    puts("barrier_init with count 0 didn't return EINVAL");
    return 1;
  }
  if (pthread_barrier_init(&b, NULL, 1) != 0) {
    puts("real barrier_init failed");
    return 1;
  }
  for (cnt = 0; cnt < 10; ++cnt) {
    e = pthread_barrier_wait(&b);
    if (e != PTHREAD_BARRIER_SERIAL_THREAD) {
      puts("barrier_wait didn't return PTHREAD_BARRIER_SERIAL_THREAD");
      return 1;
    }
  }
  if (pthread_barrier_destroy(&b) != 0) {
    puts("barrier_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    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1030 <pthread_barrier_init@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    117c <do_test+0x3c>
lea    0xe95(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1265 <do_test+0x125>
mov    -0xc(%rbp),%eax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%eax
je     11a6 <do_test+0x66>
lea    0xe8f(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1265 <do_test+0x125>
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x1,%edx
call   1030 <pthread_barrier_init@plt>
cmp    $0x0,%eax
je     11d9 <do_test+0x99>
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1265 <do_test+0x125>
movl   $0x0,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jge    1233 <do_test+0xf3>
lea    -0x8(%rbp),%rdi
call   1050 <pthread_barrier_wait@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%eax
je     1220 <do_test+0xe0>
lea    0xe5d(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1265 <do_test+0x125>
jmp    1225 <do_test+0xe5>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11e0 <do_test+0xa0>
lea    -0x8(%rbp),%rdi
call   1060 <pthread_barrier_destroy@plt>
cmp    $0x0,%rax
je     125e <do_test+0x11e>
lea    0xe58(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1265 <do_test+0x125>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <pthread_barrier_init@plt>
test   %eax,%eax
je     117f <do_test+0x3f>
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%eax
jne    1188 <do_test+0x48>
lea    0x4(%rsp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <pthread_barrier_init@plt>
test   %eax,%eax
je     11a4 <do_test+0x64>
lea    0xed6(%rip),%rbx
jmp    118f <do_test+0x4f>
lea    0xe7a(%rip),%rbx
jmp    118f <do_test+0x4f>
lea    0xe95(%rip),%rbx
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
mov    0x2e33(%rip),%r14
lea    0xeb0(%rip),%rbx
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1050 <pthread_barrier_wait@plt>
cmp    (%r14),%eax
jne    118f <do_test+0x4f>
lea    0x4(%rsp),%rdi
call   1060 <pthread_barrier_destroy@plt>
test   %rax,%rax
je     1273 <do_test+0x133>
lea    0xe37(%rip),%rbx
jmp    118f <do_test+0x4f>
xor    %eax,%eax
jmp    119c <do_test+0x5c>

LLVM IR (-O0):
@.str = [36 x i8] c"barrier_init with count 0 succeeded\00"
@EINVAL = global i32 0
@.str.1 = [47 x i8] c"barrier_init with count 0 didn't return EINVAL\00"
@.str.2 = [25 x i8] c"real barrier_init failed\00"
@PTHREAD_BARRIER_SERIAL_THREAD = global i32 0
@.str.3 = [57 x i8] c"barrier_wait didn't return PTHREAD_BARRIER_SERIAL_THREAD\00"
@.str.4 = [23 x i8] c"barrier_destroy failed\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 @pthread_barrier_init(i32* %2, i32* null, i32 0)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %42
10: ; preds = %0
%11 = load i32, i32* %3
%12 = load i32, i32* @EINVAL
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @puts(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %42
16: ; preds = %10
%17 = call i32 @pthread_barrier_init(i32* %2, i32* null, i32 1)
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %42
21: ; preds = %16
store i32 0, i32* %4
br label %22
22: ; preds = %33, %21
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 10
br i1 %24, label %25, label %36
25: ; preds = %22
%26 = call i32 @pthread_barrier_wait(i32* %2)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @puts(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %42
32: ; preds = %25
br label %33
33: ; preds = %32
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %22
36: ; preds = %22
%37 = call i64 @pthread_barrier_destroy(i32* %2)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %42
41: ; preds = %36
store i32 0, i32* %1
br label %42
42: ; preds = %41, %39, %30, %19, %14, %8
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@.str = [36 x i8] c"barrier_init with count 0 succeeded\00"
@EINVAL = global i32 0
@.str.1 = [47 x i8] c"barrier_init with count 0 didn't return EINVAL\00"
@.str.2 = [25 x i8] c"real barrier_init failed\00"
@PTHREAD_BARRIER_SERIAL_THREAD = global i32 0
@.str.3 = [57 x i8] c"barrier_wait didn't return PTHREAD_BARRIER_SERIAL_THREAD\00"
@.str.4 = [23 x i8] c"barrier_destroy failed\00"
define i32 @do_test() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @pthread_barrier_init(i32* %1, i32* null, i32 0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %54, label %5
5: ; preds = %0
%6 = load i32, i32* @EINVAL
%7 = icmp eq i32 %3, %6
br i1 %7, label %8, label %54
8: ; preds = %5
%9 = call i32 @pthread_barrier_init(i32* %1, i32* null, i32 1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %54
11: ; preds = %8
%12 = call i32 @pthread_barrier_wait(i32* %1)
%13 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %54
15: ; preds = %11
%16 = call i32 @pthread_barrier_wait(i32* %1)
%17 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %54
19: ; preds = %15
%20 = call i32 @pthread_barrier_wait(i32* %1)
%21 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %54
23: ; preds = %19
%24 = call i32 @pthread_barrier_wait(i32* %1)
%25 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %54
27: ; preds = %23
%28 = call i32 @pthread_barrier_wait(i32* %1)
%29 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %54
31: ; preds = %27
%32 = call i32 @pthread_barrier_wait(i32* %1)
%33 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %54
35: ; preds = %31
%36 = call i32 @pthread_barrier_wait(i32* %1)
%37 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %54
39: ; preds = %35
%40 = call i32 @pthread_barrier_wait(i32* %1)
%41 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %54
43: ; preds = %39
%44 = call i32 @pthread_barrier_wait(i32* %1)
%45 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %54
47: ; preds = %43
%48 = call i32 @pthread_barrier_wait(i32* %1)
%49 = load i32, i32* @PTHREAD_BARRIER_SERIAL_THREAD
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = call i64 @pthread_barrier_destroy(i32* %1)
%53 = icmp eq i64 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %51, %11, %15, %19, %23, %27, %31, %35, %39, %43, %47, %8, %5, %0
%55 = phi i8* [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), %5 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %47 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %43 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %39 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %35 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %31 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %27 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %23 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %19 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %15 ], [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), %11 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), %51 ]
%56 = call i32 @puts(i8* dereferenceable(1) %55)
br label %57
57: ; preds = %54, %51
%58 = phi i32 [ 0, %51 ], [ 1, %54 ]
ret i32 %58
}