#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*/ timeout;
struct timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ cs2d_bind_socket(int);
void *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct timeval *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int cs2d_get_players(struct sockaddr_in server, char ***players) {
  int sock_desc = socket(AF_INET, SOCK_DGRAM, 0);
  cs2d_bind_socket(sock_desc);
  struct timeval timeout;
  timeout.tv_sec = 0;
  timeout.tv_usec = 500 * 1000;
  setsockopt(sock_desc, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
  setsockopt(sock_desc, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  char datarequest[] = {0x01, 0x00, 0xfb, 0x05};
  sendto(sock_desc, datarequest, 4, 0, (struct sockaddr *)&server,
         sizeof(server));
  char response[4096];
  int length = recvfrom(sock_desc, response, 4095, 0, NULL, NULL);
  close(sock_desc);
  if (length > 0) {
    int player_count = response[4];
    *players = malloc(sizeof(void *) * player_count);
    int pos = 5;
    for (int i = 0; i < player_count; i++) {
      pos++;
      int name_len = response[pos];
      pos++;
      char *name = malloc(256);
      memcpy(name, &response[pos], 255);
      name[name_len] = '\0';
      pos += name_len;
      (*players)[i] = name;
      pos += 9;
    }
    return player_count;
  } else {
    *players = NULL;
    return 0;
  }
}


x86 assembly (clang, -O0):
<cs2d_get_players>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <cs2d_bind_socket@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x7a120,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    0xded(%rip),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
lea    -0x8(%rbp),%r8
mov    $0x4,%r9d
xor    %ecx,%ecx
mov    %r9d,%edx
call   1040 <sendto@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0xfff,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1090 <recvfrom@plt>
mov    %eax,-0x1034(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x1034(%rbp)
jle    138d <cs2d_get_players+0x20d>
movsbl -0x102c(%rbp),%eax
mov    %eax,-0x1038(%rbp)
movslq -0x1038(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x5,-0x103c(%rbp)
movl   $0x0,-0x1040(%rbp)
mov    -0x1040(%rbp),%eax
cmp    -0x1038(%rbp),%eax
jge    137f <cs2d_get_players+0x1ff>
mov    -0x103c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x103c(%rbp)
movslq -0x103c(%rbp),%rax
movsbl -0x1030(%rbp,%rax,1),%eax
ff
mov    %eax,-0x1044(%rbp)
mov    -0x103c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x103c(%rbp)
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,-0x1050(%rbp)
mov    -0x1050(%rbp),%rdi
movslq -0x103c(%rbp),%rax
lea    -0x1030(%rbp),%rsi
add    %rax,%rsi
mov    $0xff,%edx
call   1060 <memcpy@plt>
mov    -0x1050(%rbp),%rax
movslq -0x1044(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1044(%rbp),%eax
add    -0x103c(%rbp),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x1050(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x1040(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x103c(%rbp),%eax
add    $0x9,%eax
mov    %eax,-0x103c(%rbp)
mov    -0x1040(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1040(%rbp)
jmp    12a7 <cs2d_get_players+0x127>
mov    -0x1038(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139f <cs2d_get_players+0x21f>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cs2d_get_players>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    %rsi,%r13
mov    %edi,0x8(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <cs2d_bind_socket@plt>
movq   $0x0,0x28(%rsp)
movl   $0x7a120,0x20(%rsp)
mov    0x2e0c(%rip),%r14
mov    (%r14),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    (%r14),%esi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
movl   $0x5fb0001,0xc(%rsp)
05
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1040 <sendto@plt>
lea    0x30(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xfff,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <recvfrom@plt>
mov    %eax,%r12d
mov    %ebp,%edi
call   1050 <close@plt>
test   %r12d,%r12d
jle    12f2 <cs2d_get_players+0x172>
movsbl 0x34(%rsp),%ebx
lea    0x0(,%rbx,8),%edi
call   1080 <malloc@plt>
mov    %rax,0x0(%r13)
test   %ebx,%ebx
jle    12fa <cs2d_get_players+0x17a>
mov    %rbx,0x10(%rsp)
mov    %ebx,%eax
mov    %rax,0x18(%rsp)
mov    $0x5,%eax
xor    %ebx,%ebx
mov    %r14,%r12
cs nopw 0x0(%rax,%rax,1)
nop
movslq %eax,%r14
movsbq 0x1(%r14,%r12,1),%r15
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
lea    (%r14,%r12,1),%rsi
add    $0x2,%rsi
mov    %rax,%rdi
mov    $0xff,%edx
call   1060 <memcpy@plt>
movb   $0x0,0x0(%rbp,%r15,1)
mov    0x0(%r13),%rax
mov    %rbp,(%rax,%rbx,8)
lea    (%r14,%r15,1),%eax
add    $0xb,%eax
add    $0x1,%rbx
cmp    %rbx,0x18(%rsp)
jne    12a0 <cs2d_get_players+0x120>
mov    0x10(%rsp),%rbx
jmp    12fa <cs2d_get_players+0x17a>
movq   $0x0,0x0(%r13)
mov    %ebx,%eax
add    $0x1038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @cs2d_get_players(i32 %0, i8*** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %0, i32* %7
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = tail call i32 @cs2d_bind_socket(i32 %10)
%12 = bitcast %struct.timeval* %4 to i8*
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 500000, i32* %14
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_SNDTIMEO
%17 = call i32 @setsockopt(i32 %10, i32 %15, i32 %16, %struct.timeval* %4, i32 16)
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_RCVTIMEO
%20 = call i32 @setsockopt(i32 %10, i32 %18, i32 %19, %struct.timeval* %4, i32 16)
%21 = bitcast i32* %5 to i8*
store i32 100335617, i32* %5
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i32 @sendto(i32 %10, i8* %21, i32 4, i32 0, %struct.sockaddr* %22, i32 4)
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%25 = call i32 @recvfrom(i32 %10, i8* %24, i32 4095, i32 0, i32* null, i32* null)
%26 = call i32 @close(i32 %10)
%27 = icmp sgt i32 %25, 0
br i1 %27, label %28, label %59
28: ; preds = %2
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 4
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = shl nsw i32 %31, 3
%33 = call i8* @malloc(i32 %32)
%34 = bitcast i8*** %1 to i8**
store i8* %33, i8** %34
%35 = icmp sgt i8 %30, 0
br i1 %35, label %36, label %60
36: ; preds = %28
%37 = zext i32 %31 to i64
br label %38
38: ; preds = %36, %38
%39 = phi i64 [ 0, %36 ], [ %57, %38 ]
%40 = phi i32 [ 5, %36 ], [ %56, %38 ]
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = add nsw i32 %40, 2
%47 = call dereferenceable_or_null(256) i8* @malloc(i32 256)
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %48
%50 = call i32 @memcpy(i8* %47, i8* %49, i32 255)
%51 = sext i8 %44 to i64
%52 = getelementptr inbounds i8, i8* %47, i64 %51
store i8 0, i8* %52
%53 = load i8**, i8*** %1
%54 = getelementptr inbounds i8*, i8** %53, i64 %39
store i8* %47, i8** %54
%55 = add i32 %40, 11
%56 = add i32 %55, %45
%57 = add nuw nsw i64 %39, 1
%58 = icmp eq i64 %57, %37
br i1 %58, label %60, label %38
59: ; preds = %2
store i8** null, i8*** %1
br label %60
60: ; preds = %38, %28, %59
%61 = phi i32 [ 0, %59 ], [ %31, %28 ], [ %31, %38 ]
ret i32 %61
}