#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*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ CPU_FREE(int /*<<< orphan*/ *);
scalar_t__ CPU_ISSET_S(int, size_t, int /*<<< orphan*/ *);
int num_online_cpus();
int read_mapping(int, char *, int /*<<< orphan*/ **, size_t *);
int domain_to_first_cpu(int domain) {
  cpu_set_t *bits;
  size_t sz;
  int i, n_online;
  int first;
  int ret = read_mapping(domain, "domains", &bits, &sz);
  if (ret)
    return ret;
  n_online = num_online_cpus();
  first = -1;
  for (i = 0; i < n_online; ++i) {
    if (CPU_ISSET_S(i, sz, bits)) {
      first = i;
      break;
    }
  }
  CPU_FREE(bits);
  return first;
}


x86 assembly (clang, -O0):
<domain_to_first_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    0xeab(%rip),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1050 <read_mapping@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     117a <domain_to_first_cpu+0x3a>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e5 <domain_to_first_cpu+0xa5>
mov    $0x0,%al
call   1030 <num_online_cpus@plt>
mov    %eax,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11d6 <domain_to_first_cpu+0x96>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1060 <CPU_ISSET_S@plt>
cmp    $0x0,%rax
je     11c3 <domain_to_first_cpu+0x83>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11d6 <domain_to_first_cpu+0x96>
jmp    11c8 <domain_to_first_cpu+0x88>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1192 <domain_to_first_cpu+0x52>
mov    -0x10(%rbp),%rdi
call   1040 <CPU_FREE@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<domain_to_first_cpu>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeb1(%rip),%rsi
mov    %rsp,%rdx
lea    0x8(%rsp),%rcx
call   1050 <read_mapping@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    11aa <domain_to_first_cpu+0x6a>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <num_online_cpus@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jle    11a1 <domain_to_first_cpu+0x61>
mov    %eax,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rsi
mov    (%rsp),%rdx
mov    %ebp,%edi
call   1060 <CPU_ISSET_S@plt>
test   %rax,%rax
jne    119e <domain_to_first_cpu+0x5e>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1180 <domain_to_first_cpu+0x40>
jmp    11a1 <domain_to_first_cpu+0x61>
mov    %ebp,%r14d
mov    (%rsp),%rdi
call   1040 <CPU_FREE@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"domains\00"
define i32 @domain_to_first_cpu(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = load i32, i32* %3
%11 = call i32 @read_mapping(i32 %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32** %4, i64* %5)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* %9
store i32 %15, i32* %2
br label %38
16: ; preds = %1
%17 = call i32 (...) @num_online_cpus()
store i32 %17, i32* %7
store i32 -1, i32* %8
store i32 0, i32* %6
br label %18
18: ; preds = %31, %16
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %34
22: ; preds = %18
%23 = load i32, i32* %6
%24 = load i64, i64* %5
%25 = load i32*, i32** %4
%26 = call i64 @CPU_ISSET_S(i32 %23, i64 %24, i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* %6
store i32 %29, i32* %8
br label %34
30: ; preds = %22
br label %31
31: ; preds = %30
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %18
34: ; preds = %28, %18
%35 = load i32*, i32** %4
%36 = call i32 @CPU_FREE(i32* %35)
%37 = load i32, i32* %8
store i32 %37, i32* %2
br label %38
38: ; preds = %34, %14
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@.str = [8 x i8] c"domains\00"
define i32 @domain_to_first_cpu(i32 %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = bitcast i32** %2 to i8*
%5 = bitcast i64* %3 to i8*
%6 = call i32 @read_mapping(i32 %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32** %2, i64* %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %24
8: ; preds = %1
%9 = call i32 (...) @num_online_cpus()
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %8, %17
%12 = phi i32 [ %18, %17 ], [ 0, %8 ]
%13 = load i64, i64* %3
%14 = load i32*, i32** %2
%15 = call i64 @CPU_ISSET_S(i32 %12, i64 %13, i32* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = add nuw nsw i32 %12, 1
%19 = icmp eq i32 %18, %9
br i1 %19, label %20, label %11
20: ; preds = %17, %11, %8
%21 = phi i32 [ -1, %8 ], [ %12, %11 ], [ -1, %17 ]
%22 = load i32*, i32** %2
%23 = call i32 @CPU_FREE(i32* %22)
br label %24
24: ; preds = %1, %20
%25 = phi i32 [ %21, %20 ], [ %6, %1 ]
ret i32 %25
}