#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  char *s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(void *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strcpy(char *, char *);
int write(int, char *, int);
int main(void) {
  int fd, nbytes, nleft, nwritten, nread;
  char *ptr, buffer[128];
  struct sockaddr_in addr;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == 1) {
    exit(1);
  }
  memset((void *)&addr, (int)'\0', sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = "localhost";
  addr.sin_port = htons(5800);
  ptr = strcpy(buffer, "Hello!\n");
  nbytes = 7;
  nleft = nbytes;
  while (nleft > 0) {
    nwritten = write(fd, ptr, nleft);
    if (nwritten <= 0) {
      exit(1);
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  nleft = nbytes;
  ptr = &buffer[0];
  while (nleft > 0) {
    nread = write(fd, ptr, nleft);
    if (nread == -1) {
      exit(1);
    } else if (nread == 0) {
      break;
    }
    nleft -= nread;
    ptr += nread;
  }
  nread = nbytes - nbytes;
  close(fd);
  write(1, "echo: ", 6);
  write(1, buffer, nread);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11b2 <main+0x42>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0xb8(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa8(%rbp)
lea    0xe25(%rip),%rax
mov    %rax,-0xb0(%rbp)
mov    $0x16a8,%edi
call   1050 <htons@plt>
mov    %eax,-0xb8(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x20(%rbp)
movl   $0x7,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1267 <main+0xf7>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1246 <main+0xd6>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1216 <main+0xa6>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    12dd <main+0x16d>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12a8 <main+0x138>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    12b7 <main+0x147>
jmp    12dd <main+0x16d>
jmp    12bc <main+0x14c>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1278 <main+0x108>
mov    -0xc(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
lea    0xd18(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
lea    -0xa0(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
mov    0x2e76(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0x1,%eax
jne    1194 <main+0x34>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %eax,%r14d
lea    0x20(%rsp),%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
lea    0xe45(%rip),%rax
mov    %rax,0x10(%rsp)
mov    $0x16a8,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
movabs $0xa216f6c6c6548,%rax
mov    %rax,0x20(%rsp)
mov    $0x7,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    118a <main+0x2a>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     11f0 <main+0x90>
mov    $0x7,%ebx
lea    0x20(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
je     1244 <main+0xe4>
cmp    $0xffffffff,%eax
je     118a <main+0x2a>
movslq %eax,%rcx
add    %rcx,%rbp
sub    %eax,%ebx
jg     1220 <main+0xc0>
mov    %r14d,%edi
call   1060 <close@plt>
lea    0xdb7(%rip),%rsi
mov    $0x1,%edi
mov    $0x6,%edx
call   1030 <write@plt>
lea    0x20(%rsp),%rsi
mov    $0x1,%edi
xor    %edx,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [8 x i8] c"Hello!\0A\00"
@.str.2 = [7 x i8] c"echo: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [128 x i8]
%9 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = bitcast %struct.sockaddr_in* %9 to i8*
%19 = call i32 @memset(i8* %18, i32 0, i32 24)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %23
%24 = call i32 @htons(i32 5800)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%27 = call i8* @strcpy(i8* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %27, i8** %7
store i32 7, i32* %3
%28 = load i32, i32* %3
store i32 %28, i32* %4
br label %29
29: ; preds = %41, %17
%30 = load i32, i32* %4
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %49
32: ; preds = %29
%33 = load i32, i32* %2
%34 = load i8*, i8** %7
%35 = load i32, i32* %4
%36 = call i32 @write(i32 %33, i8* %34, i32 %35)
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = icmp sle i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %32
%42 = load i32, i32* %5
%43 = load i32, i32* %4
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %4
%45 = load i32, i32* %5
%46 = load i8*, i8** %7
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %7
br label %29
49: ; preds = %29
%50 = load i32, i32* %3
store i32 %50, i32* %4
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
store i8* %51, i8** %7
br label %52
52: ; preds = %69, %49
%53 = load i32, i32* %4
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %77
55: ; preds = %52
%56 = load i32, i32* %2
%57 = load i8*, i8** %7
%58 = load i32, i32* %4
%59 = call i32 @write(i32 %56, i8* %57, i32 %58)
store i32 %59, i32* %6
%60 = load i32, i32* %6
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %55
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %55
%65 = load i32, i32* %6
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %64
br label %77
68: ; preds = %64
br label %69
69: ; preds = %68
%70 = load i32, i32* %6
%71 = load i32, i32* %4
%72 = sub nsw i32 %71, %70
store i32 %72, i32* %4
%73 = load i32, i32* %6
%74 = load i8*, i8** %7
%75 = sext i32 %73 to i64
%76 = getelementptr inbounds i8, i8* %74, i64 %75
store i8* %76, i8** %7
br label %52
77: ; preds = %67, %52
%78 = load i32, i32* %3
%79 = load i32, i32* %3
%80 = sub nsw i32 %78, %79
store i32 %80, i32* %6
%81 = load i32, i32* %2
%82 = call i32 @close(i32 %81)
%83 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%84 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%85 = load i32, i32* %6
%86 = call i32 @write(i32 1, i8* %84, i32 %85)
%87 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.2 = [7 x i8] c"echo: \00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 @memset(i8* %4, i32 0, i32 24)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %15
%16 = call i32 @htons(i32 5800)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast [128 x i8]* %1 to i64*
store i64 2851512211236168, i64* %18
br label %19
19: ; preds = %11, %26
%20 = phi i8* [ %3, %11 ], [ %29, %26 ]
%21 = phi i32 [ 7, %11 ], [ %27, %26 ]
%22 = call i32 @write(i32 %7, i8* %20, i32 %21)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = sub nsw i32 %21, %22
%28 = zext i32 %22 to i64
%29 = getelementptr inbounds i8, i8* %20, i64 %28
%30 = icmp sgt i32 %27, 0
br i1 %30, label %19, label %31
31: ; preds = %26, %37
%32 = phi i8* [ %40, %37 ], [ %3, %26 ]
%33 = phi i32 [ %38, %37 ], [ 7, %26 ]
%34 = call i32 @write(i32 %7, i8* %32, i32 %33)
switch i32 %34, label %37 [
i32 -1, label %35
i32 0, label %42
]
35: ; preds = %31
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %31
%38 = sub nsw i32 %33, %34
%39 = sext i32 %34 to i64
%40 = getelementptr inbounds i8, i8* %32, i64 %39
%41 = icmp sgt i32 %38, 0
br i1 %41, label %31, label %42
42: ; preds = %31, %37
%43 = call i32 @close(i32 %7)
%44 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%45 = call i32 @write(i32 1, i8* %3, i32 0)
%46 = call i32 @exit(i32 0)
unreachable
}