#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_mutexattr_t;
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ PTHREAD_MUTEX_ERRORCHECK;
int /*<<< orphan*/ PTHREAD_PROCESS_SHARED;
int S_IRWXG;
int S_IRWXU;
int ftruncate(int, int);
int /*<<< orphan*/ handle_error(char *);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_init(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_setpshared(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/);
scalar_t__ pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int shm_open(char *, int, int);
pthread_mutex_t *setup_mutex(void) {
  pthread_mutex_t *mutex;
  int shared_mem;
  int mode = S_IRWXU | S_IRWXG;
  pthread_mutexattr_t mutex_attr;
  shared_mem = shm_open("/ainod_mutex_lock", O_CREAT | O_RDWR | O_TRUNC, mode);
  if (shared_mem < 0) {
    handle_error("Failed to create shared memory.");
  }
  if (ftruncate(shared_mem, sizeof(pthread_mutex_t)) == -1) {
    handle_error("Failed to set correct size of shared memory space.");
  }
  mutex = (pthread_mutex_t *)mmap(NULL, sizeof(pthread_mutex_t),
                                  PROT_READ | PROT_WRITE, MAP_SHARED,
                                  shared_mem, 0);
  if (mutex == MAP_FAILED) {
    handle_error("Failed to map the mutex into shared memory.");
  }
  if (pthread_mutexattr_init(&mutex_attr) != 0) {
    handle_error("Failed to create mutex attribute");
  }
  if (pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED) != 0) {
    handle_error("Failed to share mutex.");
  }
  if (pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK) != 0) {
    handle_error("Failed to set mutex type.");
  }
  if (pthread_mutex_init(mutex, &mutex_attr) != 0) {
    handle_error("Failed to initialise mutex.");
  }
  if (pthread_mutexattr_destroy(&mutex_attr) != 0) {
    handle_error("Failed to delete mutex attribute.");
  }
  return mutex;
}


