#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 */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ generate_data(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int handle_connection(int fd, fd_set *master_fds) {
  int bytes;
  char buf[100];
  bzero(buf, sizeof(buf));
  if ((bytes = recv(fd, buf, sizeof(buf) - 1, 0)) > 0) {
    printf("Server(%d): received: %s\n", fd, buf);
  } else if (bytes == 0) {
    printf("Server(%d): Client disconnected\n", fd);
  } else if (bytes < 0) {
    perror("Server: recv");
  }
  generate_data(buf, sizeof(buf));
  if ((bytes = send(fd, buf, strlen(buf), 0)) == -1) {
    perror("Server: send");
  }
  FD_CLR(fd, master_fds);
  close(fd);
  printf("Server(%d): Closed connection\n", fd);
  return bytes;
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
call   10a0 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11e9 <handle_connection+0x59>
mov    -0x4(%rbp),%esi
lea    -0x80(%rbp),%rdx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1229 <handle_connection+0x99>
cmpl   $0x0,-0x14(%rbp)
jne    1209 <handle_connection+0x79>
mov    -0x4(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1224 <handle_connection+0x94>
cmpl   $0x0,-0x14(%rbp)
jge    121f <handle_connection+0x8f>
lea    0xe21(%rip),%rdi
call   1090 <perror@plt>
jmp    1224 <handle_connection+0x94>
jmp    1229 <handle_connection+0x99>
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
call   1080 <generate_data@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x8c(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1282 <handle_connection+0xf2>
lea    0xdcb(%rip),%rdi
call   1090 <perror@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10b0 <FD_CLR@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%esi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x64,%esi
call   10a0 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    11d7 <handle_connection+0x47>
lea    0xe37(%rip),%rdi
mov    %rsp,%rdx
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11e9 <handle_connection+0x59>
jne    1250 <handle_connection+0xc0>
lea    0xe3a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x64,%esi
call   1080 <generate_data@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1242 <handle_connection+0xb2>
mov    %ebx,%edi
mov    %r14,%rsi
call   10b0 <FD_CLR@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xe27(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
jmp    1216 <handle_connection+0x86>
lea    0xde4(%rip),%rdi
call   1090 <perror@plt>
jmp    11e9 <handle_connection+0x59>

LLVM IR (-O0):
@.str = [26 x i8] c"Server(%d): received: %s\0A\00"
@.str.1 = [33 x i8] c"Server(%d): Client disconnected\0A\00"
@.str.2 = [13 x i8] c"Server: recv\00"
@.str.3 = [13 x i8] c"Server: send\00"
@.str.4 = [31 x i8] c"Server(%d): Closed connection\0A\00"
define i32 @handle_connection(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [100 x i8]
store i32 %0, i32* %3
store i32* %1, i32** %4
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%8 = call i32 @bzero(i8* %7, i32 100)
%9 = load i32, i32* %3
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%11 = call i32 @recv(i32 %9, i8* %10, i32 99, i32 0)
store i32 %11, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* %3
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %14, i8* %15)
br label %30
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %3
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %29
23: ; preds = %17
%24 = load i32, i32* %5
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
br label %29
29: ; preds = %28, %20
br label %30
30: ; preds = %29, %13
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%32 = call i32 @generate_data(i8* %31, i32 100)
%33 = load i32, i32* %3
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @send(i32 %33, i8* %34, i32 %36, i32 0)
store i32 %37, i32* %5
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %30
%42 = load i32, i32* %3
%43 = load i32*, i32** %4
%44 = call i32 @FD_CLR(i32 %42, i32* %43)
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %3
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@.str = [26 x i8] c"Server(%d): received: %s\0A\00"
@.str.1 = [33 x i8] c"Server(%d): Client disconnected\0A\00"
@.str.2 = [13 x i8] c"Server: recv\00"
@.str.3 = [13 x i8] c"Server: send\00"
@.str.4 = [31 x i8] c"Server(%d): Closed connection\0A\00"
define i32 @handle_connection(i32 %0, i32* %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = call i32 @bzero(i8* %4, i32 100)
%6 = call i32 @recv(i32 %0, i8* %4, i32 99, i32 0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %4)
br label %16
10: ; preds = %2
%11 = icmp eq i32 %6, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %12, %14, %8
%17 = call i32 @generate_data(i8* %4, i32 100)
%18 = call i32 @strlen(i8* %4)
%19 = call i32 @send(i32 %0, i8* %4, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = call i32 @FD_CLR(i32 %0, i32* %1)
%25 = call i32 @close(i32 %0)
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %0)
ret i32 %19
}