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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int SOCK_NONBLOCK;
int SOCK_STREAM;
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int *create_sockets(size_t n, int *phighest_fd, fd_set *ret_fd_set) {
  int *sockets = (int *)malloc(n * sizeof(int));
  if (sockets == NULL) {
    return NULL;
  }
  FD_ZERO(ret_fd_set);
  int max_fd = 0;
  for (size_t i = 0; i < n; i++) {
    int s = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
    if (s == -1) {
      perror("socket");
      free(sockets);
      return NULL;
    }
    if (s > max_fd) {
      max_fd = s;
    }
    FD_SET(s, ret_fd_set);
    sockets[i] = s;
  }
  *phighest_fd = max_fd;
  return sockets;
}


x86 assembly (clang, -O0):
<create_sockets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119d <create_sockets+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    1262 <create_sockets+0x102>
mov    -0x20(%rbp),%rdi
call   1040 <FD_ZERO@plt>
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1251 <create_sockets+0xf1>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1214 <create_sockets+0xb4>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1262 <create_sockets+0x102>
mov    -0x3c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jle    1226 <create_sockets+0xc6>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x3c(%rbp),%edx
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11b5 <create_sockets+0x55>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_sockets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r13
lea    0x0(,%rdi,4),%rdi
call   1050 <malloc@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1211 <create_sockets+0xb1>
mov    %rax,%r12
mov    %rbp,(%rsp)
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
test   %r13,%r13
je     11f5 <create_sockets+0x95>
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11fd <create_sockets+0x9d>
mov    %eax,%ebp
cmp    %ebx,%eax
cmovg  %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
call   1070 <FD_SET@plt>
mov    %ebp,(%r12,%r14,4)
add    $0x1,%r14
cmp    %r14,%r13
jne    11b0 <create_sockets+0x50>
mov    (%rsp),%rax
mov    %ebx,(%rax)
jmp    1214 <create_sockets+0xb4>
lea    0xdfc(%rip),%rdi
call   1060 <perror@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str = [7 x i8] c"socket\00"
define i32* @create_sockets(i64 %0, i32* nocapture writeonly %1, i32* %2) {
%4 = shl i64 %0, 2
%5 = tail call i64 @malloc(i64 %4)
%6 = inttoptr i64 %5 to i32*
%7 = icmp eq i64 %5, 0
br i1 %7, label %32, label %8
8: ; preds = %3
%9 = tail call i32 @FD_ZERO(i32* %2)
%10 = icmp eq i64 %0, 0
br i1 %10, label %30, label %11
11: ; preds = %8, %23
%12 = phi i64 [ %28, %23 ], [ 0, %8 ]
%13 = phi i32 [ %25, %23 ], [ 0, %8 ]
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @SOCK_NONBLOCK
%17 = or i32 %16, %15
%18 = tail call i32 @socket(i32 %14, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @free(i32* %6)
br label %32
23: ; preds = %11
%24 = icmp sgt i32 %18, %13
%25 = select i1 %24, i32 %18, i32 %13
%26 = tail call i32 @FD_SET(i32 %18, i32* %2)
%27 = getelementptr inbounds i32, i32* %6, i64 %12
store i32 %18, i32* %27
%28 = add nuw i64 %12, 1
%29 = icmp eq i64 %28, %0
br i1 %29, label %30, label %11
30: ; preds = %23, %8
%31 = phi i32 [ 0, %8 ], [ %25, %23 ]
store i32 %31, i32* %1
br label %32
32: ; preds = %20, %30, %3
%33 = phi i32* [ null, %3 ], [ %6, %30 ], [ null, %20 ]
ret i32* %33
}