#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 connectTCP(char const *, char const *);
int /*<<< orphan*/ errexit(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int TCPecho(const char *host, const char *service) {
  char buf[128 + 1];
  int s, n;
  int outchars, inchars;
  s = connectTCP(host, service);
  while (fgets(buf, sizeof(buf), stdin)) {
    buf[128] = '\0';
    outchars = strlen(buf);
    (void)write(s, buf, outchars);
    for (inchars = 0; inchars < outchars; inchars += n) {
      n = read(s, &buf[inchars], outchars - inchars);
      if (n < 0)
        errexit("socket read failed: %s\n", strerror(errno));
    }
    fputs(buf, stdout);
  }
}


x86 assembly (clang, -O0):
<TCPecho>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <connectTCP@plt>
mov    %eax,-0xa4(%rbp)
lea    -0xa0(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    $0x81,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12a1 <TCPecho+0x121>
movb   $0x0,-0x20(%rbp)
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xac(%rbp)
mov    -0xa4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0xac(%rbp),%edx
call   1030 <write@plt>
movl   $0x0,-0xb0(%rbp)
mov    -0xb0(%rbp),%eax
cmp    -0xac(%rbp),%eax
jge    1287 <TCPecho+0x107>
mov    -0xa4(%rbp),%edi
movslq -0xb0(%rbp),%rax
lea    -0xa0(%rbp),%rsi
add    %rax,%rsi
mov    -0xac(%rbp),%edx
sub    -0xb0(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0xa8(%rbp)
cmpl   $0x0,-0xa8(%rbp)
jge    126b <TCPecho+0xeb>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd9a(%rip),%rdi
call   1070 <errexit@plt>
jmp    1270 <TCPecho+0xf0>
mov    -0xa8(%rbp),%eax
add    -0xb0(%rbp),%eax
mov    %eax,-0xb0(%rbp)
jmp    1202 <TCPecho+0x82>
lea    -0xa0(%rbp),%rdi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
jmp    11a6 <TCPecho+0x26>
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TCPecho>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
call   1090 <connectTCP@plt>
mov    %eax,%r12d
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x81,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     1266 <TCPecho+0xe6>
mov    %rsp,%rbx
mov    0x2e16(%rip),%r13
lea    0xe37(%rip),%r15
jmp    11ff <TCPecho+0x7f>
nopl   0x0(%rax,%rax,1)
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x81,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     1266 <TCPecho+0xe6>
movb   $0x0,0x80(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %ebp,%ebp
jle    11d0 <TCPecho+0x50>
xor    %r14d,%r14d
jmp    1238 <TCPecho+0xb8>
nopw   0x0(%rax,%rax,1)
add    %ebx,%r14d
cmp    %r14d,%ebp
jle    11d0 <TCPecho+0x50>
movslq %r14d,%rax
lea    (%rsp,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1230 <TCPecho+0xb0>
mov    0x0(%r13),%edi
call   10a0 <strerror@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1070 <errexit@plt>
jmp    1230 <TCPecho+0xb0>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [24 x i8] c"socket read failed: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
define i32 @TCPecho(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [129 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i32 @connectTCP(i8* %11, i8* %12)
store i32 %13, i32* %7
br label %14
14: ; preds = %51, %2
%15 = getelementptr inbounds [129 x i8], [129 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* @stdin
%17 = call i64 @fgets(i8* %15, i32 129, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %55
19: ; preds = %14
%20 = getelementptr inbounds [129 x i8], [129 x i8]* %6, i64 0, i64 128
store i8 0, i8* %20
%21 = getelementptr inbounds [129 x i8], [129 x i8]* %6, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %9
%23 = load i32, i32* %7
%24 = getelementptr inbounds [129 x i8], [129 x i8]* %6, i64 0, i64 0
%25 = load i32, i32* %9
%26 = call i32 @write(i32 %23, i8* %24, i32 %25)
store i32 0, i32* %10
br label %27
27: ; preds = %47, %19
%28 = load i32, i32* %10
%29 = load i32, i32* %9
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %51
31: ; preds = %27
%32 = load i32, i32* %7
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [129 x i8], [129 x i8]* %6, i64 0, i64 %34
%36 = load i32, i32* %9
%37 = load i32, i32* %10
%38 = sub nsw i32 %36, %37
%39 = call i32 @read(i32 %32, i8* %35, i32 %38)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %31
%43 = load i32, i32* @errno
%44 = call i32 @strerror(i32 %43)
%45 = call i32 @errexit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %42, %31
br label %47
47: ; preds = %46
%48 = load i32, i32* %8
%49 = load i32, i32* %10
%50 = add nsw i32 %49, %48
store i32 %50, i32* %10
br label %27
51: ; preds = %27
%52 = getelementptr inbounds [129 x i8], [129 x i8]* %6, i64 0, i64 0
%53 = load i32, i32* @stdout
%54 = call i32 @fputs(i8* %52, i32 %53)
br label %14
55: ; preds = %14
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [24 x i8] c"socket read failed: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
define i32 @TCPecho(i8* %0, i8* %1) {
%3 = alloca [129 x i8]
%4 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @connectTCP(i8* %0, i8* %1)
%6 = load i32, i32* @stdin
%7 = call i64 @fgets(i8* %4, i32 129, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %2
%10 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 128
br label %11
11: ; preds = %9, %29
store i8 0, i8* %10
%12 = call i32 @strlen(i8* %4)
%13 = call i32 @write(i32 %5, i8* %4, i32 %12)
%14 = icmp sgt i32 %12, 0
br i1 %14, label %15, label %29
15: ; preds = %11, %26
%16 = phi i32 [ %27, %26 ], [ 0, %11 ]
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 %17
%19 = sub nsw i32 %12, %16
%20 = call i32 @read(i32 %5, i8* %18, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 @errexit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %15, %22
%27 = add nsw i32 %20, %16
%28 = icmp sgt i32 %12, %27
br i1 %28, label %15, label %29
29: ; preds = %26, %11
%30 = load i32, i32* @stdout
%31 = call i32 @fputs(i8* %4, i32 %30)
%32 = load i32, i32* @stdin
%33 = call i64 @fgets(i8* %4, i32 129, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %11
35: ; preds = %29, %2
ret i32 undef
}