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


x86 assembly (clang, -O0):
<get_semaphore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
call   10a0 <trace_flow@plt>
mov    $0x0,%al
call   1090 <get_semaphore_name@plt>
mov    %eax,%edi
mov    0x2e34(%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     11d3 <get_semaphore+0x53>
lea    0xe44(%rip),%rdi
call   1080 <shm_error@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    0x2de5(%rip),%rax
or     (%rax),%edx
mov    0x2dfc(%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>
lea    0xe02(%rip),%rdi
call   10a0 <trace_flow@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_semaphore>:
push   %rbx
lea    0xe78(%rip),%rdi
call   10a0 <trace_flow@plt>
xor    %eax,%eax
call   1090 <get_semaphore_name@plt>
mov    0x2e3d(%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     11c5 <get_semaphore+0x45>
lea    0xe52(%rip),%rdi
call   1080 <shm_error@plt>
mov    0x2e2c(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0a(%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
call   1060 <close@plt>
lea    0xe14(%rip),%rdi
pop    %rbx
jmp    10a0 <trace_flow@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"ENTERING: get_sem\00"
@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 = [17 x i8] c"LEAVING: get_sem\00"
define void @get_semaphore() {
%1 = alloca i32
%2 = call i32 @trace_flow(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%3 = call i32 (...) @get_semaphore_name()
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open_and_confirm(i32 %3, i32 %4, i32 0)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = call i64 @ftruncate(i32 %6, i32 4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = 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 %12, %13
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* %1
%17 = call i32 @mmap(i32* null, i32 4, i32 %14, i32 %15, i32 %16, i32 0)
%18 = call i32 @set_partner_status_ptr(i32 %17)
%19 = load i32, i32* %1
%20 = call i32 @close(i32 %19)
%21 = call i32 @trace_flow(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
ret void
}

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