#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*/ free(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
void *xmm_resize(void *old, size_t old_size, size_t new_size) {
  void *m = NULL;
  if (new_size > 0 && new_size > old_size) {
    if (new_size < 4096) {
      m = calloc(1, new_size);
    } else {
      m = mmap(NULL, (((new_size / 4096) + ((new_size % 4096) != 0)) * 4096),
               PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
      if (m == (void *)-1)
        m = NULL;
    }
    if (old) {
      if (old_size > 0) {
        if (m)
          memcpy(m, old, old_size);
        if (old_size < 4096)
          free(old);
        else
          munmap(old, (((old_size / 4096) + ((old_size % 4096) != 0)) * 4096));
      }
    }
  }
  return m;
}


x86 assembly (clang, -O0):
<xmm_resize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    12b6 <xmm_resize+0x166>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    12b6 <xmm_resize+0x166>
cmpq   $0x1000,-0x18(%rbp)
jae    11aa <xmm_resize+0x5a>
mov    -0x18(%rbp),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
jmp    1229 <xmm_resize+0xd9>
mov    -0x18(%rbp),%rsi
shr    $0xc,%rsi
mov    -0x18(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
cltq
add    %rax,%rsi
shl    $0xc,%rsi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    0x2df7(%rip),%rax
or     (%rax),%edx
mov    0x2de6(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd5(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    1224 <xmm_resize+0xd4>
movq   $0x0,-0x20(%rbp)
jmp    1229 <xmm_resize+0xd9>
cmpq   $0x0,-0x8(%rbp)
je     12b1 <xmm_resize+0x161>
cmpq   $0x0,-0x10(%rbp)
jbe    12ac <xmm_resize+0x15c>
cmpq   $0x0,-0x20(%rbp)
je     125b <xmm_resize+0x10b>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1060 <memcpy@plt>
cmpq   $0x1000,-0x10(%rbp)
jae    1277 <xmm_resize+0x127>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
jmp    12a7 <xmm_resize+0x157>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
shr    $0xc,%rsi
mov    -0x10(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
cltq
add    %rax,%rsi
shl    $0xc,%rsi
call   1070 <munmap@plt>
jmp    12ac <xmm_resize+0x15c>
jmp    12b1 <xmm_resize+0x161>
jmp    12b6 <xmm_resize+0x166>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmm_resize>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    %rsi,%rdx
jbe    1186 <xmm_resize+0x36>
mov    %rsi,%r15
mov    %rdi,%r14
cmp    $0xfff,%rdx
ja     118d <xmm_resize+0x3d>
mov    $0x1,%edi
mov    %rdx,%rsi
call   1050 <calloc@plt>
mov    %rax,%rbx
test   %r14,%r14
jne    11ee <xmm_resize+0x9e>
jmp    123c <xmm_resize+0xec>
xor    %ebx,%ebx
jmp    123c <xmm_resize+0xec>
xor    %esi,%esi
test   $0xfff,%edx
setne  %sil
shl    $0xc,%rsi
add    %rdx,%rsi
and    $0xfffffffffffff000,%rsi
mov    0x2e4a(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e10(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %r12d,%r12d
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
cmove  %r12,%rbx
test   %r14,%r14
je     123c <xmm_resize+0xec>
test   %r15,%r15
je     123c <xmm_resize+0xec>
test   %rbx,%rbx
je     1206 <xmm_resize+0xb6>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <memcpy@plt>
cmp    $0xfff,%r15
ja     1219 <xmm_resize+0xc9>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    123c <xmm_resize+0xec>
xor    %esi,%esi
test   $0xfff,%r15d
setne  %sil
shl    $0xc,%rsi
add    %r15,%rsi
and    $0xfffffffffffff000,%rsi
mov    %r14,%rdi
call   1070 <munmap@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
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_resize(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
store i8* null, i8** %7
%8 = load i64, i64* %6
%9 = icmp ugt i64 %8, 0
br i1 %9, label %10, label %76
10: ; preds = %3
%11 = load i64, i64* %6
%12 = load i64, i64* %5
%13 = icmp ugt i64 %11, %12
br i1 %13, label %14, label %76
14: ; preds = %10
%15 = load i64, i64* %6
%16 = icmp ult i64 %15, 4096
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i64, i64* %6
%19 = call i8* @calloc(i32 1, i64 %18)
store i8* %19, i8** %7
br label %41
20: ; preds = %14
%21 = load i64, i64* %6
%22 = udiv i64 %21, 4096
%23 = load i64, i64* %6
%24 = urem i64 %23, 4096
%25 = icmp ne i64 %24, 0
%26 = zext i1 %25 to i32
%27 = sext i32 %26 to i64
%28 = add i64 %22, %27
%29 = mul i64 %28, 4096
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_ANON
%34 = load i32, i32* @MAP_PRIVATE
%35 = or i32 %33, %34
%36 = call i8* @mmap(i32* null, i64 %29, i32 %32, i32 %35, i32 -1, i32 0)
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = icmp eq i8* %37, inttoptr (i64 -1 to i8*)
br i1 %38, label %39, label %40
39: ; preds = %20
store i8* null, i8** %7
br label %40
40: ; preds = %39, %20
br label %41
41: ; preds = %40, %17
%42 = load i8*, i8** %4
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %75
44: ; preds = %41
%45 = load i64, i64* %5
%46 = icmp ugt i64 %45, 0
br i1 %46, label %47, label %74
47: ; preds = %44
%48 = load i8*, i8** %7
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i8*, i8** %7
%52 = load i8*, i8** %4
%53 = load i64, i64* %5
%54 = call i32 @memcpy(i8* %51, i8* %52, i64 %53)
br label %55
55: ; preds = %50, %47
%56 = load i64, i64* %5
%57 = icmp ult i64 %56, 4096
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i8*, i8** %4
%60 = call i32 @free(i8* %59)
br label %73
61: ; preds = %55
%62 = load i8*, i8** %4
%63 = load i64, i64* %5
%64 = udiv i64 %63, 4096
%65 = load i64, i64* %5
%66 = urem i64 %65, 4096
%67 = icmp ne i64 %66, 0
%68 = zext i1 %67 to i32
%69 = sext i32 %68 to i64
%70 = add i64 %64, %69
%71 = mul i64 %70, 4096
%72 = call i32 @munmap(i8* %62, i64 %71)
br label %73
73: ; preds = %61, %58
br label %74
74: ; preds = %73, %44
br label %75
75: ; preds = %74, %41
br label %76
76: ; preds = %75, %10, %3
%77 = load i8*, i8** %7
ret i8* %77
}

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_resize(i8* %0, i64 %1, i64 %2) {
%4 = icmp ugt i64 %2, %1
br i1 %4, label %5, label %44
5: ; preds = %3
%6 = icmp ult i64 %2, 4096
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call i8* @calloc(i32 1, i64 %2)
br label %24
9: ; preds = %5
%10 = and i64 %2, 4095
%11 = icmp eq i64 %10, 0
%12 = select i1 %11, i64 0, i64 4096
%13 = add i64 %12, %2
%14 = and i64 %13, -4096
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_ANON
%19 = load i32, i32* @MAP_PRIVATE
%20 = or i32 %19, %18
%21 = tail call i8* @mmap(i32* null, i64 %14, i32 %17, i32 %20, i32 -1, i32 0)
%22 = icmp eq i8* %21, inttoptr (i64 -1 to i8*)
%23 = select i1 %22, i8* null, i8* %21
br label %24
24: ; preds = %9, %7
%25 = phi i8* [ %8, %7 ], [ %23, %9 ]
%26 = icmp ne i8* %0, null
%27 = icmp ne i64 %1, 0
%28 = and i1 %26, %27
br i1 %28, label %29, label %44
29: ; preds = %24
%30 = icmp eq i8* %25, null
br i1 %30, label %33, label %31
31: ; preds = %29
%32 = tail call i32 @memcpy(i8* %25, i8* %0, i64 %1)
br label %33
33: ; preds = %31, %29
%34 = icmp ult i64 %1, 4096
br i1 %34, label %35, label %37
35: ; preds = %33
%36 = tail call i32 @free(i8* %0)
br label %44
37: ; preds = %33
%38 = and i64 %1, 4095
%39 = icmp eq i64 %38, 0
%40 = select i1 %39, i64 0, i64 4096
%41 = add i64 %40, %1
%42 = and i64 %41, -4096
%43 = tail call i32 @munmap(i8* %0, i64 %42)
br label %44
44: ; preds = %24, %35, %37, %3
%45 = phi i8* [ %25, %35 ], [ %25, %37 ], [ %25, %24 ], [ null, %3 ]
ret i8* %45
}