#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 (1) {
    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 (1) {
    trace_flow("LEAVING: setup_semaphore");
  }
}


x86 assembly (clang, -O0):
<setup_semaphore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe61(%rip),%rdi
call   10b0 <trace_flow@plt>
mov    $0x0,%al
call   10a0 <get_semaphore_name@plt>
mov    %eax,%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%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     11ef <setup_semaphore+0x5f>
lea    0xe30(%rip),%rdi
call   1090 <shm_error@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    0x2dc1(%rip),%rax
or     (%rax),%edx
mov    0x2dd8(%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>
lea    0xde7(%rip),%rdi
call   10b0 <trace_flow@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_semaphore>:
push   %rbx
lea    0xe68(%rip),%rdi
call   10b0 <trace_flow@plt>
xor    %eax,%eax
call   10a0 <get_semaphore_name@plt>
mov    0x2e4d(%rip),%rcx
mov    0x2e1e(%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     11e1 <setup_semaphore+0x51>
lea    0xe3e(%rip),%rdi
call   1090 <shm_error@plt>
mov    0x2e08(%rip),%rax
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de6(%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
call   1070 <close@plt>
lea    0xdf9(%rip),%rdi
pop    %rbx
jmp    10b0 <trace_flow@plt>

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

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