#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ sendline;
typedef int /*<<< orphan*/ recvline;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ SIGTERM;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void fork_str_cli(FILE *fp, int sockfd) {
  ssize_t n;
  pid_t pid;
  char sendline[1024], recvline[1024];
  if (0 == (pid = fork())) {
    while (1) {
      n = read(sockfd, recvline, sizeof(recvline));
      if (n < 0) {
        if (EINTR == errno) {
          continue;
        }
        perror("read");
        exit(EXIT_FAILURE);
      } else if (0 == n) {
        break;
      }
      kill(getppid(), SIGTERM);
      fputs(recvline, stdout);
    }
    exit(EXIT_SUCCESS);
  }
  while (NULL != fgets(sendline, sizeof(sendline), stdin)) {
    write(sockfd, sendline, strlen(sendline));
  }
  shutdown(sockfd, SHUT_WR);
  pause();
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<fork_str_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jne    129e <fork_str_cli+0xde>
jmp    11ed <fork_str_cli+0x2d>
mov    -0xc(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1249 <fork_str_cli+0x89>
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
jne    122f <fork_str_cli+0x6f>
jmp    11ed <fork_str_cli+0x2d>
lea    0xdca(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    125a <fork_str_cli+0x9a>
jmp    1290 <fork_str_cli+0xd0>
jmp    125f <fork_str_cli+0x9f>
mov    $0x0,%al
call   10c0 <getppid@plt>
mov    %eax,%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
lea    -0x820(%rbp),%rdi
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
jmp    11ed <fork_str_cli+0x2d>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    12a3 <fork_str_cli+0xe3>
lea    -0x420(%rbp),%rdi
mov    0x2d27(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1080 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1307 <fork_str_cli+0x147>
mov    -0xc(%rbp),%eax
mov    %eax,-0x82c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    12a3 <fork_str_cli+0xe3>
mov    -0xc(%rbp),%edi
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    $0x0,%al
call   10a0 <pause@plt>
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<fork_str_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    1245 <fork_str_cli+0x85>
mov    %rsp,%r14
mov    0x2dd0(%rip),%r15
mov    0x2dd9(%rip),%r12
mov    0x2dea(%rip),%r13
mov    0x2db3(%rip),%rbx
jmp    120d <fork_str_cli+0x4d>
nop
mov    (%r15),%rax
cmp    (%r12),%rax
jne    12be <fork_str_cli+0xfe>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
js     1200 <fork_str_cli+0x40>
je     12b0 <fork_str_cli+0xf0>
xor    %eax,%eax
call   10c0 <getppid@plt>
mov    0x0(%r13),%esi
mov    %eax,%edi
call   1090 <kill@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1060 <fputs@plt>
jmp    120d <fork_str_cli+0x4d>
mov    0x2d8c(%rip),%r14
mov    (%r14),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     1299 <fork_str_cli+0xd9>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
test   %rax,%rax
jne    1270 <fork_str_cli+0xb0>
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <shutdown@plt>
xor    %eax,%eax
call   10a0 <pause@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xd3b(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@EINTR = global i64 0
@errno = global i64 0
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@SIGTERM = global i32 0
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
@stdin = global i32 0
@SHUT_WR = global i32 0
define void @fork_str_cli(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca [1024 x i8]
store i32* %0, i32** %3
store i32 %1, i32* %4
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = icmp eq i32 0, %9
br i1 %10, label %11, label %42
11: ; preds = %2
br label %12
12: ; preds = %11, %22, %32
%13 = load i32, i32* %4
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%15 = call i64 @read(i32 %13, i8* %14, i32 1024)
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %12
%19 = load i64, i64* @EINTR
%20 = load i64, i64* @errno
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
br label %12
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %12
%28 = load i64, i64* %5
%29 = icmp eq i64 0, %28
br i1 %29, label %30, label %31
30: ; preds = %27
br label %39
31: ; preds = %27
br label %32
32: ; preds = %31
%33 = call i32 (...) @getppid()
%34 = load i32, i32* @SIGTERM
%35 = call i32 @kill(i32 %33, i32 %34)
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%37 = load i32, i32* @stdout
%38 = call i32 @fputs(i8* %36, i32 %37)
br label %12
39: ; preds = %30
%40 = load i32, i32* @EXIT_SUCCESS
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %2
br label %43
43: ; preds = %48, %42
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%45 = load i32, i32* @stdin
%46 = call i32* @fgets(i8* %44, i32 1024, i32 %45)
%47 = icmp ne i32* null, %46
br i1 %47, label %48, label %54
48: ; preds = %43
%49 = load i32, i32* %4
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @write(i32 %49, i8* %50, i32 %52)
br label %43
54: ; preds = %43
%55 = load i32, i32* %4
%56 = load i32, i32* @SHUT_WR
%57 = call i32 @shutdown(i32 %55, i32 %56)
%58 = call i32 (...) @pause()
%59 = load i32, i32* @EXIT_SUCCESS
%60 = call i32 @exit(i32 %59)
unreachable
}

LLVM IR (-O2):
@EINTR = global i64 0
@errno = global i64 0
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@SIGTERM = global i32 0
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
@stdin = global i32 0
@SHUT_WR = global i32 0
define void @fork_str_cli(i32* nocapture readnone %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i32, i32* @stdin
%11 = call i32* @fgets(i8* %5, i32 1024, i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %42, label %36
13: ; preds = %2, %32
%14 = call i64 @read(i32 %1, i8* %6, i32 1024)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = load i64, i64* @EINTR
%18 = load i64, i64* @errno
%19 = icmp eq i64 %17, %18
br i1 %19, label %32, label %20
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %13
%25 = icmp eq i64 %14, 0
br i1 %25, label %33, label %26
26: ; preds = %24
%27 = call i32 (...) @getppid()
%28 = load i32, i32* @SIGTERM
%29 = call i32 @kill(i32 %27, i32 %28)
%30 = load i32, i32* @stdout
%31 = call i32 @fputs(i8* %6, i32 %30)
br label %32
32: ; preds = %26, %16
br label %13
33: ; preds = %24
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %9, %36
%37 = call i32 @strlen(i8* %5)
%38 = call i32 @write(i32 %1, i8* %5, i32 %37)
%39 = load i32, i32* @stdin
%40 = call i32* @fgets(i8* %5, i32 1024, i32 %39)
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %36
42: ; preds = %36, %9
%43 = load i32, i32* @SHUT_WR
%44 = call i32 @shutdown(i32 %1, i32 %43)
%45 = call i32 (...) @pause()
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
}