#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 */
struct file {
  int dummy;
};
typedef unsigned long len;

/* Variables and functions */
unsigned long do_mmap_pgoff(struct file *, unsigned long, unsigned long,
                            unsigned long, unsigned long, unsigned long);
__attribute__((always_)) unsigned long
do_mmap(struct file *file, unsigned long addr, unsigned long len,
        unsigned long prot, unsigned long flag, unsigned long offset) {
  unsigned long ret = -22;
  if ((offset + (((len) + (1UL << 12) - 1) & (~((1UL << 12) - 1)))) < offset)
    goto out;
  if (!(offset & ~(~((1UL << 12) - 1))))
    ret = do_mmap_pgoff(file, addr, len, prot, flag, offset >> 12);
out:
  return ret;
}


x86 assembly (clang, -O0):
<do_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movq   $0xffffffffffffffea,-0x38(%rbp)
ff
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    $0x1000,%rcx
sub    $0x1,%rcx
and    $0xfffffffffffff000,%rcx
add    %rcx,%rax
cmp    -0x30(%rbp),%rax
jae    1164 <do_mmap+0x54>
jmp    11a2 <do_mmap+0x92>
mov    -0x30(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
jne    119d <do_mmap+0x8d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
shr    $0xc,%r9
call   1030 <do_mmap_pgoff@plt>
mov    %rax,-0x38(%rbp)
jmp    11a2 <do_mmap+0x92>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mmap>:
lea    0xfff(%rdx),%r10
and    $0xfffffffffffff000,%r10
mov    %r9,%rax
not    %rax
cmp    %rax,%r10
ja     113f <do_mmap+0x2f>
mov    %r9d,%eax
and    $0xfff,%eax
test   %rax,%rax
jne    113f <do_mmap+0x2f>
shr    $0xc,%r9
jmp    1030 <do_mmap_pgoff@plt>
mov    $0xffffffffffffffea,%rax
ret

LLVM IR (-O0):
%struct.file = type { i32 }
define i64 @do_mmap(%struct.file* %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5) {
%7 = alloca %struct.file*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store %struct.file* %0, %struct.file** %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64 %4, i64* %11
store i64 %5, i64* %12
store i64 -22, i64* %13
%14 = load i64, i64* %12
%15 = load i64, i64* %9
%16 = add i64 %15, 4096
%17 = sub i64 %16, 1
%18 = and i64 %17, -4096
%19 = add i64 %14, %18
%20 = load i64, i64* %12
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %6
br label %37
23: ; preds = %6
%24 = load i64, i64* %12
%25 = and i64 %24, 4095
%26 = icmp ne i64 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %23
%28 = load %struct.file*, %struct.file** %7
%29 = load i64, i64* %8
%30 = load i64, i64* %9
%31 = load i64, i64* %10
%32 = load i64, i64* %11
%33 = load i64, i64* %12
%34 = lshr i64 %33, 12
%35 = call i64 @do_mmap_pgoff(%struct.file* %28, i64 %29, i64 %30, i64 %31, i64 %32, i64 %34)
store i64 %35, i64* %13
br label %36
36: ; preds = %27, %23
br label %37
37: ; preds = %36, %22
%38 = load i64, i64* %13
ret i64 %38
}

LLVM IR (-O2):
%struct.file = type { i32 }
define i64 @do_mmap(%struct.file* %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5) {
%7 = add i64 %2, 4095
%8 = and i64 %7, -4096
%9 = xor i64 %5, -1
%10 = icmp ule i64 %8, %9
%11 = and i64 %5, 4095
%12 = icmp eq i64 %11, 0
%13 = and i1 %10, %12
br i1 %13, label %14, label %17
14: ; preds = %6
%15 = lshr i64 %5, 12
%16 = tail call i64 @do_mmap_pgoff(%struct.file* %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %15)
br label %17
17: ; preds = %14, %6
%18 = phi i64 [ -22, %6 ], [ %16, %14 ]
ret i64 %18
}