#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*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
void serve(int sk) {
  char buf[512];
  int len;
  while (1) {
    len = recv(sk, buf, 512, 0);
    if (len < 0) {
      perror("recv");
      break;
    } else if (len == 0) {
      printf("Remote host has closed the connection\n");
      break;
    }
    buf[len] = '\0';
    if (strcmp(buf, "/q") == 0)
      break;
    if (send(sk, buf, strlen(buf), 0) < 0) {
      perror("send");
      break;
    }
  }
  printf("Done serving\n");
  close(sk);
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jge    11b8 <serve+0x48>
lea    0xe52(%rip),%rdi
call   1090 <perror@plt>
jmp    1265 <serve+0xf5>
cmpl   $0x0,-0x214(%rbp)
jne    11d6 <serve+0x66>
lea    0xe39(%rip),%rdi
call   1060 <printf@plt>
jmp    1265 <serve+0xf5>
jmp    11db <serve+0x6b>
movslq -0x214(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
lea    -0x210(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    120c <serve+0x9c>
jmp    1265 <serve+0xf5>
mov    -0x4(%rbp),%eax
mov    %eax,-0x224(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x220(%rbp)
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x224(%rbp),%edi
mov    -0x220(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0x0,%rax
jge    1260 <serve+0xf0>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
jmp    1265 <serve+0xf5>
jmp    117e <serve+0xe>
lea    0xdc8(%rip),%rdi
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
lea    0xe7e(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     120c <serve+0x9c>
je     11e1 <serve+0x71>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11ed <serve+0x7d>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %rax,%rax
jns    1190 <serve+0x20>
lea    0xe29(%rip),%rdi
jmp    1213 <serve+0xa3>
lea    0xe32(%rip),%rdi
call   1040 <puts@plt>
lea    0xe19(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
jmp    11ed <serve+0x7d>

LLVM IR (-O0):
@.str = [5 x i8] c"recv\00"
@.str.1 = [39 x i8] c"Remote host has closed the connection\0A\00"
@.str.2 = [3 x i8] c"/q\00"
@.str.3 = [5 x i8] c"send\00"
@.str.4 = [14 x i8] c"Done serving\0A\00"
define void @serve(i32 %0) {
%2 = alloca i32
%3 = alloca [512 x i8]
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %1, %36
%6 = load i32, i32* %2
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%8 = call i32 @recv(i32 %6, i8* %7, i32 512, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %37
13: ; preds = %5
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %37
18: ; preds = %13
br label %19
19: ; preds = %18
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
br label %37
27: ; preds = %19
%28 = load i32, i32* %2
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i64 @send(i32 %28, i8* %29, i32 %31, i32 0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %37
36: ; preds = %27
br label %5
37: ; preds = %34, %26, %16, %11
%38 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"recv\00"
@.str.2 = [3 x i8] c"/q\00"
@.str.3 = [5 x i8] c"send\00"
@str = [13 x i8] c"Done serving\00"
@str.5 = [38 x i8] c"Remote host has closed the connection\00"
define void @serve(i32 %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %18, %1
%5 = call i32 @recv(i32 %0, i8* %3, i32 512, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %24
9: ; preds = %4
%10 = icmp eq i32 %5, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.5, i64 0, i64 0))
br label %24
13: ; preds = %9
%14 = zext i32 %5 to i64
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %13
%19 = call i32 @strlen(i8* %3)
%20 = call i64 @send(i32 %0, i8* %3, i32 %19, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %4
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %13, %22, %11, %7
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%26 = call i32 @close(i32 %0)
ret void
}