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

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
char *prepend_msg(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int /*<<< orphan*/);
void *broadcast_handler(void *socket_desc) {
  int nchars = 0;
  int client_socket = *(int *)socket_desc;
  char broadcast_msg[1024] = {0};
  while ((nchars = recv(client_socket, broadcast_msg, 1024, 0)) > 0) {
    char *msg = prepend_msg("\n", broadcast_msg);
    write(STDOUT_FILENO, msg, strlen(msg));
    free(msg);
    bzero(broadcast_msg, 1024);
  }
  if (nchars < 0)
    perror("recv has encountered an error.");
  return NULL;
}


x86 assembly (clang, -O0):
<broadcast_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    124d <broadcast_handler+0xcd>
lea    -0x410(%rbp),%rsi
lea    0xe1e(%rip),%rdi
call   1080 <prepend_msg@plt>
mov    %rax,-0x418(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    -0x418(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    -0x418(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x424(%rbp),%edi
mov    -0x420(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10a0 <bzero@plt>
jmp    11b2 <broadcast_handler+0x32>
cmpl   $0x0,-0xc(%rbp)
jge    1263 <broadcast_handler+0xe3>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<broadcast_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    (%rdi),%r15d
mov    %rsp,%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    1221 <broadcast_handler+0xa1>
lea    0xe3d(%rip),%r14
mov    %rsp,%r12
mov    0x2e23(%rip),%r13
nopl   (%rax)
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <prepend_msg@plt>
mov    %rax,%rbp
mov    0x0(%r13),%ebx
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
mov    $0x400,%esi
call   10a0 <bzero@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jg     11d0 <broadcast_handler+0x50>
test   %eax,%eax
js     1239 <broadcast_handler+0xb9>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc2(%rip),%rdi
call   1090 <perror@plt>
jmp    1225 <broadcast_handler+0xa5>

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [31 x i8] c"recv has encountered an error.\00"
define i8* @broadcast_handler(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
store i32 %9, i32* %4
%10 = bitcast [1024 x i8]* %5 to i8*
br label %11
11: ; preds = %16, %1
%12 = load i32, i32* %4
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = call i32 @recv(i32 %12, i8* %13, i32 1024, i32 0)
store i32 %14, i32* %3
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %11
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%18 = call i8* @prepend_msg(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %17)
store i8* %18, i8** %6
%19 = load i32, i32* @STDOUT_FILENO
%20 = load i8*, i8** %6
%21 = load i8*, i8** %6
%22 = call i32 @strlen(i8* %21)
%23 = call i32 @write(i32 %19, i8* %20, i32 %22)
%24 = load i8*, i8** %6
%25 = call i32 @free(i8* %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = call i32 @bzero(i8* %26, i32 1024)
br label %11
28: ; preds = %11
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
ret i8* null
}

LLVM IR (-O2):
@.str = [2 x i8] c"\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [31 x i8] c"recv has encountered an error.\00"
define noalias i8* @broadcast_handler(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = call i32 @recv(i32 %4, i8* %5, i32 1024, i32 0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %1, %8
%9 = call i8* @prepend_msg(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %5)
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @strlen(i8* %9)
%12 = call i32 @write(i32 %10, i8* %9, i32 %11)
%13 = call i32 @free(i8* %9)
%14 = call i32 @bzero(i8* %5, i32 1024)
%15 = call i32 @recv(i32 %4, i8* %5, i32 1024, i32 0)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %8, label %17
17: ; preds = %8, %1
%18 = phi i32 [ %6, %1 ], [ %15, %8 ]
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
ret i8* null
}