#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 MAP_ANON;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void *calloc(int, size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
void *xmm_mnew(size_t size) {
  void *m = NULL;
  if (size > 0) {
    if (size < 4096)
      m = calloc(1, size);
    else {
      m = mmap(NULL, (((size / 4096) + ((size % 4096) != 0)) * 4096),
               PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
      if (m == NULL || m == (void *)-1)
        m = NULL;
      else
        memset(m, 0, (((size / 4096) + ((size % 4096) != 0)) * 4096));
    }
  }
  return m;
}


x86 assembly (clang, -O0):
<xmm_mnew>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jbe    123a <xmm_mnew+0x10a>
cmpq   $0x1000,-0x8(%rbp)
jae    1174 <xmm_mnew+0x44>
mov    -0x8(%rbp),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
jmp    1235 <xmm_mnew+0x105>
mov    -0x8(%rbp),%rsi
shr    $0xc,%rsi
mov    -0x8(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
cltq
add    %rax,%rsi
shl    $0xc,%rsi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
mov    0x2e2d(%rip),%rax
or     (%rax),%edx
mov    0x2e1c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0b(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f1 <xmm_mnew+0xc1>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    11fe <xmm_mnew+0xce>
movq   $0x0,-0x10(%rbp)
jmp    1230 <xmm_mnew+0x100>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
shr    $0xc,%rdx
mov    -0x8(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
cltq
add    %rax,%rdx
shl    $0xc,%rdx
xor    %esi,%esi
call   1040 <memset@plt>
jmp    1235 <xmm_mnew+0x105>
jmp    123a <xmm_mnew+0x10a>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmm_mnew>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1155 <xmm_mnew+0x25>
mov    %rdi,%rsi
cmp    $0xfff,%rdi
ja     115a <xmm_mnew+0x2a>
mov    $0x1,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <calloc@plt>
xor    %r14d,%r14d
jmp    11ca <xmm_mnew+0x9a>
xor    %ebx,%ebx
test   $0xfff,%esi
setne  %bl
shl    $0xc,%rbx
add    %rsi,%rbx
and    $0xfffffffffffff000,%rbx
mov    0x2e7e(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e44(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
add    $0x1,%rax
cmp    $0x2,%rax
jb     11ca <xmm_mnew+0x9a>
mov    %r15,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
mov    %r15,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @xmm_mnew(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
store i64 %0, i64* %2
store i8* null, i8** %3
%4 = load i64, i64* %2
%5 = icmp ugt i64 %4, 0
br i1 %5, label %6, label %49
6: ; preds = %1
%7 = load i64, i64* %2
%8 = icmp ult i64 %7, 4096
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i64, i64* %2
%11 = call i8* @calloc(i32 1, i64 %10)
store i8* %11, i8** %3
br label %48
12: ; preds = %6
%13 = load i64, i64* %2
%14 = udiv i64 %13, 4096
%15 = load i64, i64* %2
%16 = urem i64 %15, 4096
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i32
%19 = sext i32 %18 to i64
%20 = add i64 %14, %19
%21 = mul i64 %20, 4096
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* @MAP_ANON
%26 = load i32, i32* @MAP_PRIVATE
%27 = or i32 %25, %26
%28 = call i8* @mmap(i32* null, i64 %21, i32 %24, i32 %27, i32 -1, i32 0)
store i8* %28, i8** %3
%29 = load i8*, i8** %3
%30 = icmp eq i8* %29, null
br i1 %30, label %34, label %31
31: ; preds = %12
%32 = load i8*, i8** %3
%33 = icmp eq i8* %32, inttoptr (i64 -1 to i8*)
br i1 %33, label %34, label %35
34: ; preds = %31, %12
store i8* null, i8** %3
br label %47
35: ; preds = %31
%36 = load i8*, i8** %3
%37 = load i64, i64* %2
%38 = udiv i64 %37, 4096
%39 = load i64, i64* %2
%40 = urem i64 %39, 4096
%41 = icmp ne i64 %40, 0
%42 = zext i1 %41 to i32
%43 = sext i32 %42 to i64
%44 = add i64 %38, %43
%45 = mul i64 %44, 4096
%46 = call i32 @memset(i8* %36, i32 0, i64 %45)
br label %47
47: ; preds = %35, %34
br label %48
48: ; preds = %47, %9
br label %49
49: ; preds = %48, %1
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @xmm_mnew(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %23, label %3
3: ; preds = %1
%4 = icmp ult i64 %0, 4096
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i8* @calloc(i32 1, i64 %0)
br label %23
7: ; preds = %3
%8 = and i64 %0, 4095
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i64 0, i64 4096
%11 = add i64 %10, %0
%12 = and i64 %11, -4096
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_ANON
%17 = load i32, i32* @MAP_PRIVATE
%18 = or i32 %17, %16
%19 = tail call i8* @mmap(i32* null, i64 %12, i32 %15, i32 %18, i32 -1, i32 0)
%20 = ptrtoint i8* %19 to i64
switch i64 %20, label %21 [
i64 -1, label %23
i64 0, label %23
]
21: ; preds = %7
%22 = tail call i32 @memset(i8* %19, i32 0, i64 %12)
br label %23
23: ; preds = %7, %7, %5, %21, %1
%24 = phi i8* [ %6, %5 ], [ %19, %21 ], [ null, %1 ], [ null, %7 ], [ null, %7 ]
ret i8* %24
}