#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ get_or_set_grab_request(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, size_t, char *);
int read(int, char *, int);
char *realloc(char *, size_t);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
void *serve(void *c) {
  int conn = (int)c;
  char block[1024];
  char *buffer_in;
  size_t buffer_size = 0;
  fd_set fds;
  struct timeval tv;
  int len;
  FD_ZERO(&fds);
  FD_SET(conn, &fds);
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  if (!select(conn + 1, &fds, NULL, NULL, &tv)) {
    close(conn);
    return NULL;
  }
  while (1) {
    len = read(conn, block, 1024);
    buffer_size += len;
    if (buffer_size > 65536) {
      close(conn);
      return NULL;
    }
    buffer_in = realloc(buffer_in, buffer_size + 1);
    memcpy(buffer_in + buffer_size - len, block, len);
    buffer_in[buffer_size] = 0;
    if (buffer_in[buffer_size - 3] == '\n' &&
        buffer_in[buffer_size - 2] == '\r' &&
        buffer_in[buffer_size - 1] == '\n')
      break;
    else if (buffer_in[buffer_size - 2] == '\n' &&
             buffer_in[buffer_size - 1] == '\n')
      break;
    FD_ZERO(&fds);
    FD_SET(conn, &fds);
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    if (!select(conn + 1, &fds, NULL, NULL, &tv))
      break;
  }
  printf("%d: %s\n", buffer_size, buffer_in);
  get_or_set_grab_request(1);
  while (1) {
    get_or_set_grab_request(1);
    tv.tv_sec = 0;
    tv.tv_usec = 500000;
    select(0, NULL, NULL, NULL, &tv);
  }
  close(conn);
  return NULL;
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x430(%rbp)
lea    -0x434(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x434(%rbp),%rsi
call   10b0 <FD_SET@plt>
movl   $0x1,-0x440(%rbp)
movl   $0x0,-0x43c(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x434(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x440(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
cmp    $0x0,%eax
jne    121e <serve+0x8e>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1442 <serve+0x2b2>
jmp    1223 <serve+0x93>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x444(%rbp)
movslq -0x444(%rbp),%rax
add    -0x430(%rbp),%rax
mov    %rax,-0x430(%rbp)
cmpq   $0x10000,-0x430(%rbp)
jbe    1278 <serve+0xe8>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1442 <serve+0x2b2>
mov    -0x428(%rbp),%rdi
mov    -0x430(%rbp),%rsi
add    $0x1,%rsi
call   10a0 <realloc@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
add    -0x430(%rbp),%rdi
movslq -0x444(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
lea    -0x420(%rbp),%rsi
mov    -0x444(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x428(%rbp),%rax
mov    -0x430(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x428(%rbp),%rax
mov    -0x430(%rbp),%rcx
sub    $0x3,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1339 <serve+0x1a9>
mov    -0x428(%rbp),%rax
mov    -0x430(%rbp),%rcx
sub    $0x2,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1339 <serve+0x1a9>
mov    -0x428(%rbp),%rax
mov    -0x430(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1339 <serve+0x1a9>
jmp    13e3 <serve+0x253>
mov    -0x428(%rbp),%rax
mov    -0x430(%rbp),%rcx
sub    $0x2,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    137c <serve+0x1ec>
mov    -0x428(%rbp),%rax
mov    -0x430(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    137c <serve+0x1ec>
jmp    13e3 <serve+0x253>
jmp    1381 <serve+0x1f1>
lea    -0x434(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x434(%rbp),%rsi
call   10b0 <FD_SET@plt>
movl   $0x1,-0x440(%rbp)
movl   $0x0,-0x43c(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x434(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x440(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
cmp    $0x0,%eax
jne    13de <serve+0x24e>
jmp    13e3 <serve+0x253>
jmp    1223 <serve+0x93>
mov    -0x430(%rbp),%rsi
mov    -0x428(%rbp),%rdx
lea    0xc08(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <get_or_set_grab_request@plt>
mov    $0x1,%edi
call   1060 <get_or_set_grab_request@plt>
movl   $0x0,-0x440(%rbp)
movl   $0x7a120,-0x43c(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x440(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1090 <select@plt>
jmp    1407 <serve+0x277>
mov    -0x8(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r13
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
movq   $0x1,(%rsp)
lea    0x1(%r13),%edi
mov    %rsp,%r8
mov    %edi,0x8(%rsp)
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <select@plt>
test   %eax,%eax
je     1300 <serve+0x170>
xor    %ebx,%ebx
lea    0x10(%rsp),%r15
jmp    1235 <serve+0xa5>
nopl   0x0(%rax)
cmpb   $0xa,-0x1(%rbx,%rbp,1)
mov    %r14,%r15
je     12b0 <serve+0x120>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1080 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %r14,%rsi
call   10b0 <FD_SET@plt>
movq   $0x1,(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rsp,%r8
call   1090 <select@plt>
test   %eax,%eax
je     12b0 <serve+0x120>
mov    %rbx,%r14
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
movslq %eax,%rbx
add    %r14,%rbx
cmp    $0x10000,%rbx
ja     1300 <serve+0x170>
mov    %eax,%r12d
lea    0x1(%rbx),%rsi
mov    %rbp,%rdi
call   10a0 <realloc@plt>
mov    %rax,%rbp
add    %rax,%r14
mov    %r14,%rdi
mov    %r15,%r14
mov    %r15,%rsi
mov    %r12d,%edx
call   1070 <memcpy@plt>
movb   $0x0,0x0(%rbp,%rbx,1)
movzbl -0x2(%rbp,%rbx,1),%eax
cmpb   $0xa,-0x3(%rbp,%rbx,1)
jne    129a <serve+0x10a>
cmp    $0xd,%al
je     11f0 <serve+0x60>
cmp    $0xa,%al
mov    %r14,%r15
jne    11fe <serve+0x6e>
cmpb   $0xa,-0x1(%rbx,%rbp,1)
jne    11fe <serve+0x6e>
lea    0xd49(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <get_or_set_grab_request@plt>
movabs $0x7a12000000000,%rbp
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
call   1060 <get_or_set_grab_request@plt>
mov    %rbp,(%rsp)
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1090 <select@plt>
jmp    12e0 <serve+0x150>
mov    %r13d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"%d: %s\0A\00"
define i8* @serve(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = ptrtoint i8* %11 to i32
store i32 %12, i32* %4
store i64 0, i64* %7
%13 = call i32 @FD_ZERO(i32* %8)
%14 = load i32, i32* %4
%15 = call i32 @FD_SET(i32 %14, i32* %8)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 0, i32* %17
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
%20 = call i32 @select(i32 %19, i32* %8, i32* null, i32* null, %struct.timeval* %9)
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %1
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
store i8* null, i8** %2
br label %121
25: ; preds = %1
br label %26
26: ; preds = %25, %110
%27 = load i32, i32* %4
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 1024)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = sext i32 %30 to i64
%32 = load i64, i64* %7
%33 = add i64 %32, %31
store i64 %33, i64* %7
%34 = load i64, i64* %7
%35 = icmp ugt i64 %34, 65536
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
store i8* null, i8** %2
br label %121
39: ; preds = %26
%40 = load i8*, i8** %6
%41 = load i64, i64* %7
%42 = add i64 %41, 1
%43 = call i8* @realloc(i8* %40, i64 %42)
store i8* %43, i8** %6
%44 = load i8*, i8** %6
%45 = load i64, i64* %7
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = sub i64 0, %48
%50 = getelementptr inbounds i8, i8* %46, i64 %49
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%52 = load i32, i32* %10
%53 = call i32 @memcpy(i8* %50, i8* %51, i32 %52)
%54 = load i8*, i8** %6
%55 = load i64, i64* %7
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 0, i8* %56
%57 = load i8*, i8** %6
%58 = load i64, i64* %7
%59 = sub i64 %58, 3
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 10
br i1 %63, label %64, label %81
64: ; preds = %39
%65 = load i8*, i8** %6
%66 = load i64, i64* %7
%67 = sub i64 %66, 2
%68 = getelementptr inbounds i8, i8* %65, i64 %67
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp eq i32 %70, 13
br i1 %71, label %72, label %81
72: ; preds = %64
%73 = load i8*, i8** %6
%74 = load i64, i64* %7
%75 = sub i64 %74, 1
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp eq i32 %78, 10
br i1 %79, label %80, label %81
80: ; preds = %72
br label %111
81: ; preds = %72, %64, %39
%82 = load i8*, i8** %6
%83 = load i64, i64* %7
%84 = sub i64 %83, 2
%85 = getelementptr inbounds i8, i8* %82, i64 %84
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = icmp eq i32 %87, 10
br i1 %88, label %89, label %98
89: ; preds = %81
%90 = load i8*, i8** %6
%91 = load i64, i64* %7
%92 = sub i64 %91, 1
%93 = getelementptr inbounds i8, i8* %90, i64 %92
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = icmp eq i32 %95, 10
br i1 %96, label %97, label %98
97: ; preds = %89
br label %111
98: ; preds = %89, %81
br label %99
99: ; preds = %98
%100 = call i32 @FD_ZERO(i32* %8)
%101 = load i32, i32* %4
%102 = call i32 @FD_SET(i32 %101, i32* %8)
%103 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1, i32* %103
%104 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 0, i32* %104
%105 = load i32, i32* %4
%106 = add nsw i32 %105, 1
%107 = call i32 @select(i32 %106, i32* %8, i32* null, i32* null, %struct.timeval* %9)
%108 = icmp ne i32 %107, 0
br i1 %108, label %110, label %109
109: ; preds = %99
br label %111
110: ; preds = %99
br label %26
111: ; preds = %109, %97, %80
%112 = load i64, i64* %7
%113 = load i8*, i8** %6
%114 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %112, i8* %113)
%115 = call i32 @get_or_set_grab_request(i32 1)
br label %116
116: ; preds = %111, %116
%117 = call i32 @get_or_set_grab_request(i32 1)
%118 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 0, i32* %118
%119 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 500000, i32* %119
%120 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %9)
br label %116
121: ; preds = %36, %22
%122 = load i8*, i8** %2
ret i8* %122
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [8 x i8] c"%d: %s\0A\00"
define noalias i8* @serve(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = ptrtoint i8* %0 to i64
%6 = trunc i64 %5 to i32
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%8 = bitcast i32* %3 to i8*
%9 = bitcast %struct.timeval* %4 to i8*
%10 = call i32 @FD_ZERO(i32* %3)
%11 = call i32 @FD_SET(i32 %6, i32* %3)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 0, i32* %13
%14 = add nsw i32 %6, 1
%15 = call i32 @select(i32 %14, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %62, label %17
17: ; preds = %1, %51
%18 = phi i8* [ %26, %51 ], [ undef, %1 ]
%19 = phi i64 [ %22, %51 ], [ 0, %1 ]
%20 = call i32 @read(i32 %6, i8* %7, i32 1024)
%21 = sext i32 %20 to i64
%22 = add i64 %19, %21
%23 = icmp ugt i64 %22, 65536
br i1 %23, label %62, label %24
24: ; preds = %17
%25 = add nuw nsw i64 %22, 1
%26 = call i8* @realloc(i8* %18, i64 %25)
%27 = getelementptr inbounds i8, i8* %26, i64 %22
%28 = getelementptr inbounds i8, i8* %26, i64 %19
%29 = call i32 @memcpy(i8* %28, i8* %7, i32 %20)
store i8 0, i8* %27
%30 = add nsw i64 %22, -3
%31 = getelementptr inbounds i8, i8* %26, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 10
%34 = add nsw i64 %22, -2
%35 = getelementptr inbounds i8, i8* %26, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 13
%38 = select i1 %33, i1 %37, i1 false
br i1 %38, label %39, label %44
39: ; preds = %24
%40 = add nsw i64 %22, -1
%41 = getelementptr inbounds i8, i8* %26, i64 %40
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 10
br i1 %43, label %56, label %51
44: ; preds = %24
%45 = icmp eq i8 %36, 10
br i1 %45, label %46, label %51
46: ; preds = %44
%47 = add nsw i64 %22, -1
%48 = getelementptr inbounds i8, i8* %26, i64 %47
%49 = load i8, i8* %48
%50 = icmp eq i8 %49, 10
br i1 %50, label %56, label %51
51: ; preds = %39, %44, %46
%52 = call i32 @FD_ZERO(i32* %3)
%53 = call i32 @FD_SET(i32 %6, i32* %3)
store i32 1, i32* %12
store i32 0, i32* %13
%54 = call i32 @select(i32 %14, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %17
56: ; preds = %51, %46, %39
%57 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %22, i8* %26)
%58 = call i32 @get_or_set_grab_request(i32 1)
br label %59
59: ; preds = %59, %56
%60 = call i32 @get_or_set_grab_request(i32 1)
store i32 0, i32* %12
store i32 500000, i32* %13
%61 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %4)
br label %59
62: ; preds = %17, %1
%63 = call i32 @close(i32 %6)
ret i8* null
}