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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
char rot_char(char, int);
int /*<<< orphan*/ send(int, char *, size_t, int /*<<< orphan*/);
void handle_child(int fd) {
  char outbuf[16384 + 1];
  size_t outbuf_used = 0;
  ssize_t result;
  for (;;) {
    char ch;
    result = recv(fd, &ch, 1, 0);
    if (result == 0) {
      break;
    } else if (result == -1) {
      perror("read");
      break;
    }
    if (outbuf_used < sizeof(outbuf)) {
      outbuf[outbuf_used++] = rot_char(ch, 3);
    }
    if (ch == '\n') {
      send(fd, outbuf, outbuf_used, 0);
      outbuf_used = 0;
      continue;
    }
  }
}


x86 assembly (clang, -O0):
<handle_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x4018(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x401d(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x401c(%rbp)
cmpl   $0x0,-0x401c(%rbp)
jne    1187 <handle_child+0x47>
jmp    1227 <handle_child+0xe7>
cmpl   $0xffffffff,-0x401c(%rbp)
jne    11a5 <handle_child+0x65>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
jmp    1227 <handle_child+0xe7>
jmp    11aa <handle_child+0x6a>
cmpq   $0x4001,-0x4018(%rbp)
jae    11ea <handle_child+0xaa>
mov    $0x3,%esi
movsbl -0x401d(%rbp),%edi
call   1040 <rot_char@plt>
mov    %al,%cl
mov    -0x4018(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x4018(%rbp)
mov    %cl,-0x4010(%rbp,%rax,1)
movsbl -0x401d(%rbp),%eax
cmp    $0xa,%eax
jne    1222 <handle_child+0xe2>
mov    -0x4(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    -0x4018(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
movq   $0x0,-0x4018(%rbp)
jmp    1159 <handle_child+0x19>
jmp    1159 <handle_child+0x19>
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_child>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4018,%rsp
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0xf(%rsp),%r14
lea    0x10(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     11c0 <handle_child+0x80>
cmp    $0xffffffff,%eax
je     11b4 <handle_child+0x74>
cmp    $0x4000,%rbx
ja     119a <handle_child+0x5a>
movsbl 0xf(%rsp),%edi
mov    $0x3,%esi
call   1040 <rot_char@plt>
mov    %al,0x10(%rsp,%rbx,1)
add    $0x1,%rbx
cmpb   $0xa,0xf(%rsp)
jne    1160 <handle_child+0x20>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
xor    %ebx,%ebx
jmp    1160 <handle_child+0x20>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
add    $0x4018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
define void @handle_child(i32 %0) {
%2 = alloca i32
%3 = alloca [16385 x i8]
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8
store i32 %0, i32* %2
store i64 0, i64* %4
br label %7
7: ; preds = %37, %32, %1
%8 = load i32, i32* %2
%9 = call i32 @recv(i32 %8, i8* %6, i32 1, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %7
br label %38
13: ; preds = %7
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %38
18: ; preds = %13
br label %19
19: ; preds = %18
%20 = load i64, i64* %4
%21 = icmp ult i64 %20, 16385
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i8, i8* %6
%24 = call signext i8 @rot_char(i8 signext %23, i32 3)
%25 = load i64, i64* %4
%26 = add i64 %25, 1
store i64 %26, i64* %4
%27 = getelementptr inbounds [16385 x i8], [16385 x i8]* %3, i64 0, i64 %25
store i8 %24, i8* %27
br label %28
28: ; preds = %22, %19
%29 = load i8, i8* %6
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* %2
%34 = getelementptr inbounds [16385 x i8], [16385 x i8]* %3, i64 0, i64 0
%35 = load i64, i64* %4
%36 = call i32 @send(i32 %33, i8* %34, i64 %35, i32 0)
store i64 0, i64* %4
br label %7
37: ; preds = %28
br label %7
38: ; preds = %16, %12
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
define void @handle_child(i32 %0) {
%2 = alloca [16385 x i8]
%3 = alloca i8
%4 = getelementptr inbounds [16385 x i8], [16385 x i8]* %2, i64 0, i64 0
br label %5
5: ; preds = %23, %1
%6 = phi i64 [ 0, %1 ], [ %24, %23 ]
%7 = call i32 @recv(i32 %0, i8* %3, i32 1, i32 0)
switch i32 %7, label %10 [
i32 0, label %25
i32 -1, label %8
]
8: ; preds = %5
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %25
10: ; preds = %5
%11 = icmp ult i64 %6, 16385
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = load i8, i8* %3
%14 = call signext i8 @rot_char(i8 signext %13, i32 3)
%15 = add nuw nsw i64 %6, 1
%16 = getelementptr inbounds [16385 x i8], [16385 x i8]* %2, i64 0, i64 %6
store i8 %14, i8* %16
br label %17
17: ; preds = %12, %10
%18 = phi i64 [ %15, %12 ], [ %6, %10 ]
%19 = load i8, i8* %3
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @send(i32 %0, i8* %4, i64 %18, i32 0)
br label %23
23: ; preds = %17, %21
%24 = phi i64 [ 0, %21 ], [ %18, %17 ]
br label %5
25: ; preds = %5, %8
ret void
}