#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 pqos_cpuinfo {
  int dummy;
};
typedef int /*<<< orphan*/ sockets;

/* Variables and functions */
unsigned int *malloc(int);
int pqos_cpu_get_num_sockets(struct pqos_cpuinfo const *, unsigned int *);
int pqos_cpu_get_sockets(struct pqos_cpuinfo const *, unsigned int,
                         unsigned int *, unsigned int *);
int get_cpu_sockets(const struct pqos_cpuinfo *cpu, unsigned *number,
                    unsigned **table) {
  unsigned *sockets = NULL;
  unsigned sockets_num = 0, sockets_count = 0;
  int ret = 0;
  ;
  ;
  ;
  *number = 0;
  *table = NULL;
  ret = pqos_cpu_get_num_sockets(cpu, &sockets_count);
  if (ret != 0)
    return ret;
  sockets = malloc(sizeof(sockets[0]) * sockets_count);
  if (sockets == NULL)
    return 3;
  ret = pqos_cpu_get_sockets(cpu, sockets_count, &sockets_num, sockets);
  if (ret == 0) {
    *number = sockets_num;
    *table = sockets;
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_cpu_sockets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <pqos_cpu_get_num_sockets@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     119b <get_cpu_sockets+0x6b>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <get_cpu_sockets+0xcf>
mov    -0x30(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c4 <get_cpu_sockets+0x94>
movl   $0x3,-0x4(%rbp)
jmp    11ff <get_cpu_sockets+0xcf>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%rdx
call   1040 <pqos_cpu_get_sockets@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11f9 <get_cpu_sockets+0xc9>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cpu_sockets>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,(%rsi)
movq   $0x0,(%rdx)
lea    0x8(%rsp),%rsi
call   1050 <pqos_cpu_get_num_sockets@plt>
test   %eax,%eax
je     117f <get_cpu_sockets+0x4f>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%r13d
lea    0x0(,%r13,4),%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     11be <get_cpu_sockets+0x8e>
mov    %rax,%r12
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %rax,%rcx
call   1040 <pqos_cpu_get_sockets@plt>
test   %eax,%eax
jne    1171 <get_cpu_sockets+0x41>
mov    0xc(%rsp),%eax
mov    %eax,(%r15)
mov    %r12,(%r14)
xor    %eax,%eax
jmp    1171 <get_cpu_sockets+0x41>
mov    $0x3,%eax
jmp    1171 <get_cpu_sockets+0x41>

LLVM IR (-O0):
%struct.pqos_cpuinfo = type { i32 }
define i32 @get_cpu_sockets(%struct.pqos_cpuinfo* %0, i32* %1, i32** %2) {
%4 = alloca i32
%5 = alloca %struct.pqos_cpuinfo*
%6 = alloca i32*
%7 = alloca i32**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store %struct.pqos_cpuinfo* %0, %struct.pqos_cpuinfo** %5
store i32* %1, i32** %6
store i32** %2, i32*** %7
store i32* null, i32** %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
%12 = load i32*, i32** %6
store i32 0, i32* %12
%13 = load i32**, i32*** %7
store i32* null, i32** %13
%14 = load %struct.pqos_cpuinfo*, %struct.pqos_cpuinfo** %5
%15 = call i32 @pqos_cpu_get_num_sockets(%struct.pqos_cpuinfo* %14, i32* %10)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i32, i32* %11
store i32 %19, i32* %4
br label %43
20: ; preds = %3
%21 = load i32, i32* %10
%22 = zext i32 %21 to i64
%23 = mul i64 4, %22
%24 = trunc i64 %23 to i32
%25 = call i32* @malloc(i32 %24)
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %20
store i32 3, i32* %4
br label %43
29: ; preds = %20
%30 = load %struct.pqos_cpuinfo*, %struct.pqos_cpuinfo** %5
%31 = load i32, i32* %10
%32 = load i32*, i32** %8
%33 = call i32 @pqos_cpu_get_sockets(%struct.pqos_cpuinfo* %30, i32 %31, i32* %9, i32* %32)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %29
%37 = load i32, i32* %9
%38 = load i32*, i32** %6
store i32 %37, i32* %38
%39 = load i32*, i32** %8
%40 = load i32**, i32*** %7
store i32* %39, i32** %40
br label %41
41: ; preds = %36, %29
%42 = load i32, i32* %11
store i32 %42, i32* %4
br label %43
43: ; preds = %41, %28, %18
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
%struct.pqos_cpuinfo = type { i32 }
define i32 @get_cpu_sockets(%struct.pqos_cpuinfo* %0, i32* nocapture writeonly %1, i32** nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = bitcast i32* %5 to i8*
store i32 0, i32* %5
store i32 0, i32* %1
store i32* null, i32** %2
%8 = call i32 @pqos_cpu_get_num_sockets(%struct.pqos_cpuinfo* %0, i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %3
%11 = load i32, i32* %5
%12 = shl i32 %11, 2
%13 = call i32* @malloc(i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %20, label %15
15: ; preds = %10
%16 = call i32 @pqos_cpu_get_sockets(%struct.pqos_cpuinfo* %0, i32 %11, i32* %4, i32* %13)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i32, i32* %4
store i32 %19, i32* %1
store i32* %13, i32** %2
br label %20
20: ; preds = %15, %18, %10, %3
%21 = phi i32 [ %8, %3 ], [ 3, %10 ], [ 0, %18 ], [ %16, %15 ]
ret i32 %21
}