#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 */
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int const);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int const, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void *connection_handler(void *socket_desc) {
  int sock = *(int *)socket_desc;
  char *message = "Greetings!\n";
  const int buf_len = 10000;
  char read_buf[buf_len];
  int read_size;
  write(sock, message, strlen(message));
  int did_find_end = 0;
  while (!did_find_end && (read_size = recv(sock, read_buf, buf_len, 0)) > 0) {
    read_buf[read_size] = '\0';
    write(sock, read_buf, strlen(read_buf));
    for (int i = 0; i < read_size; i++) {
      char eof = '\0';
      char end_of_transmission = '\4';
      if (read_buf[i] == eof || read_buf[i] == end_of_transmission) {
        did_find_end = 1;
      }
    }
    memset(read_buf, 0, buf_len);
  }
  if (0 == read_size) {
    fprintf(stderr, "Client disconnected\n");
  } else if (-1 == read_size) {
    perror("recv failed");
  }
  if (0 > close(sock)) {
    perror("Error shutting down socket");
  }
  fprintf(stderr, "Connection closed\n");
  return NULL;
}


x86 assembly (clang, -O0):
<connection_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2770,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xe71(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x2710,-0x1c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x275c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x2758(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x275c(%rbp),%edi
mov    -0x2758(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
movl   $0x0,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x30(%rbp)
mov    %al,-0x275d(%rbp)
jne    1210 <connection_handler+0xa0>
mov    -0xc(%rbp),%edi
lea    -0x2750(%rbp),%rsi
mov    $0x2710,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x275d(%rbp)
mov    -0x275d(%rbp),%al
test   $0x1,%al
jne    1223 <connection_handler+0xb3>
jmp    12d5 <connection_handler+0x165>
movslq -0x2c(%rbp),%rax
movb   $0x0,-0x2750(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2764(%rbp)
lea    -0x2750(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2764(%rbp),%edi
mov    %eax,%edx
lea    -0x2750(%rbp),%rsi
call   1040 <write@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12bd <connection_handler+0x14d>
movb   $0x0,-0x35(%rbp)
movb   $0x4,-0x36(%rbp)
movslq -0x34(%rbp),%rax
movsbl -0x2750(%rbp,%rax,1),%eax
ff
movsbl -0x35(%rbp),%ecx
cmp    %ecx,%eax
je     12a3 <connection_handler+0x133>
movslq -0x34(%rbp),%rax
movsbl -0x2750(%rbp,%rax,1),%eax
ff
movsbl -0x36(%rbp),%ecx
cmp    %ecx,%eax
jne    12aa <connection_handler+0x13a>
movl   $0x1,-0x30(%rbp)
jmp    12af <connection_handler+0x13f>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    125f <connection_handler+0xef>
lea    -0x2750(%rbp),%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1060 <memset@plt>
jmp    11d9 <connection_handler+0x69>
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
jne    12fa <connection_handler+0x18a>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd1c(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1319 <connection_handler+0x1a9>
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    1314 <connection_handler+0x1a4>
lea    0xd12(%rip),%rdi
call   1090 <perror@plt>
jmp    1319 <connection_handler+0x1a9>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    133b <connection_handler+0x1cb>
lea    0xcf7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
lea    0xcfd(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
add    $0x2770,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connection_handler>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2710,%rsp
mov    (%rdi),%r14d
lea    0xe77(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rsp,%r15
jmp    11c7 <connection_handler+0x57>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1060 <memset@plt>
test   %ebx,%ebx
jne    128a <connection_handler+0x11a>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x2710,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    128a <connection_handler+0x11a>
mov    %r12d,%ebp
movb   $0x0,(%rsp,%rbp,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    -0x1(%rbp),%rcx
mov    %ebp,%eax
and    $0x3,%eax
cmp    $0x3,%rcx
jae    1240 <connection_handler+0xd0>
xor    %ecx,%ecx
xor    %ebx,%ebx
test   %rax,%rax
je     11b0 <connection_handler+0x40>
add    %rsp,%rcx
xor    %edx,%edx
jmp    1229 <connection_handler+0xb9>
nop
add    $0x1,%rdx
cmp    %rdx,%rax
je     11b0 <connection_handler+0x40>
testb  $0xfb,(%rcx,%rdx,1)
jne    1220 <connection_handler+0xb0>
mov    $0x1,%ebx
jmp    1220 <connection_handler+0xb0>
cs nopw 0x0(%rax,%rax,1)
and    $0xfffffffc,%ebp
xor    %ecx,%ecx
xor    %ebx,%ebx
jmp    1259 <connection_handler+0xe9>
nopl   0x0(%rax)
add    $0x4,%rcx
cmp    %rcx,%rbp
je     1213 <connection_handler+0xa3>
testb  $0xfb,(%rsp,%rcx,1)
jne    1264 <connection_handler+0xf4>
mov    $0x1,%ebx
testb  $0xfb,0x1(%rsp,%rcx,1)
jne    1270 <connection_handler+0x100>
mov    $0x1,%ebx
testb  $0xfb,0x2(%rsp,%rcx,1)
jne    127c <connection_handler+0x10c>
mov    $0x1,%ebx
testb  $0xfb,0x3(%rsp,%rcx,1)
jne    1250 <connection_handler+0xe0>
mov    $0x1,%ebx
jmp    1250 <connection_handler+0xe0>
test   %r12d,%r12d
je     12a3 <connection_handler+0x133>
cmp    $0xffffffff,%r12d
jne    12b8 <connection_handler+0x148>
lea    0xd85(%rip),%rdi
call   1090 <perror@plt>
jmp    12b8 <connection_handler+0x148>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
lea    0xd59(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
test   %rax,%rax
js     12ec <connection_handler+0x17c>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
call   1080 <fprintf@plt>
xor    %eax,%eax
add    $0x2710,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3a(%rip),%rdi
call   1090 <perror@plt>
jmp    12c5 <connection_handler+0x155>

LLVM IR (-O0):
@.str = [12 x i8] c"Greetings!\0A\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Client disconnected\0A\00"
@.str.2 = [12 x i8] c"recv failed\00"
@.str.3 = [27 x i8] c"Error shutting down socket\00"
@.str.4 = [19 x i8] c"Connection closed\0A\00"
define i8* @connection_handler(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i8
store i8* %0, i8** %2
%12 = load i8*, i8** %2
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
store i32 %14, i32* %3
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 10000, i32* %5
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 10000
%17 = load i32, i32* %3
%18 = load i8*, i8** %4
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = call i32 @write(i32 %17, i8* %18, i32 %20)
store i32 0, i32* %8
br label %22
22: ; preds = %65, %1
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = load i32, i32* %3
%27 = call i32 @recv(i32 %26, i8* %16, i32 10000, i32 0)
store i32 %27, i32* %7
%28 = icmp sgt i32 %27, 0
br label %29
29: ; preds = %25, %22
%30 = phi i1 [ false, %22 ], [ %28, %25 ]
br i1 %30, label %31, label %67
31: ; preds = %29
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %16, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %3
%36 = call i32 @strlen(i8* %16)
%37 = call i32 @write(i32 %35, i8* %16, i32 %36)
store i32 0, i32* %9
br label %38
38: ; preds = %62, %31
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %65
42: ; preds = %38
store i8 0, i8* %10
store i8 4, i8* %11
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %16, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = load i8, i8* %10
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %47, %49
br i1 %50, label %60, label %51
51: ; preds = %42
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %16, i64 %53
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = load i8, i8* %11
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %56, %58
br i1 %59, label %60, label %61
60: ; preds = %51, %42
store i32 1, i32* %8
br label %61
61: ; preds = %60, %51
br label %62
62: ; preds = %61
%63 = load i32, i32* %9
%64 = add nsw i32 %63, 1
store i32 %64, i32* %9
br label %38
65: ; preds = %38
%66 = call i32 @memset(i8* %16, i32 0, i32 10000)
br label %22
67: ; preds = %29
%68 = load i32, i32* %7
%69 = icmp eq i32 0, %68
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %79
73: ; preds = %67
%74 = load i32, i32* %7
%75 = icmp eq i32 -1, %74
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %78
78: ; preds = %76, %73
br label %79
79: ; preds = %78, %70
%80 = load i32, i32* %3
%81 = call i64 @close(i32 %80)
%82 = icmp sgt i64 0, %81
br i1 %82, label %83, label %85
83: ; preds = %79
%84 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %85
85: ; preds = %83, %79
%86 = load i32, i32* @stderr
%87 = call i32 @fprintf(i32 %86, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%88 = load i8*, i8** %6
ret i8* null
}

LLVM IR (-O2):
@.str = [12 x i8] c"Greetings!\0A\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Client disconnected\0A\00"
@.str.2 = [12 x i8] c"recv failed\00"
@.str.3 = [27 x i8] c"Error shutting down socket\00"
@.str.4 = [19 x i8] c"Connection closed\0A\00"
define noalias i8* @connection_handler(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = alloca [10000 x i8]
%5 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %8
8: ; preds = %1, %38
%9 = call i32 @recv(i32 %3, i8* %5, i32 10000, i32 0)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %72
11: ; preds = %8
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @strlen(i8* %5)
%15 = call i32 @write(i32 %3, i8* %5, i32 %14)
%16 = add nsw i64 %12, -1
%17 = and i64 %12, 3
%18 = icmp ult i64 %16, 3
br i1 %18, label %21, label %19
19: ; preds = %11
%20 = and i64 %12, 4294967292
br label %42
21: ; preds = %67, %11
%22 = phi i32 [ undef, %11 ], [ %68, %67 ]
%23 = phi i64 [ 0, %11 ], [ %69, %67 ]
%24 = phi i32 [ 0, %11 ], [ %68, %67 ]
%25 = icmp eq i64 %17, 0
br i1 %25, label %38, label %26
26: ; preds = %21, %33
%27 = phi i64 [ %35, %33 ], [ %23, %21 ]
%28 = phi i32 [ %34, %33 ], [ %24, %21 ]
%29 = phi i64 [ %36, %33 ], [ 0, %21 ]
%30 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 %27
%31 = load i8, i8* %30
switch i8 %31, label %33 [
i8 0, label %32
i8 4, label %32
]
32: ; preds = %26, %26
br label %33
33: ; preds = %32, %26
%34 = phi i32 [ 1, %32 ], [ %28, %26 ]
%35 = add nuw nsw i64 %27, 1
%36 = add i64 %29, 1
%37 = icmp eq i64 %36, %17
br i1 %37, label %38, label %26
38: ; preds = %33, %21
%39 = phi i32 [ %22, %21 ], [ %34, %33 ]
%40 = call i32 @memset(i8* %5, i32 0, i32 10000)
%41 = icmp eq i32 %39, 0
br i1 %41, label %8, label %72
42: ; preds = %67, %19
%43 = phi i64 [ 0, %19 ], [ %69, %67 ]
%44 = phi i32 [ 0, %19 ], [ %68, %67 ]
%45 = phi i64 [ 0, %19 ], [ %70, %67 ]
%46 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 %43
%47 = load i8, i8* %46
switch i8 %47, label %49 [
i8 0, label %48
i8 4, label %48
]
48: ; preds = %42, %42
br label %49
49: ; preds = %42, %48
%50 = phi i32 [ 1, %48 ], [ %44, %42 ]
%51 = or i64 %43, 1
%52 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 %51
%53 = load i8, i8* %52
switch i8 %53, label %55 [
i8 0, label %54
i8 4, label %54
]
54: ; preds = %49, %49
br label %55
55: ; preds = %54, %49
%56 = phi i32 [ 1, %54 ], [ %50, %49 ]
%57 = or i64 %43, 2
%58 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 %57
%59 = load i8, i8* %58
switch i8 %59, label %61 [
i8 0, label %60
i8 4, label %60
]
60: ; preds = %55, %55
br label %61
61: ; preds = %60, %55
%62 = phi i32 [ 1, %60 ], [ %56, %55 ]
%63 = or i64 %43, 3
%64 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 %63
%65 = load i8, i8* %64
switch i8 %65, label %67 [
i8 0, label %66
i8 4, label %66
]
66: ; preds = %61, %61
br label %67
67: ; preds = %66, %61
%68 = phi i32 [ 1, %66 ], [ %62, %61 ]
%69 = add nuw nsw i64 %43, 4
%70 = add i64 %45, 4
%71 = icmp eq i64 %70, %20
br i1 %71, label %21, label %42
72: ; preds = %38, %8
switch i32 %9, label %78 [
i32 0, label %73
i32 -1, label %76
]
73: ; preds = %72
%74 = load i32, i32* @stderr
%75 = call i32 @fprintf(i32 %74, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %78
76: ; preds = %72
%77 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %78
78: ; preds = %72, %76, %73
%79 = call i64 @close(i32 %3)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %78
%82 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %83
83: ; preds = %81, %78
%84 = load i32, i32* @stderr
%85 = call i32 @fprintf(i32 %84, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
ret i8* null
}