#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 TCPrecv(int, char *, int, int /*<<< orphan*/);
int clientTCPsock(char *, int);
int /*<<< orphan*/ errmesg(char *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int, int, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int TCPecho(char *destination, int portN) {
  char buf[128 + 1];
  int sock;
  int outchars, inchars;
  if ((sock = clientTCPsock(destination, portN)) < 0)
    errmesg("fail to obtain TCP socket");
  while (fgets(buf, sizeof(buf), stdin)) {
    buf[128] = '\0';
    outchars = strlen(buf);
    (void)write(sock, buf, outchars);
    printf("\tTCPecho(%s, %d): sent %d bytes to echod: `%s`\n", destination,
           portN, outchars, buf);
    inchars = TCPrecv(sock, buf, 128 - 1, 0);
    if (inchars < 0)
      errmesg("socket read failed\n");
    fputs(buf, stdout);
  }
  return 0;
}


x86 assembly (clang, -O0):
<TCPecho>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <clientTCPsock@plt>
mov    %eax,-0x94(%rbp)
cmp    $0x0,%eax
jge    11b9 <TCPecho+0x39>
lea    0xe4c(%rip),%rdi
call   1090 <errmesg@plt>
jmp    11be <TCPecho+0x3e>
lea    -0x90(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    $0x81,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1282 <TCPecho+0x102>
movb   $0x0,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x98(%rbp)
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x98(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x98(%rbp),%ecx
lea    -0x90(%rbp),%r8
lea    0xdef(%rip),%rdi
call   1060 <printf@plt>
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x7f,%edx
xor    %ecx,%ecx
call   10a0 <TCPrecv@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0x0,-0x9c(%rbp)
jge    1268 <TCPecho+0xe8>
lea    0xde7(%rip),%rdi
call   1090 <errmesg@plt>
lea    -0x90(%rbp),%rdi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
call   1070 <fputs@plt>
jmp    11be <TCPecho+0x3e>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TCPecho>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
call   1050 <clientTCPsock@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11ae <TCPecho+0x2e>
lea    0xe57(%rip),%rdi
call   1090 <errmesg@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x81,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     1264 <TCPecho+0xe4>
mov    %rsp,%rbx
mov    0x2e01(%rip),%r12
jmp    1207 <TCPecho+0x87>
nopl   0x0(%rax)
mov    (%r12),%esi
mov    %rbx,%rdi
call   1070 <fputs@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x81,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     1264 <TCPecho+0xe4>
movb   $0x0,0x80(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xded(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
mov    %r13d,%ecx
mov    %rbx,%r8
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7f,%edx
xor    %ecx,%ecx
call   10a0 <TCPrecv@plt>
test   %eax,%eax
jns    11e0 <TCPecho+0x60>
lea    0xdf0(%rip),%rdi
call   1090 <errmesg@plt>
jmp    11e0 <TCPecho+0x60>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"fail to obtain TCP socket\00"
@stdin = global i32 0
@.str.1 = [48 x i8] c"\09TCPecho(%s, %d): sent %d bytes to echod: `%s`\0A\00"
@.str.2 = [20 x i8] c"socket read failed\0A\00"
@stdout = global i32 0
define i32 @TCPecho(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [129 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = load i32, i32* %4
%11 = call i32 @clientTCPsock(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @errmesg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
br label %16
16: ; preds = %41, %15
%17 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%18 = load i32, i32* @stdin
%19 = call i64 @fgets(i8* %17, i32 129, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %45
21: ; preds = %16
%22 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 128
store i8 0, i8* %22
%23 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %7
%25 = load i32, i32* %6
%26 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%27 = load i32, i32* %7
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
%29 = load i8*, i8** %3
%30 = load i32, i32* %4
%31 = load i32, i32* %7
%32 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %30, i32 %31, i8* %32)
%34 = load i32, i32* %6
%35 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%36 = call i32 @TCPrecv(i32 %34, i8* %35, i32 127, i32 0)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %21
%40 = call i32 @errmesg(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %21
%42 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%43 = load i32, i32* @stdout
%44 = call i32 @fputs(i8* %42, i32 %43)
br label %16
45: ; preds = %16
ret i32 0
}

LLVM IR (-O2):
@.str = [26 x i8] c"fail to obtain TCP socket\00"
@stdin = global i32 0
@.str.1 = [48 x i8] c"\09TCPecho(%s, %d): sent %d bytes to echod: `%s`\0A\00"
@.str.2 = [20 x i8] c"socket read failed\0A\00"
@stdout = global i32 0
define i32 @TCPecho(i8* %0, i32 %1) {
%3 = alloca [129 x i8]
%4 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @clientTCPsock(i8* %0, i32 %1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @errmesg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* @stdin
%11 = call i64 @fgets(i8* %4, i32 129, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %29, label %13
13: ; preds = %9
%14 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 128
br label %15
15: ; preds = %13, %23
store i8 0, i8* %14
%16 = call i32 @strlen(i8* %4)
%17 = call i32 @write(i32 %5, i8* %4, i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1, i32 %16, i8* %4)
%19 = call i32 @TCPrecv(i32 %5, i8* %4, i32 127, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @errmesg(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = load i32, i32* @stdout
%25 = call i32 @fputs(i8* %4, i32 %24)
%26 = load i32, i32* @stdin
%27 = call i64 @fgets(i8* %4, i32 129, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %15
29: ; preds = %23, %9
ret i32 0
}