#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_barrierattr_t;
typedef int /*<<< orphan*/ pthread_barrier_t;

/* Variables and functions */
int /*<<< orphan*/ *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ PTHREAD_PROCESS_SHARED;
unsigned int atoi(char *);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int ftruncate(int, int);
char *malloc(scalar_t__);
scalar_t__ mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int pthread_barrier_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                         unsigned int);
int /*<<< orphan*/ pthread_barrierattr_setpshared(int /*<<< orphan*/ *,
                                                  int /*<<< orphan*/);
int shm_open(char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char const *);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "Usage: %s barrier-name barrier-size\n", argv[0]);
    return 1;
  }
  const char *barrier_name_suffix = argv[1];
  unsigned int barrier_size = atoi(argv[2]);
  char *barrier_name =
      malloc(strlen(barrier_name_suffix) + strlen("java-ubench-agent") + 1);
  strcpy(barrier_name, "java-ubench-agent");
  strcat(barrier_name, barrier_name_suffix);
  int shared_mem_fd = shm_open(barrier_name, O_RDWR | O_CREAT, 0600);
  if (shared_mem_fd == -1) {
    die("shm_open");
  }
  int err = ftruncate(shared_mem_fd, sizeof(pthread_barrier_t));
  if (err == -1) {
    die("ftruncate");
  }
  pthread_barrier_t *barrier = (pthread_barrier_t *)mmap(
      0, sizeof(pthread_barrier_t), PROT_READ | PROT_WRITE, MAP_SHARED,
      shared_mem_fd, 0);
  if (barrier == MAP_FAILED) {
    die("mmap of shared memory");
  }
  pthread_barrierattr_t attr;
  pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
  err = pthread_barrier_init(barrier, &attr, barrier_size);
  if (err != 0) {
    die("pthread_barrier_init");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1208 <main+0x48>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe09(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1362 <main+0x1a2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x48(%rbp)
lea    0xded(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10d0 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    0x2d76(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10e0 <shm_open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12a7 <main+0xe7>
lea    0xd95(%rip),%rdi
call   10b0 <die@plt>
mov    -0x2c(%rbp),%edi
mov    $0x4,%esi
call   1080 <ftruncate@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    12cd <main+0x10d>
lea    0xd78(%rip),%rdi
call   10b0 <die@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
mov    0x2cdb(%rip),%rax
or     (%rax),%edx
mov    0x2cfa(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x4,%esi
mov    %r9d,%edi
call   1070 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2c9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1320 <main+0x160>
lea    0xd2f(%rip),%rdi
call   10b0 <die@plt>
mov    0x2c89(%rip),%rax
mov    (%rax),%esi
lea    -0x3c(%rbp),%rdi
call   1030 <pthread_barrierattr_setpshared@plt>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    -0x3c(%rbp),%rsi
call   1050 <pthread_barrier_init@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     135b <main+0x19b>
lea    0xd0a(%rip),%rdi
call   10b0 <die@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    12de <main+0x11e>
mov    0x8(%rsi),%rbp
mov    0x10(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x12(%rax),%rdi
call   10a0 <malloc@plt>
mov    %rax,%rbx
lea    0xe29(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10d0 <strcat@plt>
mov    0x2dba(%rip),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10e0 <shm_open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1241 <main+0x81>
lea    0xdfb(%rip),%rdi
call   10b0 <die@plt>
mov    %ebp,%edi
mov    $0x4,%esi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    125e <main+0x9e>
lea    0xde7(%rip),%rdi
call   10b0 <die@plt>
mov    0x2d8b(%rip),%rax
mov    0x2d4c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d69(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%rbp
mov    0x2d13(%rip),%rax
cmp    %rbp,(%rax)
jne    12a6 <main+0xe6>
lea    0xda9(%rip),%rdi
call   10b0 <die@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <pthread_barrierattr_setpshared@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <pthread_barrier_init@plt>
test   %eax,%eax
je     12da <main+0x11a>
lea    0xd8b(%rip),%rdi
call   10b0 <die@plt>
xor    %eax,%eax
jmp    12fb <main+0x13b>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd0f(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Usage: %s barrier-name barrier-size\0A\00"
@.str.1 = [18 x i8] c"java-ubench-agent\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [9 x i8] c"shm_open\00"
@.str.3 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.4 = [22 x i8] c"mmap of shared memory\00"
@PTHREAD_PROCESS_SHARED = global i32 0
@.str.5 = [21 x i8] c"pthread_barrier_init\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %80
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %6
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %7
%29 = load i8*, i8** %6
%30 = call i64 @strlen(i8* %29)
%31 = call i64 @strlen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%32 = add nsw i64 %30, %31
%33 = add nsw i64 %32, 1
%34 = call i8* @malloc(i64 %33)
store i8* %34, i8** %8
%35 = load i8*, i8** %8
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%37 = load i8*, i8** %8
%38 = load i8*, i8** %6
%39 = call i32 @strcat(i8* %37, i8* %38)
%40 = load i8*, i8** %8
%41 = load i32, i32* @O_RDWR
%42 = load i32, i32* @O_CREAT
%43 = or i32 %41, %42
%44 = call i32 @shm_open(i8* %40, i32 %43, i32 384)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %21
%48 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %21
%50 = load i32, i32* %9
%51 = call i32 @ftruncate(i32 %50, i32 4)
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %49
%57 = load i32, i32* @PROT_READ
%58 = load i32, i32* @PROT_WRITE
%59 = or i32 %57, %58
%60 = load i32, i32* @MAP_SHARED
%61 = load i32, i32* %9
%62 = call i64 @mmap(i32 0, i32 4, i32 %59, i32 %60, i32 %61, i32 0)
%63 = inttoptr i64 %62 to i32*
store i32* %63, i32** %11
%64 = load i32*, i32** %11
%65 = load i32*, i32** @MAP_FAILED
%66 = icmp eq i32* %64, %65
br i1 %66, label %67, label %69
67: ; preds = %56
%68 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %69
69: ; preds = %67, %56
%70 = load i32, i32* @PTHREAD_PROCESS_SHARED
%71 = call i32 @pthread_barrierattr_setpshared(i32* %12, i32 %70)
%72 = load i32*, i32** %11
%73 = load i32, i32* %7
%74 = call i32 @pthread_barrier_init(i32* %72, i32* %12, i32 %73)
store i32 %74, i32* %10
%75 = load i32, i32* %10
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %69
%78 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %79
79: ; preds = %77, %69
store i32 0, i32* %3
br label %80
80: ; preds = %79, %15
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Usage: %s barrier-name barrier-size\0A\00"
@.str.1 = [18 x i8] c"java-ubench-agent\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [9 x i8] c"shm_open\00"
@.str.3 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.4 = [22 x i8] c"mmap of shared memory\00"
@PTHREAD_PROCESS_SHARED = global i32 0
@.str.5 = [21 x i8] c"pthread_barrier_init\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 3
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %52
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%16 = add nsw i64 %15, 18
%17 = tail call i8* @malloc(i64 %16)
%18 = tail call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @strcat(i8* %17, i8* %11)
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = tail call i32 @shm_open(i8* %17, i32 %22, i32 384)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %9
%26 = tail call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %9
%28 = tail call i32 @ftruncate(i32 %23, i32 4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_SHARED
%37 = tail call i64 @mmap(i32 0, i32 4, i32 %35, i32 %36, i32 %23, i32 0)
%38 = inttoptr i64 %37 to i32*
%39 = load i32*, i32** @MAP_FAILED
%40 = icmp eq i32* %39, %38
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = tail call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %32
%44 = bitcast i32* %3 to i8*
%45 = load i32, i32* @PTHREAD_PROCESS_SHARED
%46 = call i32 @pthread_barrierattr_setpshared(i32* %3, i32 %45)
%47 = call i32 @pthread_barrier_init(i32* %38, i32* %3, i32 %14)
%48 = icmp eq i32 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %43
%50 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %43
br label %52
52: ; preds = %51, %5
%53 = phi i32 [ 1, %5 ], [ 0, %51 ]
ret i32 %53
}