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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int get_socket(int);
int *malloc(int);
int /*<<< orphan*/ perror(char *);
int recv(int, int *, int, int /*<<< orphan*/);
int send(int, int *, int, int /*<<< orphan*/);
int get_cores_list(int pid, int *nr_cores, int **cores) {
  int s = get_socket(pid);
  if (s < 0)
    return s;
  int action = 0;
  if (send(s, &action, sizeof(action), 0) == -1) {
    perror("send");
    return -2;
  }
  if (recv(s, nr_cores, sizeof(*nr_cores), 0) == -1) {
    perror("recv");
    return -2;
  }
  int i;
  *cores = malloc(*nr_cores * sizeof(**cores));
  for (i = 0; i < *nr_cores; i++) {
    if (recv(s, &((*cores)[i]), sizeof((*cores)[i]), 0) == -1) {
      perror("recv");
      return -3;
    }
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<get_cores_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <get_socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1193 <get_cores_list+0x33>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1298 <get_cores_list+0x138>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    11ce <get_cores_list+0x6e>
lea    0xe43(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1298 <get_cores_list+0x138>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    1202 <get_cores_list+0xa2>
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1298 <get_cores_list+0x138>
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    1289 <get_cores_list+0x129>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    1276 <get_cores_list+0x116>
lea    0xda0(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1298 <get_cores_list+0x138>
jmp    127b <get_cores_list+0x11b>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1225 <get_cores_list+0xc5>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cores_list>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
call   1040 <get_socket@plt>
mov    %eax,%r12d
test   %eax,%eax
js     1249 <get_cores_list+0xe9>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %r12d,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
je     1259 <get_cores_list+0xf9>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1262 <get_cores_list+0x102>
mov    (%r15),%ebx
lea    0x0(,%rbx,4),%edi
call   1070 <malloc@plt>
mov    %rax,(%r14)
test   %ebx,%ebx
jle    123e <get_cores_list+0xde>
mov    %r12d,%edi
mov    %rax,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     122a <get_cores_list+0xca>
mov    $0x4,%esi
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq (%r15),%rax
add    $0x1,%rbx
cmp    %rax,%rbx
jge    123e <get_cores_list+0xde>
lea    0x4(%rsi),%rbp
add    (%r14),%rsi
mov    %r12d,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rbp,%rsi
cmp    $0xffffffff,%eax
jne    1200 <get_cores_list+0xa0>
lea    0xdd4(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffd,%r12d
jmp    1249 <get_cores_list+0xe9>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda0(%rip),%rdi
jmp    1269 <get_cores_list+0x109>
lea    0xd9c(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffe,%r12d
jmp    1249 <get_cores_list+0xe9>

LLVM IR (-O0):
@.str = [5 x i8] c"send\00"
@.str.1 = [5 x i8] c"recv\00"
define i32 @get_cores_list(i32 %0, i32* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32** %2, i32*** %7
%11 = load i32, i32* %5
%12 = call i32 @get_socket(i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* %8
store i32 %16, i32* %4
br label %61
17: ; preds = %3
store i32 0, i32* %9
%18 = load i32, i32* %8
%19 = call i32 @send(i32 %18, i32* %9, i32 4, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %4
br label %61
23: ; preds = %17
%24 = load i32, i32* %8
%25 = load i32*, i32** %6
%26 = call i32 @recv(i32 %24, i32* %25, i32 4, i32 0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %4
br label %61
30: ; preds = %23
%31 = load i32*, i32** %6
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = mul i64 %33, 4
%35 = trunc i64 %34 to i32
%36 = call i32* @malloc(i32 %35)
%37 = load i32**, i32*** %7
store i32* %36, i32** %37
store i32 0, i32* %10
br label %38
38: ; preds = %55, %30
%39 = load i32, i32* %10
%40 = load i32*, i32** %6
%41 = load i32, i32* %40
%42 = icmp slt i32 %39, %41
br i1 %42, label %43, label %58
43: ; preds = %38
%44 = load i32, i32* %8
%45 = load i32**, i32*** %7
%46 = load i32*, i32** %45
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = call i32 @recv(i32 %44, i32* %49, i32 4, i32 0)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %43
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -3, i32* %4
br label %61
54: ; preds = %43
br label %55
55: ; preds = %54
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %38
58: ; preds = %38
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
store i32 0, i32* %4
br label %61
61: ; preds = %58, %52, %28, %21, %15
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
@.str = [5 x i8] c"send\00"
@.str.1 = [5 x i8] c"recv\00"
define i32 @get_cores_list(i32 %0, i32* %1, i32** nocapture %2) {
%4 = alloca i32
%5 = tail call i32 @get_socket(i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %43, label %7
7: ; preds = %3
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = call i32 @send(i32 %5, i32* %4, i32 4, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %41
13: ; preds = %7
%14 = call i32 @recv(i32 %5, i32* %1, i32 4, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %41
18: ; preds = %13
%19 = load i32, i32* %1
%20 = shl i32 %19, 2
%21 = call i32* @malloc(i32 %20)
store i32* %21, i32** %2
%22 = icmp sgt i32 %19, 0
br i1 %22, label %23, label %39
23: ; preds = %18
%24 = call i32 @recv(i32 %5, i32* %21, i32 4, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %37, label %26
26: ; preds = %23, %31
%27 = phi i64 [ %36, %31 ], [ 1, %23 ]
%28 = load i32, i32* %1
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %31, label %39
31: ; preds = %26
%32 = load i32*, i32** %2
%33 = getelementptr inbounds i32, i32* %32, i64 %27
%34 = call i32 @recv(i32 %5, i32* %33, i32 4, i32 0)
%35 = icmp eq i32 %34, -1
%36 = add nuw nsw i64 %27, 1
br i1 %35, label %37, label %26
37: ; preds = %31, %23
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %41
39: ; preds = %26, %18
%40 = call i32 @close(i32 %5)
br label %41
41: ; preds = %37, %39, %16, %11
%42 = phi i32 [ -2, %11 ], [ -2, %16 ], [ -3, %37 ], [ 0, %39 ]
br label %43
43: ; preds = %3, %41
%44 = phi i32 [ %42, %41 ], [ %5, %3 ]
ret i32 %44
}