#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int fileno(void *);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
char *shmem_attach(void *fh, size_t length) {
  int fd;
  void *mem;
  fd = fileno(fh);
  if ((mem = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
      MAP_FAILED) {
    perror("mmap");
    return (NULL);
  }
  return ((char *)mem);
}


x86 assembly (clang, -O0):
<shmem_attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2ea1(%rip),%rax
mov    (%rax),%edx
mov    0x2e70(%rip),%rax
or     (%rax),%edx
mov    0x2e7f(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    11a8 <shmem_attach+0x78>
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b0 <shmem_attach+0x80>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shmem_attach>:
push   %rbx
mov    %rsi,%rbx
call   1040 <fileno@plt>
mov    0x2eb8(%rip),%rcx
mov    0x2e89(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e96(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
je     1172 <shmem_attach+0x42>
pop    %rbx
ret
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
define i8* @shmem_attach(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = call i32 @fileno(i8* %8)
store i32 %9, i32* %6
%10 = load i64, i64* %5
%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* %6
%16 = call i8* @mmap(i32* null, i64 %10, i32 %13, i32 %14, i32 %15, i32 0)
store i8* %16, i8** %7
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %23
21: ; preds = %2
%22 = load i8*, i8** %7
store i8* %22, i8** %3
br label %23
23: ; preds = %21, %19
%24 = load i8*, i8** %3
ret i8* %24
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
define i8* @shmem_attach(i8* nocapture %0, i64 %1) {
%3 = tail call i32 @fileno(i8* %0)
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_SHARED
%8 = tail call i8* @mmap(i32* null, i64 %1, i32 %6, i32 %7, i32 %3, i32 0)
%9 = load i8*, i8** @MAP_FAILED
%10 = icmp eq i8* %8, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %2, %11
%14 = phi i8* [ null, %11 ], [ %8, %2 ]
ret i8* %14
}