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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ ftruncate(int, int);
int /*<<< orphan*/ get_semaphore_name();
int /*<<< orphan*/ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                        int /*<<< orphan*/);
int open_and_confirm(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ set_partner_status(int /*<<< orphan*/);
int /*<<< orphan*/ set_partner_status_ptr(int /*<<< orphan*/);
int /*<<< orphan*/ shm_error(char *);
int /*<<< orphan*/ trace_flow(char *);
void setup_semaphore() {
  if (0) {
    trace_flow("ENTERING: setup_semaphore");
  }
  int fd = open_and_confirm(get_semaphore_name(), O_CREAT | O_RDWR, 0666);
  if (ftruncate(fd, sizeof(int))) {
    shm_error("truncate");
  }
  set_partner_status_ptr(
      mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
  set_partner_status(0);
  close(fd);
  if (0) {
    trace_flow("LEAVING: setup_semaphore");
  }
}


x86 assembly (clang, -O0):
<setup_semaphore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <get_semaphore_name@plt>
mov    %eax,%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1030 <open_and_confirm@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x4,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
je     11d3 <setup_semaphore+0x53>
lea    0xe32(%rip),%rdi
call   1090 <shm_error@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    0x2ddd(%rip),%rax
or     (%rax),%edx
mov    0x2df4(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %eax,%edi
call   1080 <set_partner_status_ptr@plt>
xor    %edi,%edi
call   1050 <set_partner_status@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_semaphore>:
push   %rbx
xor    %eax,%eax
call   10a0 <get_semaphore_name@plt>
mov    0x2e69(%rip),%rcx
mov    0x2e3a(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%edi
mov    $0x1b6,%edx
call   1030 <open_and_confirm@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x4,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
je     11c5 <setup_semaphore+0x45>
lea    0xe40(%rip),%rdi
call   1090 <shm_error@plt>
mov    0x2e24(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e02(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %eax,%edi
call   1080 <set_partner_status_ptr@plt>
xor    %edi,%edi
call   1050 <set_partner_status@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1070 <close@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [9 x i8] c"truncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define void @setup_semaphore() {
%1 = alloca i32
%2 = call i32 (...) @get_semaphore_name()
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %3, %4
%6 = call i32 @open_and_confirm(i32 %2, i32 %5, i32 438)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = call i64 @ftruncate(i32 %7, i32 4)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @shm_error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* %1
%18 = call i32 @mmap(i32* null, i32 4, i32 %15, i32 %16, i32 %17, i32 0)
%19 = call i32 @set_partner_status_ptr(i32 %18)
%20 = call i32 @set_partner_status(i32 0)
%21 = load i32, i32* %1
%22 = call i32 @close(i32 %21)
ret void
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [9 x i8] c"truncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define void @setup_semaphore() {
%1 = tail call i32 (...) @get_semaphore_name()
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_RDWR
%4 = or i32 %3, %2
%5 = tail call i32 @open_and_confirm(i32 %1, i32 %4, i32 438)
%6 = tail call i64 @ftruncate(i32 %5, i32 4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = tail call i32 @shm_error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i32 @mmap(i32* null, i32 4, i32 %13, i32 %14, i32 %5, i32 0)
%16 = tail call i32 @set_partner_status_ptr(i32 %15)
%17 = tail call i32 @set_partner_status(i32 0)
%18 = tail call i32 @close(i32 %5)
ret void
}