#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 */
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ free(void *);
long long lseek(int, long long, int /*<<< orphan*/);
void *malloc(size_t);
size_t read(int, void *, size_t);
void *mmap(void *addr, size_t len, int prot, int flags, int fd,
           long long offset) {
  void *buf;
  ssize_t count;
  if (addr || fd == -1 || (prot & (1 << 1)))
    return ((void *)-1);
  buf = malloc(len);
  if (NULL == buf)
    return ((void *)-1);
  if (lseek(fd, offset, SEEK_SET) != offset)
    return ((void *)-1);
  count = read(fd, buf, len);
  if (count != len) {
    free(buf);
    return ((void *)-1);
  }
  return buf;
}


x86 assembly (clang, -O0):
<mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <mmap+0x42>
cmpl   $0xffffffff,-0x24(%rbp)
je     1182 <mmap+0x42>
mov    -0x1c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1192 <mmap+0x52>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    122d <mmap+0xed>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jne    11bb <mmap+0x7b>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    122d <mmap+0xed>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    -0x30(%rbp),%rax
je     11ea <mmap+0xaa>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    122d <mmap+0xed>
mov    -0x24(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1225 <mmap+0xe5>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    122d <mmap+0xed>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffffffffffff,%r14
test   $0x2,%dl
jne    11af <mmap+0x6f>
test   %rdi,%rdi
jne    11af <mmap+0x6f>
mov    %r8d,%ebp
cmp    $0xffffffff,%r8d
je     11af <mmap+0x6f>
mov    %r9,%rbx
mov    %rsi,%r15
mov    %rsi,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11af <mmap+0x6f>
mov    %rax,%r12
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <lseek@plt>
cmp    %rbx,%rax
jne    11af <mmap+0x6f>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1050 <read@plt>
cmp    %r15,%rax
jne    11a7 <mmap+0x67>
mov    %r12,%r14
jmp    11af <mmap+0x6f>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i8* @mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i64 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i64
store i8* %0, i8** %8
store i64 %1, i64* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i64 %5, i64* %13
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %25, label %18
18: ; preds = %6
%19 = load i32, i32* %12
%20 = icmp eq i32 %19, -1
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i32, i32* %10
%23 = and i32 %22, 2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21, %18, %6
store i8* inttoptr (i64 -1 to i8*), i8** %7
br label %53
26: ; preds = %21
%27 = load i64, i64* %9
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %14
%29 = load i8*, i8** %14
%30 = icmp eq i8* null, %29
br i1 %30, label %31, label %32
31: ; preds = %26
store i8* inttoptr (i64 -1 to i8*), i8** %7
br label %53
32: ; preds = %26
%33 = load i32, i32* %12
%34 = load i64, i64* %13
%35 = load i32, i32* @SEEK_SET
%36 = call i64 @lseek(i32 %33, i64 %34, i32 %35)
%37 = load i64, i64* %13
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %32
store i8* inttoptr (i64 -1 to i8*), i8** %7
br label %53
40: ; preds = %32
%41 = load i32, i32* %12
%42 = load i8*, i8** %14
%43 = load i64, i64* %9
%44 = call i64 @read(i32 %41, i8* %42, i64 %43)
store i64 %44, i64* %15
%45 = load i64, i64* %15
%46 = load i64, i64* %9
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i8*, i8** %14
%50 = call i32 @free(i8* %49)
store i8* inttoptr (i64 -1 to i8*), i8** %7
br label %53
51: ; preds = %40
%52 = load i8*, i8** %14
store i8* %52, i8** %7
br label %53
53: ; preds = %51, %48, %39, %31, %25
%54 = load i8*, i8** %7
ret i8* %54
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i8* @mmap(i8* readnone %0, i64 %1, i32 %2, i32 %3, i32 %4, i64 %5) {
%7 = icmp eq i8* %0, null
%8 = icmp ne i32 %4, -1
%9 = and i1 %7, %8
%10 = and i32 %2, 2
%11 = icmp eq i32 %10, 0
%12 = and i1 %11, %9
br i1 %12, label %13, label %25
13: ; preds = %6
%14 = tail call i8* @malloc(i64 %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %25, label %16
16: ; preds = %13
%17 = load i32, i32* @SEEK_SET
%18 = tail call i64 @lseek(i32 %4, i64 %5, i32 %17)
%19 = icmp eq i64 %18, %5
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = tail call i64 @read(i32 %4, i8* %14, i64 %1)
%22 = icmp eq i64 %21, %1
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @free(i8* %14)
br label %25
25: ; preds = %20, %16, %13, %6, %23
%26 = phi i8* [ inttoptr (i64 -1 to i8*), %23 ], [ inttoptr (i64 -1 to i8*), %6 ], [ inttoptr (i64 -1 to i8*), %13 ], [ inttoptr (i64 -1 to i8*), %16 ], [ %14, %20 ]
ret i8* %26
}