#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 /*<<< orphan*/ PROT_READ;
scalar_t__ beaglelogic_get_buffersize(int, size_t *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
void *beaglelogic_mmap(int fd) {
  size_t sz;
  void *addr;
  if (beaglelogic_get_buffersize(fd, &sz))
    return MAP_FAILED;
  addr = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
  return addr;
}


x86 assembly (clang, -O0):
<beaglelogic_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <beaglelogic_get_buffersize@plt>
cmp    $0x0,%rax
je     1154 <beaglelogic_mmap+0x34>
mov    0x2e88(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1186 <beaglelogic_mmap+0x66>
mov    -0x18(%rbp),%rsi
mov    0x2e99(%rip),%rax
mov    (%rax),%edx
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<beaglelogic_mmap>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1040 <beaglelogic_get_buffersize@plt>
test   %rax,%rax
je     1142 <beaglelogic_mmap+0x22>
mov    0x2e93(%rip),%rax
mov    (%rax),%rax
jmp    1166 <beaglelogic_mmap+0x46>
mov    0x8(%rsp),%rsi
mov    0x2eaa(%rip),%rax
mov    (%rax),%edx
mov    0x2e91(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@MAP_FAILED = global i8* null
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @beaglelogic_mmap(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i64 @beaglelogic_get_buffersize(i32 %6, i64* %4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i8*, i8** @MAP_FAILED
store i8* %10, i8** %2
br label %18
11: ; preds = %1
%12 = load i64, i64* %4
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_SHARED
%15 = load i32, i32* %3
%16 = call i8* @mmap(i32* null, i64 %12, i32 %13, i32 %14, i32 %15, i32 0)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
store i8* %17, i8** %2
br label %18
18: ; preds = %11, %9
%19 = load i8*, i8** %2
ret i8* %19
}

LLVM IR (-O2):
@MAP_FAILED = global i8* null
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @beaglelogic_mmap(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = call i64 @beaglelogic_get_buffersize(i32 %0, i64* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = load i8*, i8** @MAP_FAILED
br label %13
8: ; preds = %1
%9 = load i64, i64* %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @MAP_SHARED
%12 = call i8* @mmap(i32* null, i64 %9, i32 %10, i32 %11, i32 %0, i32 0)
br label %13
13: ; preds = %8, %6
%14 = phi i8* [ %7, %6 ], [ %12, %8 ]
ret i8* %14
}