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

/* Variables and functions */
long get_ncpus_proc_cpuinfo(char *);
long get_ncpus_sys_devices(char *);
long get_ncpus_sysconf();
long loadavgwatch_impl_get_ncpus(void) {
  long ncpus_list[] = {
      get_ncpus_proc_cpuinfo("/proc/cpuinfo"),
      get_ncpus_sys_devices("/sys/devices/system/cpu/online"),
      get_ncpus_sysconf(),
  };
  long ncpus = -1;
  for (size_t i = 0; i < sizeof(ncpus_list) / sizeof(ncpus_list[0]); ++i) {
    if (ncpus_list[i] > ncpus) {
      ncpus = ncpus_list[i];
    }
  }
  return ncpus;
}


x86 assembly (clang, -O0):
<loadavgwatch_impl_get_ncpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec1(%rip),%rdi
call   1030 <get_ncpus_proc_cpuinfo@plt>
mov    %rax,-0x20(%rbp)
lea    0xebf(%rip),%rdi
call   1040 <get_ncpus_sys_devices@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <get_ncpus_sysconf@plt>
mov    %rax,-0x10(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
movq   $0x0,-0x30(%rbp)
cmpq   $0x3,-0x30(%rbp)
jae    11b4 <loadavgwatch_impl_get_ncpus+0x84>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rax
cmp    -0x28(%rbp),%rax
jle    119e <loadavgwatch_impl_get_ncpus+0x6e>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rax
mov    %rax,-0x28(%rbp)
jmp    11a3 <loadavgwatch_impl_get_ncpus+0x73>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1173 <loadavgwatch_impl_get_ncpus+0x43>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadavgwatch_impl_get_ncpus>:
push   %r14
push   %rbx
push   %rax
lea    0xec5(%rip),%rdi
call   1030 <get_ncpus_proc_cpuinfo@plt>
mov    %rax,%r14
lea    0xec4(%rip),%rdi
call   1040 <get_ncpus_sys_devices@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <get_ncpus_sysconf@plt>
test   %r14,%r14
mov    $0xffffffffffffffff,%rcx
cmovns %r14,%rcx
cmp    %rcx,%rbx
cmovg  %rbx,%rcx
cmp    %rcx,%rax
cmovle %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [31 x i8] c"/sys/devices/system/cpu/online\00"
define i64 @loadavgwatch_impl_get_ncpus() {
%1 = alloca [3 x i64]
%2 = alloca i64
%3 = alloca i64
%4 = getelementptr inbounds [3 x i64], [3 x i64]* %1, i64 0, i64 0
%5 = call i64 @get_ncpus_proc_cpuinfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i64 %5, i64* %4
%6 = getelementptr inbounds i64, i64* %4, i64 1
%7 = call i64 @get_ncpus_sys_devices(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i64 %7, i64* %6
%8 = getelementptr inbounds i64, i64* %6, i64 1
%9 = call i64 (...) @get_ncpus_sysconf()
store i64 %9, i64* %8
store i64 -1, i64* %2
store i64 0, i64* %3
br label %10
10: ; preds = %24, %0
%11 = load i64, i64* %3
%12 = icmp ult i64 %11, 3
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i64, i64* %3
%15 = getelementptr inbounds [3 x i64], [3 x i64]* %1, i64 0, i64 %14
%16 = load i64, i64* %15
%17 = load i64, i64* %2
%18 = icmp sgt i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i64, i64* %3
%21 = getelementptr inbounds [3 x i64], [3 x i64]* %1, i64 0, i64 %20
%22 = load i64, i64* %21
store i64 %22, i64* %2
br label %23
23: ; preds = %19, %13
br label %24
24: ; preds = %23
%25 = load i64, i64* %3
%26 = add i64 %25, 1
store i64 %26, i64* %3
br label %10
27: ; preds = %10
%28 = load i64, i64* %2
ret i64 %28
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [31 x i8] c"/sys/devices/system/cpu/online\00"
define i64 @loadavgwatch_impl_get_ncpus() {
%1 = tail call i64 @get_ncpus_proc_cpuinfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%2 = tail call i64 @get_ncpus_sys_devices(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i64 (...) @get_ncpus_sysconf()
%4 = icmp sgt i64 %1, -1
%5 = select i1 %4, i64 %1, i64 -1
%6 = icmp sgt i64 %2, %5
%7 = select i1 %6, i64 %2, i64 %5
%8 = icmp sgt i64 %3, %7
%9 = select i1 %8, i64 %3, i64 %7
ret i64 %9
}