#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 /*<<< orphan*/ 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 /*<<< orphan*/,
                     int /*<<< orphan*/);
int /*<<< orphan*/ set_partner_status_ptr(int /*<<< orphan*/);
int /*<<< orphan*/ shm_error(char *);
int /*<<< orphan*/ trace_flow(char *);
void get_semaphore() {
  if (0) {
    trace_flow("ENTERING: get_sem");
  }
  int fd = open_and_confirm(get_semaphore_name(), O_RDWR, 0);
  if (ftruncate(fd, sizeof(int))) {
    shm_error("truncate");
  }
  set_partner_status_ptr(
      mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
  close(fd);
  if (0) {
    trace_flow("LEAVING: get_sem");
  }
}


x86 assembly (clang, -O0):
<get_semaphore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <get_semaphore_name@plt>
mov    %eax,%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <open_and_confirm@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
je     11b7 <get_semaphore+0x47>
lea    0xe4e(%rip),%rdi
call   1080 <shm_error@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e01(%rip),%rax
or     (%rax),%edx
mov    0x2e18(%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   1070 <set_partner_status_ptr@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_semaphore>:
push   %rbx
xor    %eax,%eax
call   1090 <get_semaphore_name@plt>
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1030 <open_and_confirm@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
test   %rax,%rax
je     11a9 <get_semaphore+0x39>
lea    0xe5c(%rip),%rdi
call   1080 <shm_error@plt>
mov    0x2e48(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e26(%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   1070 <set_partner_status_ptr@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1060 <close@plt>

LLVM IR (-O0):
@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 @get_semaphore() {
%1 = alloca i32
%2 = call i32 (...) @get_semaphore_name()
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open_and_confirm(i32 %2, i32 %3, i32 0)
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = call i64 @ftruncate(i32 %5, i32 4)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = 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 %11, %12
%14 = load i32, i32* @MAP_SHARED
%15 = load i32, i32* %1
%16 = call i32 @mmap(i32* null, i32 4, i32 %13, i32 %14, i32 %15, i32 0)
%17 = call i32 @set_partner_status_ptr(i32 %16)
%18 = load i32, i32* %1
%19 = call i32 @close(i32 %18)
ret void
}

LLVM IR (-O2):
@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 @get_semaphore() {
%1 = tail call i32 (...) @get_semaphore_name()
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open_and_confirm(i32 %1, i32 %2, i32 0)
%4 = tail call i64 @ftruncate(i32 %3, i32 4)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = tail call i32 @shm_error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = tail call i32 @mmap(i32* null, i32 4, i32 %11, i32 %12, i32 %3, i32 0)
%14 = tail call i32 @set_partner_status_ptr(i32 %13)
%15 = tail call i32 @close(i32 %3)
ret void
}