#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
void send_echo(FILE *fp, int fd) {
  char send_buf[256] = {0};
  char recv_buf[256] = {0};
  int readn = 0;
  int writen = 0;
  while (fgets(send_buf, 256, fp) != NULL) {
    if ((writen = write(fd, send_buf, strlen(send_buf) + 1)) < 0) {
      printf("1st write error\n");
      break;
    } else {
      printf("1st write ok\n");
    }
    sleep(1);
    if ((writen = write(fd, "(test)", strlen("(test)") + 1)) < 0) {
      printf("2nd write error\n");
      break;
    } else {
      printf("2nd write ok\n");
    }
    sleep(1);
    if ((readn = read(fd, recv_buf, 256)) < 0) {
      printf("read error\n");
      break;
    } else if (readn == 0) {
      printf("read EOF\n");
      break;
    }
    fputs(recv_buf, stdout);
  }
  printf("client exit\n");
}


x86 assembly (clang, -O0):
<send_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
movl   $0x0,-0x214(%rbp)
movl   $0x0,-0x218(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x100,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     1332 <send_echo+0x1b2>
mov    -0xc(%rbp),%eax
mov    %eax,-0x224(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x220(%rbp)
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x224(%rbp),%edi
mov    -0x220(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    %eax,-0x218(%rbp)
cmp    $0x0,%eax
jge    1247 <send_echo+0xc7>
lea    0xdc3(%rip),%rdi
call   1050 <printf@plt>
jmp    1332 <send_echo+0x1b2>
lea    0xdc3(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x228(%rbp)
lea    0xdb2(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x228(%rbp),%edi
mov    %rax,%rdx
add    $0x1,%rdx
lea    0xd99(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0x218(%rbp)
cmp    $0x0,%eax
jge    12ab <send_echo+0x12b>
lea    0xd85(%rip),%rdi
call   1050 <printf@plt>
jmp    1332 <send_echo+0x1b2>
lea    0xd85(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0xc(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %eax,-0x214(%rbp)
cmp    $0x0,%eax
jge    12f5 <send_echo+0x175>
lea    0xd5a(%rip),%rdi
call   1050 <printf@plt>
jmp    1332 <send_echo+0x1b2>
cmpl   $0x0,-0x214(%rbp)
jne    1313 <send_echo+0x193>
lea    0xd48(%rip),%rdi
call   1050 <printf@plt>
jmp    1332 <send_echo+0x1b2>
jmp    1318 <send_echo+0x198>
lea    -0x210(%rbp),%rdi
mov    0x2cba(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
jmp    11cc <send_echo+0x4c>
lea    0xd22(%rip),%rdi
call   1050 <printf@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_echo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     136d <send_echo+0x1fd>
mov    %rsp,%r12
lea    0xd6d(%rip),%rbp
lea    0xd86(%rip),%r15
lea    0x100(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r12,%rsi
call   1040 <write@plt>
test   %eax,%eax
js     1346 <send_echo+0x1d6>
lea    0xd42(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0x7,%edx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <write@plt>
test   %eax,%eax
js     134f <send_echo+0x1df>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1358 <send_echo+0x1e8>
je     1361 <send_echo+0x1f1>
mov    0x2cbf(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1060 <fputs@plt>
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    12b0 <send_echo+0x140>
jmp    136d <send_echo+0x1fd>
lea    0xd04(%rip),%rdi
jmp    1368 <send_echo+0x1f8>
lea    0xceb(%rip),%rdi
jmp    1368 <send_echo+0x1f8>
lea    0xcd7(%rip),%rdi
jmp    1368 <send_echo+0x1f8>
lea    0xcc5(%rip),%rdi
call   1030 <puts@plt>
lea    0xc93(%rip),%rdi
call   1030 <puts@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"1st write error\0A\00"
@.str.1 = [14 x i8] c"1st write ok\0A\00"
@.str.2 = [7 x i8] c"(test)\00"
@.str.3 = [17 x i8] c"2nd write error\0A\00"
@.str.4 = [14 x i8] c"2nd write ok\0A\00"
@.str.5 = [12 x i8] c"read error\0A\00"
@.str.6 = [10 x i8] c"read EOF\0A\00"
@stdout = global i32 0
@.str.7 = [13 x i8] c"client exit\0A\00"
define void @send_echo(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%9 = bitcast [256 x i8]* %5 to i8*
%10 = bitcast [256 x i8]* %6 to i8*
store i32 0, i32* %7
store i32 0, i32* %8
br label %11
11: ; preds = %53, %2
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%13 = load i32*, i32** %3
%14 = call i32* @fgets(i8* %12, i32 256, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %57
16: ; preds = %11
%17 = load i32, i32* %4
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = call i32 @write(i32 %17, i8* %18, i64 %21)
store i32 %22, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %57
26: ; preds = %16
%27 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26
%29 = call i32 @sleep(i32 1)
%30 = load i32, i32* %4
%31 = call i64 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%32 = add nsw i64 %31, 1
%33 = call i32 @write(i32 %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 %32)
store i32 %33, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %57
37: ; preds = %28
%38 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37
%40 = call i32 @sleep(i32 1)
%41 = load i32, i32* %4
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 256)
store i32 %43, i32* %7
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %57
47: ; preds = %39
%48 = load i32, i32* %7
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %57
52: ; preds = %47
br label %53
53: ; preds = %52
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%55 = load i32, i32* @stdout
%56 = call i32 @fputs(i8* %54, i32 %55)
br label %11
57: ; preds = %50, %45, %35, %24, %11
%58 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.2 = [7 x i8] c"(test)\00"
@stdout = global i32 0
@str = [12 x i8] c"client exit\00"
@str.8 = [13 x i8] c"1st write ok\00"
@str.9 = [13 x i8] c"2nd write ok\00"
@str.10 = [9 x i8] c"read EOF\00"
@str.11 = [11 x i8] c"read error\00"
@str.12 = [16 x i8] c"2nd write error\00"
@str.13 = [16 x i8] c"1st write error\00"
define void @send_echo(i32* nocapture %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32* @fgets(i8* %5, i32 256, i32* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %34, label %9
9: ; preds = %2, %26
%10 = call i64 @strlen(i8* %5)
%11 = add nsw i64 %10, 1
%12 = call i32 @write(i32 %1, i8* %5, i64 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %31, label %14
14: ; preds = %9
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%16 = call i32 @sleep(i32 1)
%17 = call i32 @write(i32 %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 7)
%18 = icmp slt i32 %17, 0
br i1 %18, label %31, label %19
19: ; preds = %14
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
%21 = call i32 @sleep(i32 1)
%22 = call i32 @read(i32 %1, i8* %6, i32 256)
%23 = icmp slt i32 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %19
%25 = icmp eq i32 %22, 0
br i1 %25, label %31, label %26
26: ; preds = %24
%27 = load i32, i32* @stdout
%28 = call i32 @fputs(i8* %6, i32 %27)
%29 = call i32* @fgets(i8* %5, i32 256, i32* %0)
%30 = icmp eq i32* %29, null
br i1 %30, label %34, label %9
31: ; preds = %24, %19, %14, %9
%32 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.13, i64 0, i64 0), %9 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.12, i64 0, i64 0), %14 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.11, i64 0, i64 0), %19 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @str.10, i64 0, i64 0), %24 ]
%33 = call i32 @puts(i8* dereferenceable(1) %32)
br label %34
34: ; preds = %26, %31, %2
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
ret void
}