x86 assembly (clang, -O0):
<setup_mutex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    0x2e10(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
mov    -0x10(%rbp),%edx
lea    0xe2e(%rip),%rdi
call   1070 <shm_open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11f0 <setup_mutex+0x60>
lea    0xe27(%rip),%rdi
call   10b0 <handle_error@plt>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    1212 <setup_mutex+0x82>
lea    0xe25(%rip),%rdi
call   10b0 <handle_error@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    0x2d86(%rip),%rax
or     (%rax),%edx
mov    0x2dad(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%rax
jne    1266 <setup_mutex+0xd6>
lea    0xe04(%rip),%rdi
call   10b0 <handle_error@plt>
lea    -0x14(%rbp),%rdi
call   1080 <pthread_mutexattr_init@plt>
cmp    $0x0,%rax
je     1285 <setup_mutex+0xf5>
lea    0xe11(%rip),%rdi
call   10b0 <handle_error@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   1060 <pthread_mutexattr_setpshared@plt>
cmp    $0x0,%rax
je     12ad <setup_mutex+0x11d>
lea    0xe0a(%rip),%rdi
call   10b0 <handle_error@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   1040 <pthread_mutexattr_settype@plt>
cmp    $0x0,%rax
je     12d5 <setup_mutex+0x145>
lea    0xdf9(%rip),%rdi
call   10b0 <handle_error@plt>
mov    -0x8(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1090 <pthread_mutex_init@plt>
cmp    $0x0,%rax
je     12f8 <setup_mutex+0x168>
lea    0xdf0(%rip),%rdi
call   10b0 <handle_error@plt>
lea    -0x14(%rbp),%rdi
call   10a0 <pthread_mutexattr_destroy@plt>
cmp    $0x0,%rax
je     1317 <setup_mutex+0x187>
lea    0xded(%rip),%rdi
call   10b0 <handle_error@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_mutex>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e2c(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4a(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
lea    0xe37(%rip),%rdi
call   1070 <shm_open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e0 <setup_mutex+0x50>
lea    0xe37(%rip),%rdi
call   10b0 <handle_error@plt>
mov    %ebx,%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11fd <setup_mutex+0x6d>
lea    0xe3a(%rip),%rdi
call   10b0 <handle_error@plt>
mov    0x2de4(%rip),%rax
mov    0x2d9d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc2(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2d64(%rip),%rax
cmp    %rbx,(%rax)
jne    1245 <setup_mutex+0xb5>
lea    0xe25(%rip),%rdi
call   10b0 <handle_error@plt>
lea    0xc(%rsp),%rdi
call   1080 <pthread_mutexattr_init@plt>
test   %rax,%rax
je     1260 <setup_mutex+0xd0>
lea    0xe36(%rip),%rdi
call   10b0 <handle_error@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdi
call   1060 <pthread_mutexattr_setpshared@plt>
test   %rax,%rax
je     1284 <setup_mutex+0xf4>
lea    0xe33(%rip),%rdi
call   10b0 <handle_error@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdi
call   1040 <pthread_mutexattr_settype@plt>
test   %rax,%rax
je     12a8 <setup_mutex+0x118>
lea    0xe26(%rip),%rdi
call   10b0 <handle_error@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <pthread_mutex_init@plt>
test   %rax,%rax
je     12c6 <setup_mutex+0x136>
lea    0xe22(%rip),%rdi
call   10b0 <handle_error@plt>
lea    0xc(%rsp),%rdi
call   10a0 <pthread_mutexattr_destroy@plt>
test   %rax,%rax
je     12e1 <setup_mutex+0x151>
lea    0xe23(%rip),%rdi
call   10b0 <handle_error@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@.str = [18 x i8] c"/ainod_mutex_lock\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [32 x i8] c"Failed to create shared memory.\00"
@.str.2 = [51 x i8] c"Failed to set correct size of shared memory space.\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [44 x i8] c"Failed to map the mutex into shared memory.\00"
@.str.4 = [33 x i8] c"Failed to create mutex attribute\00"
@PTHREAD_PROCESS_SHARED = global i32 0
@.str.5 = [23 x i8] c"Failed to share mutex.\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.6 = [26 x i8] c"Failed to set mutex type.\00"
@.str.7 = [28 x i8] c"Failed to initialise mutex.\00"
@.str.8 = [34 x i8] c"Failed to delete mutex attribute.\00"
define i32* @setup_mutex() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @S_IRWXU
%6 = load i32, i32* @S_IRWXG
%7 = or i32 %5, %6
store i32 %7, i32* %3
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* %3
%14 = call i32 @shm_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @handle_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = load i32, i32* %2
%21 = call i32 @ftruncate(i32 %20, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @handle_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %26, %27
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %2
%31 = call i64 @mmap(i32* null, i32 4, i32 %28, i32 %29, i32 %30, i32 0)
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %1
%33 = load i32*, i32** %1
%34 = load i32*, i32** @MAP_FAILED
%35 = icmp eq i32* %33, %34
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @handle_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %25
%39 = call i64 @pthread_mutexattr_init(i32* %4)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @handle_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = load i32, i32* @PTHREAD_PROCESS_SHARED
%45 = call i64 @pthread_mutexattr_setpshared(i32* %4, i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @handle_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%51 = call i64 @pthread_mutexattr_settype(i32* %4, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @handle_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %49
%56 = load i32*, i32** %1
%57 = call i64 @pthread_mutex_init(i32* %56, i32* %4)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @handle_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
br label %61
61: ; preds = %59, %55
%62 = call i64 @pthread_mutexattr_destroy(i32* %4)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @handle_error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
%67 = load i32*, i32** %1
ret i32* %67
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@.str = [18 x i8] c"/ainod_mutex_lock\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [32 x i8] c"Failed to create shared memory.\00"
@.str.2 = [51 x i8] c"Failed to set correct size of shared memory space.\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [44 x i8] c"Failed to map the mutex into shared memory.\00"
@.str.4 = [33 x i8] c"Failed to create mutex attribute\00"
@PTHREAD_PROCESS_SHARED = global i32 0
@.str.5 = [23 x i8] c"Failed to share mutex.\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.6 = [26 x i8] c"Failed to set mutex type.\00"
@.str.7 = [28 x i8] c"Failed to initialise mutex.\00"
@.str.8 = [34 x i8] c"Failed to delete mutex attribute.\00"
define i32* @setup_mutex() {
%1 = alloca i32
%2 = load i32, i32* @S_IRWXU
%3 = load i32, i32* @S_IRWXG
%4 = or i32 %3, %2
%5 = bitcast i32* %1 to i8*
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = tail call i32 @shm_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %4)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @handle_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = tail call i32 @ftruncate(i32 %11, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @handle_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %22, %21
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i64 @mmap(i32* null, i32 4, i32 %23, i32 %24, i32 %11, i32 0)
%26 = inttoptr i64 %25 to i32*
%27 = load i32*, i32** @MAP_FAILED
%28 = icmp eq i32* %27, %26
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = tail call i32 @handle_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %20
%32 = call i64 @pthread_mutexattr_init(i32* %1)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @handle_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = load i32, i32* @PTHREAD_PROCESS_SHARED
%38 = call i64 @pthread_mutexattr_setpshared(i32* %1, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %36
%41 = call i32 @handle_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%44 = call i64 @pthread_mutexattr_settype(i32* %1, i32 %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = call i32 @handle_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
%49 = call i64 @pthread_mutex_init(i32* %26, i32* %1)
%50 = icmp eq i64 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %48
%52 = call i32 @handle_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
%54 = call i64 @pthread_mutexattr_destroy(i32* %1)
%55 = icmp eq i64 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = call i32 @handle_error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
ret i32* %26
}