#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int send(int, char **, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
int handle_writefd(char *message, int sender_socket, int srv_socket, int max_fd,
                   fd_set *active_fd_set) {
  char buf[1024];
  memset(buf, 0, sizeof(buf));
  int i, buf_written, buf_available = 0;
  for (i = 0; i <= max_fd; i++) {
    if (i == srv_socket) {
      continue;
    }
    if (FD_ISSET(i, active_fd_set)) {
      sprintf(buf, "[Remote socket %d says]: %s", sender_socket, message);
      buf_written = send(i, &buf, sizeof(buf), 0);
      if (buf_written < 0) {
        perror("send() failed");
        return -1;
      }
      buf_available = 1024 - buf_written;
      printf("(Socket %d): Written %d bytes, buffer available %d bytes.\n", i,
             buf_written, buf_available);
    }
  }
  return buf_written;
}


x86 assembly (clang, -O0):
<handle_writefd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
movl   $0x0,-0x43c(%rbp)
movl   $0x0,-0x434(%rbp)
mov    -0x434(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1285 <handle_writefd+0x125>
mov    -0x434(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    11c6 <handle_writefd+0x66>
jmp    1271 <handle_writefd+0x111>
mov    -0x434(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     126c <handle_writefd+0x10c>
lea    -0x430(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe0c(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x434(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x438(%rbp)
cmpl   $0x0,-0x438(%rbp)
jge    123d <handle_writefd+0xdd>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <handle_writefd+0x12e>
mov    $0x400,%eax
sub    -0x438(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x434(%rbp),%esi
mov    -0x438(%rbp),%edx
mov    -0x43c(%rbp),%ecx
lea    0xdc3(%rip),%rdi
call   1050 <printf@plt>
jmp    1271 <handle_writefd+0x111>
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    11a3 <handle_writefd+0x43>
mov    -0x438(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_writefd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %r8,%r14
mov    %ecx,%ebp
mov    %edx,%r13d
mov    %esi,0x4(%rsp)
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
test   %ebp,%ebp
js     1214 <handle_writefd+0xb4>
add    $0x1,%ebp
lea    0x10(%rsp),%r15
jmp    11b7 <handle_writefd+0x57>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebx
cmp    %ebx,%ebp
je     1214 <handle_writefd+0xb4>
cmp    %ebx,%r13d
je     11b0 <handle_writefd+0x50>
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11b0 <handle_writefd+0x50>
mov    %r15,%rdi
lea    0xe2b(%rip),%rsi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%rcx
call   1080 <sprintf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
test   %eax,%eax
js     1229 <handle_writefd+0xc9>
mov    %eax,%r12d
mov    $0x400,%ecx
sub    %eax,%ecx
lea    0xe21(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <printf@plt>
jmp    11b0 <handle_writefd+0x50>
mov    %r12d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r12d
jmp    1214 <handle_writefd+0xb4>

LLVM IR (-O0):
@.str = [28 x i8] c"[Remote socket %d says]: %s\00"
@.str.1 = [14 x i8] c"send() failed\00"
@.str.2 = [59 x i8] c"(Socket %d): Written %d bytes, buffer available %d bytes.\0A\00"
define i32 @handle_writefd(i8* %0, i32 %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca [1024 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%17 = call i32 @memset(i8* %16, i32 0, i32 1024)
store i32 0, i32* %15
store i32 0, i32* %13
br label %18
18: ; preds = %52, %5
%19 = load i32, i32* %13
%20 = load i32, i32* %10
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %55
22: ; preds = %18
%23 = load i32, i32* %13
%24 = load i32, i32* %9
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %52
27: ; preds = %22
%28 = load i32, i32* %13
%29 = load i32*, i32** %11
%30 = call i64 @FD_ISSET(i32 %28, i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %51
32: ; preds = %27
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%34 = load i32, i32* %8
%35 = load i8*, i8** %7
%36 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %34, i8* %35)
%37 = load i32, i32* %13
%38 = bitcast [1024 x i8]* %12 to i8**
%39 = call i32 @send(i32 %37, i8** %38, i32 1024, i32 0)
store i32 %39, i32* %14
%40 = load i32, i32* %14
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %32
%43 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %57
44: ; preds = %32
%45 = load i32, i32* %14
%46 = sub nsw i32 1024, %45
store i32 %46, i32* %15
%47 = load i32, i32* %13
%48 = load i32, i32* %14
%49 = load i32, i32* %15
%50 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %47, i32 %48, i32 %49)
br label %51
51: ; preds = %44, %27
br label %52
52: ; preds = %51, %26
%53 = load i32, i32* %13
%54 = add nsw i32 %53, 1
store i32 %54, i32* %13
br label %18
55: ; preds = %18
%56 = load i32, i32* %14
store i32 %56, i32* %6
br label %57
57: ; preds = %55, %42
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
@.str = [28 x i8] c"[Remote socket %d says]: %s\00"
@.str.1 = [14 x i8] c"send() failed\00"
@.str.2 = [59 x i8] c"(Socket %d): Written %d bytes, buffer available %d bytes.\0A\00"
define i32 @handle_writefd(i8* %0, i32 %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 1024)
%9 = icmp slt i32 %3, 0
br i1 %9, label %32, label %10
10: ; preds = %5
%11 = bitcast [1024 x i8]* %6 to i8**
br label %12
12: ; preds = %10, %28
%13 = phi i32 [ undef, %10 ], [ %29, %28 ]
%14 = phi i32 [ 0, %10 ], [ %30, %28 ]
%15 = icmp eq i32 %14, %2
br i1 %15, label %28, label %16
16: ; preds = %12
%17 = call i64 @FD_ISSET(i32 %14, i32* %4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %16
%20 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %1, i8* %0)
%21 = call i32 @send(i32 %14, i8** %11, i32 1024, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %32
25: ; preds = %19
%26 = sub nsw i32 1024, %21
%27 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %21, i32 %26)
br label %28
28: ; preds = %16, %25, %12
%29 = phi i32 [ %13, %12 ], [ %21, %25 ], [ %13, %16 ]
%30 = add nuw i32 %14, 1
%31 = icmp eq i32 %14, %3
br i1 %31, label %32, label %12
32: ; preds = %28, %5, %23
%33 = phi i32 [ -1, %23 ], [ undef, %5 ], [ %29, %28 ]
ret i32 %33
}