#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*/ int8_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
scalar_t__ recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ search_empty_and_set_socket_number(int, int /*<<< orphan*/);
scalar_t__ send_broadcast(int, int /*<<< orphan*/ *);
void *socket_thread(void *arg) {
  int32_t new_socket = *((int32_t *)arg);
  int32_t keepalive = 1;
  int8_t client_message[1024];
  while (1) {
    memset(client_message, '\0', 1024);
    if (recv(new_socket, client_message, 1024, MSG_NOSIGNAL) <= 0) {
      close(new_socket);
      search_empty_and_set_socket_number(new_socket, 0);
      pthread_exit(NULL);
      break;
    }
    if (send_broadcast(new_socket, client_message)) {
      close(new_socket);
      search_empty_and_set_socket_number(new_socket, 0);
      pthread_exit(NULL);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<socket_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    0x2e3d(%rip),%rax
mov    (%rax),%ecx
mov    $0x400,%edx
call   1030 <recv@plt>
cmp    $0x0,%rax
jg     11d9 <socket_thread+0x79>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1070 <search_empty_and_set_socket_number@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <pthread_exit@plt>
jmp    1217 <socket_thread+0xb7>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
call   1080 <send_broadcast@plt>
cmp    $0x0,%rax
je     1212 <socket_thread+0xb2>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1070 <search_empty_and_set_socket_number@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <pthread_exit@plt>
jmp    1217 <socket_thread+0xb7>
jmp    117f <socket_thread+0x1f>
mov    -0x8(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_thread>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    (%rdi),%ebx
mov    %rsp,%r14
mov    0x2e68(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    (%r15),%ecx
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <recv@plt>
test   %rax,%rax
jle    11b5 <socket_thread+0x55>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <send_broadcast@plt>
test   %rax,%rax
je     1180 <socket_thread+0x20>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <search_empty_and_set_socket_number@plt>
xor    %edi,%edi
call   1060 <pthread_exit@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
define i8* @socket_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i32]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
store i32 %9, i32* %4
store i32 1, i32* %5
br label %10
10: ; preds = %1, %35
%11 = getelementptr inbounds [1024 x i32], [1024 x i32]* %6, i64 0, i64 0
%12 = call i32 @memset(i32* %11, i8 signext 0, i32 1024)
%13 = load i32, i32* %4
%14 = getelementptr inbounds [1024 x i32], [1024 x i32]* %6, i64 0, i64 0
%15 = load i32, i32* @MSG_NOSIGNAL
%16 = call i64 @recv(i32 %13, i32* %14, i32 1024, i32 %15)
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %10
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %4
%22 = call i32 @search_empty_and_set_socket_number(i32 %21, i32 0)
%23 = call i32 @pthread_exit(i32* null)
br label %36
24: ; preds = %10
%25 = load i32, i32* %4
%26 = getelementptr inbounds [1024 x i32], [1024 x i32]* %6, i64 0, i64 0
%27 = call i64 @send_broadcast(i32 %25, i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %4
%33 = call i32 @search_empty_and_set_socket_number(i32 %32, i32 0)
%34 = call i32 @pthread_exit(i32* null)
br label %36
35: ; preds = %24
br label %10
36: ; preds = %29, %18
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
@MSG_NOSIGNAL = global i32 0
define noalias i8* @socket_thread(i8* nocapture %0) {
%2 = alloca [1024 x i32]
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = bitcast [1024 x i32]* %2 to i8*
%6 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 0
br label %7
7: ; preds = %12, %1
%8 = call i32 @memset(i32* %6, i8 signext 0, i32 1024)
%9 = load i32, i32* @MSG_NOSIGNAL
%10 = call i64 @recv(i32 %4, i32* %6, i32 1024, i32 %9)
%11 = icmp slt i64 %10, 1
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = call i64 @send_broadcast(i32 %4, i32* %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %7, label %15
15: ; preds = %12, %7
%16 = call i32 @close(i32 %4)
%17 = call i32 @search_empty_and_set_socket_number(i32 %4, i32 0)
%18 = call i32 @pthread_exit(i32* null)
ret i8* undef
}