#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 int /*<<< orphan*/ fname;
typedef int /*<<< orphan*/ cpu_set_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ *CPU_ALLOC(int);
size_t CPU_ALLOC_SIZE(int);
int /*<<< orphan*/ CPU_SET_S(int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO_S(size_t, int /*<<< orphan*/ *);
int ffsl(unsigned long);
int num_online_cpus();
int read_file(char *, char **, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int);
int strnlen(char *, int);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int read_mapping(int idx, const char *which, cpu_set_t **set, size_t *sz) {
  int ret = -1;
  char buf[4096 / 4 + 4096 / (4 * 8) + 1] = {0};
  char fname[80] = {0};
  char *chunk_str;
  int len, nbits;
  int i;
  *set = NULL;
  *sz = 0;
  if (num_online_cpus() > 4096)
    goto out;
  snprintf(fname, sizeof(fname), "/proc/litmus/%s/%d", which, idx);
  ret = read_file(fname, &buf, sizeof(buf) - 1);
  if (ret <= 0)
    goto out;
  len = strnlen(buf, sizeof(buf));
  nbits = 32 * (len / 9) + 4 * (len % 9);
  *set = CPU_ALLOC(nbits);
  *sz = CPU_ALLOC_SIZE(nbits);
  CPU_ZERO_S(*sz, *set);
  chunk_str = buf + len - 8;
  i = 0;
  do {
    unsigned long chunk;
    if (chunk_str < buf)
      chunk_str = buf;
    chunk = strtoul(chunk_str, NULL, 16);
    while (chunk) {
      int j = ffsl(chunk) - 1;
      int x = i * 32 + j;
      CPU_SET_S(x, *sz, *set);
      chunk &= ~(1ul << j);
    }
    chunk_str -= 9;
    i += 1;
  } while (chunk_str >= buf - 8);
  ret = 0;
out:
  return ret;
}


x86 assembly (clang, -O0):
<read_mapping>:
push   %rbp
mov    %rsp,%rbp
sub    $0x530,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    -0x4b0(%rbp),%rdi
xor    %esi,%esi
mov    $0x481,%edx
call   1070 <memset@plt>
lea    -0x500(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1090 <num_online_cpus@plt>
cmp    $0x1000,%eax
jle    1224 <read_mapping+0x74>
jmp    141a <read_mapping+0x26a>
lea    -0x500(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x4(%rbp),%r8d
mov    $0x50,%esi
lea    0xdc1(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x500(%rbp),%rdi
lea    -0x4b0(%rbp),%rsi
mov    $0x480,%edx
call   10a0 <read_file@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     126e <read_mapping+0xbe>
jmp    141a <read_mapping+0x26a>
lea    -0x4b0(%rbp),%rdi
mov    $0x481,%esi
call   1080 <strnlen@plt>
mov    %eax,-0x50c(%rbp)
mov    -0x50c(%rbp),%eax
mov    $0x9,%ecx
cltd
idiv   %ecx
shl    $0x5,%eax
mov    %eax,-0x52c(%rbp)
mov    -0x50c(%rbp),%eax
mov    $0x9,%ecx
cltd
idiv   %ecx
mov    -0x52c(%rbp),%eax
shl    $0x2,%edx
add    %edx,%eax
mov    %eax,-0x510(%rbp)
mov    -0x510(%rbp),%edi
call   1030 <CPU_ALLOC@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x510(%rbp),%edi
call   1050 <CPU_ALLOC_SIZE@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   10d0 <CPU_ZERO_S@plt>
lea    -0x4b0(%rbp),%rax
movslq -0x50c(%rbp),%rcx
add    %rcx,%rax
add    $0xfffffffffffffff8,%rax
mov    %rax,-0x508(%rbp)
movl   $0x0,-0x514(%rbp)
mov    -0x508(%rbp),%rax
lea    -0x4b0(%rbp),%rcx
cmp    %rcx,%rax
jae    1343 <read_mapping+0x193>
lea    -0x4b0(%rbp),%rax
mov    %rax,-0x508(%rbp)
mov    -0x508(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    %rax,-0x520(%rbp)
cmpq   $0x0,-0x520(%rbp)
je     13d7 <read_mapping+0x227>
mov    -0x520(%rbp),%rdi
call   1040 <ffsl@plt>
sub    $0x1,%eax
mov    %eax,-0x524(%rbp)
mov    -0x514(%rbp),%eax
shl    $0x5,%eax
add    -0x524(%rbp),%eax
mov    %eax,-0x528(%rbp)
mov    -0x528(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
call   10c0 <CPU_SET_S@plt>
mov    -0x524(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
xor    $0xffffffffffffffff,%rax
and    -0x520(%rbp),%rax
mov    %rax,-0x520(%rbp)
jmp    135f <read_mapping+0x1af>
mov    -0x508(%rbp),%rax
add    $0xfffffffffffffff7,%rax
mov    %rax,-0x508(%rbp)
mov    -0x514(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x514(%rbp)
mov    -0x508(%rbp),%rax
lea    -0x4b0(%rbp),%rcx
add    $0xfffffffffffffff8,%rcx
cmp    %rcx,%rax
jae    131e <read_mapping+0x16e>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x530,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mapping>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4e8,%rsp
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %edi,%ebp
lea    0x60(%rsp),%rdi
mov    $0x481,%edx
xor    %esi,%esi
call   1060 <memset@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x0(%r13)
movq   $0x0,(%r14)
xor    %eax,%eax
call   1080 <num_online_cpus@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1000,%ecx
jg     134c <read_mapping+0x1ac>
lea    0xde7(%rip),%rdx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x50,%esi
mov    %r15,%rcx
mov    %ebp,%r8d
call   1050 <snprintf@plt>
lea    0x60(%rsp),%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x480,%edx
call   1090 <read_file@plt>
test   %eax,%eax
jle    134c <read_mapping+0x1ac>
mov    %rbx,%rdi
mov    $0x481,%esi
call   1070 <strnlen@plt>
movslq %eax,%r15
imul   $0x38e38e39,%r15,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rax
add    %ecx,%eax
lea    (%rax,%rax,8),%ecx
shl    $0x5,%eax
mov    %r15d,%edx
sub    %ecx,%edx
lea    (%rax,%rdx,4),%ebp
mov    %ebp,%edi
call   1030 <CPU_ALLOC@plt>
mov    %rax,0x0(%r13)
mov    %ebp,%edi
call   1040 <CPU_ALLOC_SIZE@plt>
mov    %rax,(%r14)
mov    0x0(%r13),%rsi
mov    %rax,%rdi
call   10c0 <CPU_ZERO_S@plt>
add    %rsp,%r15
add    $0x58,%r15
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    12ef <read_mapping+0x14f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x60(%rsp),%rbx
cmp    %rbx,%r15
lea    -0x9(%r15),%rax
lea    0x57(%rsp),%rcx
cmovb  %rcx,%rax
mov    0x8(%rsp),%rcx
add    $0x1,%ecx
mov    %rcx,0x8(%rsp)
mov    %rax,%r15
lea    0x58(%rsp),%rcx
cmp    %rcx,%rax
jb     134a <read_mapping+0x1aa>
cmp    %rbx,%r15
mov    %r15,%rdi
cmovb  %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
test   %rax,%rax
je     12c0 <read_mapping+0x120>
mov    %rax,%rbp
mov    0x8(%rsp),%r12
shl    $0x5,%r12d
cs nopw 0x0(%rax,%rax,1)
bsf    %rbp,%rbx
lea    (%r12,%rbx,1),%edi
mov    (%r14),%rsi
mov    0x0(%r13),%rdx
call   10b0 <CPU_SET_S@plt>
mov    $0xfffffffffffffffe,%rax
mov    %ebx,%ecx
rol    %cl,%rax
and    %rax,%rbp
jne    1320 <read_mapping+0x180>
jmp    12c0 <read_mapping+0x120>
xor    %eax,%eax
add    $0x4e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/litmus/%s/%d\00"
define i32 @read_mapping(i32 %0, i8* %1, i32** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32**
%8 = alloca i64*
%9 = alloca i32
%10 = alloca [1153 x i8]
%11 = alloca [80 x i8]
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32** %2, i32*** %7
store i64* %3, i64** %8
store i32 -1, i32* %9
%19 = bitcast [1153 x i8]* %10 to i8*
%20 = bitcast [80 x i8]* %11 to i8*
%21 = load i32**, i32*** %7
store i32* null, i32** %21
%22 = load i64*, i64** %8
store i64 0, i64* %22
%23 = call i32 (...) @num_online_cpus()
%24 = icmp sgt i32 %23, 4096
br i1 %24, label %25, label %26
25: ; preds = %4
br label %106
26: ; preds = %4
%27 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%28 = load i8*, i8** %6
%29 = load i32, i32* %5
%30 = call i32 @snprintf(i8* %27, i32 80, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %28, i32 %29)
%31 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%32 = bitcast [1153 x i8]* %10 to i8**
%33 = call i32 @read_file(i8* %31, i8** %32, i32 1152)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp sle i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %26
br label %106
37: ; preds = %26
%38 = getelementptr inbounds [1153 x i8], [1153 x i8]* %10, i64 0, i64 0
%39 = call i32 @strnlen(i8* %38, i32 1153)
store i32 %39, i32* %13
%40 = load i32, i32* %13
%41 = sdiv i32 %40, 9
%42 = mul nsw i32 32, %41
%43 = load i32, i32* %13
%44 = srem i32 %43, 9
%45 = mul nsw i32 4, %44
%46 = add nsw i32 %42, %45
store i32 %46, i32* %14
%47 = load i32, i32* %14
%48 = call i32* @CPU_ALLOC(i32 %47)
%49 = load i32**, i32*** %7
store i32* %48, i32** %49
%50 = load i32, i32* %14
%51 = call i64 @CPU_ALLOC_SIZE(i32 %50)
%52 = load i64*, i64** %8
store i64 %51, i64* %52
%53 = load i64*, i64** %8
%54 = load i64, i64* %53
%55 = load i32**, i32*** %7
%56 = load i32*, i32** %55
%57 = call i32 @CPU_ZERO_S(i64 %54, i32* %56)
%58 = getelementptr inbounds [1153 x i8], [1153 x i8]* %10, i64 0, i64 0
%59 = load i32, i32* %13
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = getelementptr inbounds i8, i8* %61, i64 -8
store i8* %62, i8** %12
store i32 0, i32* %15
br label %63
63: ; preds = %100, %37
%64 = load i8*, i8** %12
%65 = getelementptr inbounds [1153 x i8], [1153 x i8]* %10, i64 0, i64 0
%66 = icmp ult i8* %64, %65
br i1 %66, label %67, label %69
67: ; preds = %63
%68 = getelementptr inbounds [1153 x i8], [1153 x i8]* %10, i64 0, i64 0
store i8* %68, i8** %12
br label %69
69: ; preds = %67, %63
%70 = load i8*, i8** %12
%71 = call i64 @strtoul(i8* %70, i32* null, i32 16)
store i64 %71, i64* %16
br label %72
72: ; preds = %75, %69
%73 = load i64, i64* %16
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %95
75: ; preds = %72
%76 = load i64, i64* %16
%77 = call i32 @ffsl(i64 %76)
%78 = sub nsw i32 %77, 1
store i32 %78, i32* %17
%79 = load i32, i32* %15
%80 = mul nsw i32 %79, 32
%81 = load i32, i32* %17
%82 = add nsw i32 %80, %81
store i32 %82, i32* %18
%83 = load i32, i32* %18
%84 = load i64*, i64** %8
%85 = load i64, i64* %84
%86 = load i32**, i32*** %7
%87 = load i32*, i32** %86
%88 = call i32 @CPU_SET_S(i32 %83, i64 %85, i32* %87)
%89 = load i32, i32* %17
%90 = zext i32 %89 to i64
%91 = shl i64 1, %90
%92 = xor i64 %91, -1
%93 = load i64, i64* %16
%94 = and i64 %93, %92
store i64 %94, i64* %16
br label %72
95: ; preds = %72
%96 = load i8*, i8** %12
%97 = getelementptr inbounds i8, i8* %96, i64 -9
store i8* %97, i8** %12
%98 = load i32, i32* %15
%99 = add nsw i32 %98, 1
store i32 %99, i32* %15
br label %100
100: ; preds = %95
%101 = load i8*, i8** %12
%102 = getelementptr inbounds [1153 x i8], [1153 x i8]* %10, i64 0, i64 0
%103 = getelementptr inbounds i8, i8* %102, i64 -8
%104 = icmp uge i8* %101, %103
br i1 %104, label %63, label %105
105: ; preds = %100
store i32 0, i32* %9
br label %106
106: ; preds = %105, %36, %25
%107 = load i32, i32* %9
ret i32 %107
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/litmus/%s/%d\00"
define i32 @read_mapping(i32 %0, i8* %1, i32** nocapture %2, i64* nocapture %3) {
%5 = alloca [1153 x i8]
%6 = alloca [80 x i8]
%7 = getelementptr inbounds [1153 x i8], [1153 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
store i32* null, i32** %2
store i64 0, i64* %3
%9 = tail call i32 (...) @num_online_cpus()
%10 = icmp sgt i32 %9, 4096
br i1 %10, label %58, label %11
11: ; preds = %4
%12 = call i32 @snprintf(i8* %8, i32 80, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %0)
%13 = bitcast [1153 x i8]* %5 to i8**
%14 = call i32 @read_file(i8* %8, i8** %13, i32 1152)
%15 = icmp slt i32 %14, 1
br i1 %15, label %58, label %16
16: ; preds = %11
%17 = call i32 @strnlen(i8* %7, i32 1153)
%18 = sdiv i32 %17, 9
%19 = shl nsw i32 %18, 5
%20 = srem i32 %17, 9
%21 = shl nsw i32 %20, 2
%22 = add nsw i32 %19, %21
%23 = call i32* @CPU_ALLOC(i32 %22)
store i32* %23, i32** %2
%24 = call i64 @CPU_ALLOC_SIZE(i32 %22)
store i64 %24, i64* %3
%25 = load i32*, i32** %2
%26 = call i32 @CPU_ZERO_S(i64 %24, i32* %25)
%27 = sext i32 %17 to i64
%28 = getelementptr inbounds [1153 x i8], [1153 x i8]* %5, i64 0, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -8
%30 = getelementptr inbounds [1153 x i8], [1153 x i8]* %5, i64 0, i64 -9
%31 = getelementptr inbounds [1153 x i8], [1153 x i8]* %5, i64 0, i64 -8
br label %32
32: ; preds = %53, %16
%33 = phi i32 [ 0, %16 ], [ %56, %53 ]
%34 = phi i8* [ %29, %16 ], [ %55, %53 ]
%35 = icmp ult i8* %34, %7
%36 = select i1 %35, i8* %7, i8* %34
%37 = call i64 @strtoul(i8* nocapture %36, i32* null, i32 16)
%38 = icmp eq i64 %37, 0
br i1 %38, label %53, label %39
39: ; preds = %32
%40 = shl nsw i32 %33, 5
br label %41
41: ; preds = %39, %41
%42 = phi i64 [ %37, %39 ], [ %51, %41 ]
%43 = call i64 @llvm.cttz.i64(i64 %42, i1 true)
%44 = trunc i64 %43 to i32
%45 = add nuw nsw i32 %40, %44
%46 = load i64, i64* %3
%47 = load i32*, i32** %2
%48 = call i32 @CPU_SET_S(i32 %45, i64 %46, i32* %47)
%49 = shl nuw i64 1, %43
%50 = xor i64 %49, -1
%51 = and i64 %42, %50
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %41
53: ; preds = %41, %32
%54 = getelementptr inbounds i8, i8* %34, i64 -9
%55 = select i1 %35, i8* %30, i8* %54
%56 = add nuw nsw i32 %33, 1
%57 = icmp ult i8* %55, %31
br i1 %57, label %58, label %32
58: ; preds = %53, %11, %4
%59 = phi i32 [ -1, %4 ], [ %14, %11 ], [ 0, %53 ]
ret i32 %59
}