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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
void do_echo(int connfd) {
  int ret;
  char buf[1024] = {};
  for (;;) {
    ret = read(connfd, buf, sizeof(buf));
    if (ret == -1)
      do {
        fprintf(stderr, "[%s][%d]:%s %s\n",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/"
                "heads/master/apue/day12/echosvr.c",
                26, "read", strerror(errno));
        exit(EXIT_FAILURE);
      } while (0);
    else if (ret == 0) {
      printf("cli close!\n");
      exit(EXIT_SUCCESS);
    }
    if (write(connfd, buf, ret) == -1)
      do {
        fprintf(stderr, "[%s][%d]:%s %s\n",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/"
                "heads/master/apue/day12/echosvr.c",
                32, "write", strerror(errno));
        exit(EXIT_FAILURE);
      } while (0);
  }
}


x86 assembly (clang, -O0):
<do_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    120a <do_echo+0x9a>
jmp    11b7 <do_echo+0x47>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x414(%rbp),%edi
mov    %rax,%r9
lea    0xe1c(%rip),%rsi
lea    0xe25(%rip),%rdx
mov    $0x1a,%ecx
lea    0xeaf(%rip),%r8
call   1070 <fprintf@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    122e <do_echo+0xbe>
lea    0xe90(%rip),%rdi
call   1040 <printf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1233 <do_echo+0xc3>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12a6 <do_echo+0x136>
jmp    1253 <do_echo+0xe3>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x418(%rbp),%edi
mov    %rax,%r9
lea    0xd80(%rip),%rsi
lea    0xd89(%rip),%rdx
mov    $0x20,%ecx
lea    0xe24(%rip),%r8
call   1070 <fprintf@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1191 <do_echo+0x21>

x86 assembly (clang, -O2):
<do_echo>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11ee <do_echo+0x7e>
cmp    $0xffffffff,%eax
je     1208 <do_echo+0x98>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1190 <do_echo+0x20>
mov    0x2e20(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe29(%rip),%rsi
lea    0xe32(%rip),%rdx
lea    0xec6(%rip),%r8
mov    %ebx,%edi
mov    $0x20,%ecx
jmp    123b <do_echo+0xcb>
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdda(%rip),%rsi
lea    0xde3(%rip),%rdx
lea    0xe72(%rip),%r8
mov    %ebx,%edi
mov    $0x1a,%ecx
mov    %rax,%r9
call   1070 <fprintf@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [150 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day12/echosvr.c\00"
@.str.2 = [5 x i8] c"read\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"cli close!\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [6 x i8] c"write\00"
define void @do_echo(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 %0, i32* %2
%5 = bitcast [1024 x i8]* %4 to i8*
br label %6
6: ; preds = %44, %1
%7 = load i32, i32* %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 1024)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %21
12: ; preds = %6
br label %13
13: ; preds = %12
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i32 26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; No predecessors!
br label %29
21: ; preds = %6
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_SUCCESS
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %21
br label %29
29: ; preds = %28, %20
%30 = load i32, i32* %2
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = load i32, i32* %3
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %44
35: ; preds = %29
br label %36
36: ; preds = %35
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; No predecessors!
br label %44
44: ; preds = %43, %29
br label %6
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [150 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day12/echosvr.c\00"
@.str.2 = [5 x i8] c"read\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.4 = [6 x i8] c"write\00"
@str = [11 x i8] c"cli close!\00"
define void @do_echo(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %17, %1
%5 = call i32 @read(i32 %0, i8* %3, i32 1024)
switch i32 %5, label %17 [
i32 -1, label %6
i32 0, label %13
]
6: ; preds = %4
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i32 26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %4
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %4
%18 = call i32 @write(i32 %0, i8* %3, i32 %5)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %4
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
}