#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ recvBuff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOF;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(void) {
  int sockfd = 0, n = 0;
  char recvBuff[1024];
  struct sockaddr_in serv_addr;
  memset(recvBuff, '0', sizeof(recvBuff));
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("\n Error : Could not create socket \n");
    return 1;
  }
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(8888);
  serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    printf("\n Error : Connect Failed \n");
    return 1;
  }
  while ((n = read(sockfd, recvBuff, sizeof(recvBuff) - 1)) > 0) {
    recvBuff[n] = 0;
    if (fputs(recvBuff, stdout) == EOF) {
      printf("\n Error : Fputs error");
    }
    printf("\n");
  }
  if (n < 0) {
    printf("\n Read Error \n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x30,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f3 <main+0x73>
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ea <main+0x16a>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    $0x22b8,%edi
call   1030 <htons@plt>
mov    %eax,-0x41c(%rbp)
lea    0xe0b(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x420(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    125a <main+0xda>
lea    0xde5(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ea <main+0x16a>
jmp    125f <main+0xdf>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    12cd <main+0x14d>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    0x2d39(%rip),%rcx
cmp    (%rcx),%rax
jne    12bc <main+0x13c>
lea    0xd92(%rip),%rdi
call   1040 <printf@plt>
lea    0xd84(%rip),%rdi
call   1040 <printf@plt>
jmp    125f <main+0xdf>
cmpl   $0x0,-0xc(%rbp)
jge    12e3 <main+0x163>
lea    0xd81(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
lea    0x10(%rsp),%rdi
mov    $0x30,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    0x2e1e(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0x1,%r14d
test   %eax,%eax
js     12af <main+0x10f>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xdf7(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     12b8 <main+0x118>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3ff,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    129f <main+0xff>
mov    0x2d83(%rip),%r15
lea    0x10(%rsp),%rbp
mov    0x2d8f(%rip),%r12
lea    0xdb2(%rip),%r14
jmp    127d <main+0xdd>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    129f <main+0xff>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    (%r15),%esi
mov    %rbp,%rdi
call   1070 <fputs@plt>
cmp    (%r12),%rax
jne    1260 <main+0xc0>
mov    %r14,%rdi
call   1060 <printf@plt>
jmp    1260 <main+0xc0>
xor    %r14d,%r14d
test   %eax,%eax
jns    12c4 <main+0x124>
lea    0xd73(%rip),%rdi
jmp    12bf <main+0x11f>
lea    0xd92(%rip),%rdi
jmp    12bf <main+0x11f>
lea    0xd6f(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"\0A Error : Could not create socket \0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [27 x i8] c"\0A Error : Connect Failed \0A\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.3 = [22 x i8] c"\0A Error : Fputs error\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [15 x i8] c"\0A Read Error \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca %struct.sockaddr_in
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 48, i32 1024)
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %53
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 8888)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %17, i32* %18
%19 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%24 = call i64 @connect(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %53
28: ; preds = %14
br label %29
29: ; preds = %45, %28
%30 = load i32, i32* %2
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 1023)
store i32 %32, i32* %3
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %29
%35 = load i32, i32* %3
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %36
store i8 0, i8* %37
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%39 = load i32, i32* @stdout
%40 = call i64 @fputs(i8* %38, i32 %39)
%41 = load i64, i64* @EOF
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %34
%46 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %29
47: ; preds = %29
%48 = load i32, i32* %3
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %52
52: ; preds = %50, %47
store i32 0, i32* %1
br label %53
53: ; preds = %52, %26, %12
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.3 = [22 x i8] c"\0A Error : Fputs error\00"
@str = [14 x i8] c"\0A Read Error \00"
@str.6 = [26 x i8] c"\0A Error : Connect Failed \00"
@str.7 = [35 x i8] c"\0A Error : Could not create socket \00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = call i32 @memset(i8* %3, i32 48, i32 1024)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %40, label %10
10: ; preds = %0
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 8888)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i64 @connect(i32 %8, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %40, label %20
20: ; preds = %10
%21 = call i32 @read(i32 %8, i8* %3, i32 1023)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %20, %33
%24 = phi i32 [ %35, %33 ], [ %21, %20 ]
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* @stdout
%28 = call i64 @fputs(i8* %3, i32 %27)
%29 = load i64, i64* @EOF
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %23
%34 = call i32 @putchar(i32 10)
%35 = call i32 @read(i32 %8, i8* %3, i32 1023)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %23, label %37
37: ; preds = %33, %20
%38 = phi i32 [ %21, %20 ], [ %35, %33 ]
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37, %10, %0
%41 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.7, i64 0, i64 0), %0 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0), %10 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0), %37 ]
%42 = phi i32 [ 1, %0 ], [ 1, %10 ], [ 0, %37 ]
%43 = call i32 @puts(i8* dereferenceable(1) %41)
br label %44
44: ; preds = %40, %37
%45 = phi i32 [ 0, %37 ], [ %42, %40 ]
ret i32 %45
}