#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 bitmask {
  int /*<<< orphan*/ size;
  int /*<<< orphan*/ maskp;
};

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_PRIVATE;
int /*<<< orphan*/ MPOL_BIND;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fix_interleaving(size_t, size_t *, int const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ mbind(void *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
                 int /*<<< orphan*/, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
struct bitmask *numa_allocate_nodemask();
scalar_t__ numa_available();
int /*<<< orphan*/ numa_bitmask_clearbit(struct bitmask *, int const);
int /*<<< orphan*/ numa_bitmask_free(struct bitmask *);
int /*<<< orphan*/ numa_bitmask_setbit(struct bitmask *, int const);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void *alloc_interleaved(size_t size, size_t *parts, size_t nr_parts,
                        const int *nodes) {
  int i;
  void *ret = NULL;
  if (numa_available() < 0) {
    fprintf(stderr, "numa not implemented\n");
    exit(1);
  }
  ret = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0,
             0);
  if (ret == (void *)-1) {
    ret = NULL;
    goto exit;
  }
  struct bitmask *nodemask = numa_allocate_nodemask();
  fix_interleaving(nr_parts, parts, nodes);
  void *curr_part = ret;
  for (i = 0; i < nr_parts; i++) {
    numa_bitmask_setbit(nodemask, nodes[i]);
    if (parts[i] != 0 && mbind(curr_part, parts[i], MPOL_BIND, nodemask->maskp,
                               nodemask->size, 0) < 0) {
      perror("mbind");
      exit(1);
    }
    curr_part += parts[i];
    numa_bitmask_clearbit(nodemask, nodes[i]);
  }
  numa_bitmask_free(nodemask);
exit:
  return ret;
}


x86 assembly (clang, -O0):
<alloc_interleaved>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <numa_available@plt>
cmp    $0x0,%rax
jge    1200 <alloc_interleaved+0x50>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%rsi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
mov    0x2dac(%rip),%rax
or     (%rax),%edx
mov    0x2dbb(%rip),%rax
mov    (%rax),%ecx
mov    0x2d92(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1060 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x30(%rbp)
jne    1259 <alloc_interleaved+0xa9>
movq   $0x0,-0x30(%rbp)
jmp    1347 <alloc_interleaved+0x197>
mov    $0x0,%al
call   1070 <numa_allocate_nodemask@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <fix_interleaving@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    133e <alloc_interleaved+0x18e>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1050 <numa_bitmask_setbit@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1308 <alloc_interleaved+0x158>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    0x2d18(%rip),%rax
mov    (%rax),%edx
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x38(%rbp),%rax
mov    (%rax),%r8d
xor    %r9d,%r9d
call   1040 <mbind@plt>
cmp    $0x0,%rax
jge    1308 <alloc_interleaved+0x158>
lea    0xd1d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1030 <numa_bitmask_clearbit@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1284 <alloc_interleaved+0xd4>
mov    -0x38(%rbp),%rdi
call   10c0 <numa_bitmask_free@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_interleaved>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <numa_available@plt>
test   %rax,%rax
js     12da <alloc_interleaved+0x12a>
mov    0x2e1a(%rip),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de8(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %rbx,%rsi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,(%rsp)
cmp    $0xffffffffffffffff,%rax
je     12b2 <alloc_interleaved+0x102>
xor    %eax,%eax
call   1070 <numa_allocate_nodemask@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1090 <fix_interleaving@plt>
test   %r14,%r14
je     12a6 <alloc_interleaved+0xf6>
xor    %r12d,%r12d
mov    (%rsp),%rbp
jmp    126b <alloc_interleaved+0xbb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
add    %rax,%rbp
mov    0x0(%r13,%r12,4),%esi
mov    %rbx,%rdi
call   1030 <numa_bitmask_clearbit@plt>
add    $0x1,%r12
cmp    %r12,%r14
je     12a6 <alloc_interleaved+0xf6>
mov    0x0(%r13,%r12,4),%esi
mov    %rbx,%rdi
call   1050 <numa_bitmask_setbit@plt>
mov    (%r15,%r12,8),%rsi
test   %rsi,%rsi
je     1250 <alloc_interleaved+0xa0>
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
mov    (%rbx),%r8d
mov    0x4(%rbx),%ecx
mov    %rbp,%rdi
xor    %r9d,%r9d
call   1040 <mbind@plt>
test   %rax,%rax
js     12c4 <alloc_interleaved+0x114>
mov    (%r15,%r12,8),%rax
jmp    1252 <alloc_interleaved+0xa2>
mov    %rbx,%rdi
call   10c0 <numa_bitmask_free@plt>
mov    (%rsp),%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.bitmask = type { i32, i32 }
@stderr = global i32 0
@.str = [22 x i8] c"numa not implemented\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MPOL_BIND = global i32 0
@.str.1 = [6 x i8] c"mbind\00"
define i8* @alloc_interleaved(i64 %0, i64* %1, i64 %2, i32* %3) {
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.bitmask*
%12 = alloca i8*
store i64 %0, i64* %5
store i64* %1, i64** %6
store i64 %2, i64* %7
store i32* %3, i32** %8
store i8* null, i8** %10
%13 = call i64 (...) @numa_available()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %4
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %4
%20 = load i64, i64* %5
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_ANONYMOUS
%25 = load i32, i32* @MAP_PRIVATE
%26 = or i32 %24, %25
%27 = call i8* @mmap(i32* null, i64 %20, i32 %23, i32 %26, i32 0, i32 0)
store i8* %27, i8** %10
%28 = load i8*, i8** %10
%29 = icmp eq i8* %28, inttoptr (i64 -1 to i8*)
br i1 %29, label %30, label %31
30: ; preds = %19
store i8* null, i8** %10
br label %97
31: ; preds = %19
%32 = call %struct.bitmask* (...) @numa_allocate_nodemask()
store %struct.bitmask* %32, %struct.bitmask** %11
%33 = load i64, i64* %7
%34 = load i64*, i64** %6
%35 = load i32*, i32** %8
%36 = call i32 @fix_interleaving(i64 %33, i64* %34, i32* %35)
%37 = load i8*, i8** %10
store i8* %37, i8** %12
store i32 0, i32* %9
br label %38
38: ; preds = %91, %31
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = load i64, i64* %7
%42 = icmp ult i64 %40, %41
br i1 %42, label %43, label %94
43: ; preds = %38
%44 = load %struct.bitmask*, %struct.bitmask** %11
%45 = load i32*, i32** %8
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load i32, i32* %48
%50 = call i32 @numa_bitmask_setbit(%struct.bitmask* %44, i32 %49)
%51 = load i64*, i64** %6
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i64, i64* %51, i64 %53
%55 = load i64, i64* %54
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %76
57: ; preds = %43
%58 = load i8*, i8** %12
%59 = load i64*, i64** %6
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i64, i64* %59, i64 %61
%63 = load i64, i64* %62
%64 = load i32, i32* @MPOL_BIND
%65 = load %struct.bitmask*, %struct.bitmask** %11
%66 = getelementptr inbounds %struct.bitmask, %struct.bitmask* %65, i32 0, i32 1
%67 = load i32, i32* %66
%68 = load %struct.bitmask*, %struct.bitmask** %11
%69 = getelementptr inbounds %struct.bitmask, %struct.bitmask* %68, i32 0, i32 0
%70 = load i32, i32* %69
%71 = call i64 @mbind(i8* %58, i64 %63, i32 %64, i32 %67, i32 %70, i32 0)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %57
%74 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %57, %43
%77 = load i64*, i64** %6
%78 = load i32, i32* %9
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i64, i64* %77, i64 %79
%81 = load i64, i64* %80
%82 = load i8*, i8** %12
%83 = getelementptr i8, i8* %82, i64 %81
store i8* %83, i8** %12
%84 = load %struct.bitmask*, %struct.bitmask** %11
%85 = load i32*, i32** %8
%86 = load i32, i32* %9
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i32, i32* %85, i64 %87
%89 = load i32, i32* %88
%90 = call i32 @numa_bitmask_clearbit(%struct.bitmask* %84, i32 %89)
br label %91
91: ; preds = %76
%92 = load i32, i32* %9
%93 = add nsw i32 %92, 1
store i32 %93, i32* %9
br label %38
94: ; preds = %38
%95 = load %struct.bitmask*, %struct.bitmask** %11
%96 = call i32 @numa_bitmask_free(%struct.bitmask* %95)
br label %97
97: ; preds = %94, %30
%98 = load i8*, i8** %10
ret i8* %98
}

LLVM IR (-O2):
%struct.bitmask = type { i32, i32 }
@stderr = global i32 0
@.str = [22 x i8] c"numa not implemented\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MPOL_BIND = global i32 0
@.str.1 = [6 x i8] c"mbind\00"
define i8* @alloc_interleaved(i64 %0, i64* %1, i64 %2, i32* %3) {
%5 = tail call i64 (...) @numa_available()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %4
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_ANONYMOUS
%16 = load i32, i32* @MAP_PRIVATE
%17 = or i32 %16, %15
%18 = tail call i8* @mmap(i32* null, i64 %0, i32 %14, i32 %17, i32 0, i32 0)
%19 = icmp eq i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %56, label %20
20: ; preds = %11
%21 = tail call %struct.bitmask* (...) @numa_allocate_nodemask()
%22 = tail call i32 @fix_interleaving(i64 %2, i64* %1, i32* %3)
%23 = icmp eq i64 %2, 0
br i1 %23, label %54, label %24
24: ; preds = %20
%25 = getelementptr inbounds %struct.bitmask, %struct.bitmask* %21, i64 0, i32 1
%26 = getelementptr inbounds %struct.bitmask, %struct.bitmask* %21, i64 0, i32 0
br label %27
27: ; preds = %24, %47
%28 = phi i64 [ 0, %24 ], [ %52, %47 ]
%29 = phi i8* [ %18, %24 ], [ %49, %47 ]
%30 = getelementptr inbounds i32, i32* %3, i64 %28
%31 = load i32, i32* %30
%32 = tail call i32 @numa_bitmask_setbit(%struct.bitmask* %21, i32 %31)
%33 = getelementptr inbounds i64, i64* %1, i64 %28
%34 = load i64, i64* %33
%35 = icmp eq i64 %34, 0
br i1 %35, label %47, label %36
36: ; preds = %27
%37 = load i32, i32* @MPOL_BIND
%38 = load i32, i32* %25
%39 = load i32, i32* %26
%40 = tail call i64 @mbind(i8* %29, i64 %34, i32 %37, i32 %38, i32 %39, i32 0)
%41 = icmp slt i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %36
%43 = load i64, i64* %33
br label %47
44: ; preds = %36
%45 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%46 = tail call i32 @exit(i32 1)
unreachable
47: ; preds = %42, %27
%48 = phi i64 [ %43, %42 ], [ 0, %27 ]
%49 = getelementptr i8, i8* %29, i64 %48
%50 = load i32, i32* %30
%51 = tail call i32 @numa_bitmask_clearbit(%struct.bitmask* %21, i32 %50)
%52 = add nuw i64 %28, 1
%53 = icmp eq i64 %52, %2
br i1 %53, label %54, label %27
54: ; preds = %47, %20
%55 = tail call i32 @numa_bitmask_free(%struct.bitmask* %21)
br label %56
56: ; preds = %11, %54
%57 = phi i8* [ %18, %54 ], [ null, %11 ]
ret i8* %57
}