#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ cs2d_bind_socket(int);
int /*<<< orphan*/ cs2d_request_ips(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
struct sockaddr_in *malloc(int);
int /*<<< orphan*/ recvfrom(int, unsigned char *, int, int /*<<< orphan*/,
                            int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char,
                           unsigned char, unsigned char);
int cs2d_get_servers(struct sockaddr_in **servers) {
  int sock_desc = socket(AF_INET, SOCK_DGRAM, 0);
  cs2d_bind_socket(sock_desc);
  cs2d_request_ips(sock_desc);
  unsigned char response[1024];
  recvfrom(sock_desc, response, 1024, 0, NULL, NULL);
  close(sock_desc);
  int ips = response[3];
  *servers = malloc(sizeof(struct sockaddr_in) * ips);
  int loc = 5;
  for (int i = 0; i < ips; i++) {
    char ip[500];
    sprintf(ip, "%d.%d.%d.%d", response[loc + 3], response[loc + 2],
            response[loc + 1], response[loc]);
    int port = response[loc + 4] | response[loc + 5] << 8;
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = inet_addr(ip);
    (*servers)[i] = server;
    loc += 6;
  }
  return ips;
}


x86 assembly (clang, -O0):
<cs2d_get_servers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <cs2d_bind_socket@plt>
mov    -0xc(%rbp),%edi
call   1090 <cs2d_request_ips@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1080 <recvfrom@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movzbl -0x40d(%rbp),%eax
mov    %eax,-0x414(%rbp)
movslq -0x414(%rbp),%rax
imul   $0xc,%rax,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x5,-0x418(%rbp)
movl   $0x0,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
cmp    -0x414(%rbp),%eax
jge    1353 <cs2d_get_servers+0x1c3>
lea    -0x610(%rbp),%rdi
mov    -0x418(%rbp),%eax
add    $0x3,%eax
cltq
mov    -0x410(%rbp,%rax,1),%dl
mov    -0x418(%rbp),%eax
add    $0x2,%eax
cltq
mov    -0x410(%rbp,%rax,1),%cl
mov    -0x418(%rbp),%eax
add    $0x1,%eax
cltq
mov    -0x410(%rbp,%rax,1),%r8b
ff
movslq -0x418(%rbp),%rax
lea    0xd74(%rip),%rsi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl -0x410(%rbp,%rax,1),%r9d
ff ff
call   10a0 <sprintf@plt>
mov    -0x418(%rbp),%eax
add    $0x4,%eax
cltq
movzbl -0x410(%rbp,%rax,1),%eax
ff
mov    -0x418(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
movzbl -0x410(%rbp,%rcx,1),%ecx
ff
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x614(%rbp)
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x618(%rbp)
mov    -0x614(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x61c(%rbp)
lea    -0x610(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x620(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x41c(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    -0x620(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x618(%rbp),%ecx
mov    %ecx,0x8(%rax)
mov    -0x418(%rbp),%eax
add    $0x6,%eax
mov    %eax,-0x418(%rbp)
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    122e <cs2d_get_servers+0x9e>
mov    -0x414(%rbp),%eax
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cs2d_get_servers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x608,%rsp
mov    %rdi,%r14
mov    0x2e3d(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <cs2d_bind_socket@plt>
mov    %ebx,%edi
call   1090 <cs2d_request_ips@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <recvfrom@plt>
mov    %ebx,%edi
call   1040 <close@plt>
movzbl 0x13(%rsp),%ebx
lea    0x0(,%rbx,4),%eax
lea    (%rax,%rax,2),%edi
call   1070 <malloc@plt>
mov    %rax,(%r14)
mov    %rbx,(%rsp)
test   %ebx,%ebx
je     128d <cs2d_get_servers+0xfd>
mov    (%rsp),%eax
add    %rax,%rax
lea    (%rax,%rax,2),%rax
mov    %rax,0x8(%rsp)
xor    %r13d,%r13d
lea    0x410(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
movzbl 0x18(%rsp,%r13,1),%edx
movzbl 0x17(%rsp,%r13,1),%ecx
movzbl 0x16(%rsp,%r13,1),%r8d
movzbl 0x15(%rsp,%r13,1),%r9d
mov    %rbx,%rdi
lea    0xdae(%rip),%rsi
call   10a0 <sprintf@plt>
movzwl 0x19(%rsp,%r13,1),%edi
mov    0x0(%rbp),%r15d
call   1030 <htons@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1050 <inet_addr@plt>
mov    (%r14),%rcx
mov    %eax,(%rcx,%r13,2)
mov    %r12d,0x4(%rcx,%r13,2)
mov    %r15d,0x8(%rcx,%r13,2)
add    $0x6,%r13
cmp    %r13,0x8(%rsp)
jne    1230 <cs2d_get_servers+0xa0>
mov    (%rsp),%rax
add    $0x608,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [12 x i8] c"%d.%d.%d.%d\00"
define i32 @cs2d_get_servers(%struct.sockaddr_in** %0) {
%2 = alloca %struct.sockaddr_in**
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [500 x i8]
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store %struct.sockaddr_in** %0, %struct.sockaddr_in*** %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = call i32 @cs2d_bind_socket(i32 %14)
%16 = load i32, i32* %3
%17 = call i32 @cs2d_request_ips(i32 %16)
%18 = load i32, i32* %3
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i32 @recvfrom(i32 %18, i8* %19, i32 1024, i32 0, i32* null, i32* null)
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 3
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = mul i64 12, %27
%29 = trunc i64 %28 to i32
%30 = call %struct.sockaddr_in* @malloc(i32 %29)
%31 = load %struct.sockaddr_in**, %struct.sockaddr_in*** %2
store %struct.sockaddr_in* %30, %struct.sockaddr_in** %31
store i32 5, i32* %6
store i32 0, i32* %7
br label %32
32: ; preds = %90, %1
%33 = load i32, i32* %7
%34 = load i32, i32* %5
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %93
36: ; preds = %32
%37 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 3
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 2
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8 zeroext %42, i8 zeroext %47, i8 zeroext %52, i8 zeroext %56)
%58 = load i32, i32* %6
%59 = add nsw i32 %58, 4
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %60
%62 = load i8, i8* %61
%63 = zext i8 %62 to i32
%64 = load i32, i32* %6
%65 = add nsw i32 %64, 5
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %66
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = shl i32 %69, 8
%71 = or i32 %63, %70
store i32 %71, i32* %9
%72 = load i32, i32* @AF_INET
%73 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %72, i32* %73
%74 = load i32, i32* %9
%75 = call i32 @htons(i32 %74)
%76 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %75, i32* %76
%77 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%78 = call i32 @inet_addr(i8* %77)
%79 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%80 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %79, i32 0, i32 0
store i32 %78, i32* %80
%81 = load %struct.sockaddr_in**, %struct.sockaddr_in*** %2
%82 = load %struct.sockaddr_in*, %struct.sockaddr_in** %81
%83 = load i32, i32* %7
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %82, i64 %84
%86 = bitcast %struct.sockaddr_in* %85 to i8*
%87 = bitcast %struct.sockaddr_in* %10 to i8*
%88 = load i32, i32* %6
%89 = add nsw i32 %88, 6
store i32 %89, i32* %6
br label %90
90: ; preds = %36
%91 = load i32, i32* %7
%92 = add nsw i32 %91, 1
store i32 %92, i32* %7
br label %32
93: ; preds = %32
%94 = load i32, i32* %5
ret i32 %94
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [12 x i8] c"%d.%d.%d.%d\00"
define i32 @cs2d_get_servers(%struct.sockaddr_in** nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca [500 x i8]
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = tail call i32 @cs2d_bind_socket(i32 %6)
%8 = tail call i32 @cs2d_request_ips(i32 %6)
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%10 = call i32 @recvfrom(i32 %6, i8* %9, i32 1024, i32 0, i32* null, i32* null)
%11 = call i32 @close(i32 %6)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 3
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = mul nuw nsw i32 %14, 12
%16 = call %struct.sockaddr_in* @malloc(i32 %15)
store %struct.sockaddr_in* %16, %struct.sockaddr_in** %0
%17 = icmp eq i8 %13, 0
br i1 %17, label %21, label %18
18: ; preds = %1
%19 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%20 = zext i8 %13 to i64
br label %22
21: ; preds = %22, %1
ret i32 %14
22: ; preds = %18, %22
%23 = phi i64 [ 0, %18 ], [ %55, %22 ]
%24 = phi i64 [ 5, %18 ], [ %54, %22 ]
%25 = add nuw nsw i64 %24, 3
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = add nuw nsw i64 %24, 2
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = add nuw nsw i64 %24, 1
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %24
%35 = load i8, i8* %34
%36 = call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8 zeroext %27, i8 zeroext %30, i8 zeroext %33, i8 zeroext %35)
%37 = add nuw nsw i64 %24, 4
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = add nuw nsw i64 %24, 5
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = shl nuw nsw i32 %44, 8
%46 = or i32 %45, %40
%47 = load i32, i32* @AF_INET
%48 = call i32 @htons(i32 %46)
%49 = call i32 @inet_addr(i8* %19)
%50 = load %struct.sockaddr_in*, %struct.sockaddr_in** %0
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i64 %23, i32 0, i32 0
store i32 %49, i32* %51
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i64 %23, i32 1
store i32 %48, i32* %52
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i64 %23, i32 2
store i32 %47, i32* %53
%54 = add nuw nsw i64 %24, 6
%55 = add nuw nsw i64 %23, 1
%56 = icmp eq i64 %55, %20
br i1 %56, label %21, label %22
}