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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int main() {
  int sock_desc;
  struct sockaddr_in serv_addr;
  char sbuff[255], rbuff[255];
  if ((sock_desc = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    printf("Failed creating socket\n");
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  serv_addr.sin_port = htons(8000);
  if (connect(sock_desc, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <
      0) {
    printf("Failed to connect to server\n");
    return -1;
  }
  printf("Connected successfully - Please enter string\n");
  while (fgets(sbuff, 255, stdin) != NULL) {
    send(sock_desc, sbuff, strlen(sbuff), 0);
    bzero(rbuff, 255);
    if (recv(sock_desc, sbuff, 255, 0) == 0)
      printf("Error");
    else
      printf("data receive: %s\n", sbuff);
    bzero(rbuff, 255);
  }
  close(sock_desc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f5 <main+0x45>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xe02(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    1260 <main+0xb0>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <main+0x199>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     133a <main+0x18a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x22c(%rbp),%edi
mov    -0x228(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
lea    -0x220(%rbp),%rdi
mov    $0xff,%esi
call   10b0 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jne    130f <main+0x15f>
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1324 <main+0x174>
lea    -0x120(%rbp),%rsi
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x220(%rbp),%rdi
mov    $0xff,%esi
call   10b0 <bzero@plt>
jmp    126e <main+0xbe>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    0x2e12(%rip),%r14
mov    (%r14),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fb <main+0x3b>
lea    0xe75(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
lea    0xde7(%rip),%rdi
call   10b0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x1f40,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     12f6 <main+0x136>
lea    0xdd4(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d86(%rip),%r12
mov    (%r12),%edx
lea    0x10(%rsp),%rdi
mov    $0xff,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     12eb <main+0x12b>
lea    0x10(%rsp),%rbx
lea    0xd8c(%rip),%r14
lea    0xd8b(%rip),%r15
jmp    12b3 <main+0xf3>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     12eb <main+0x12b>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jne    1290 <main+0xd0>
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    129d <main+0xdd>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
jmp    1307 <main+0x147>
lea    0xd52(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed creating socket\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [29 x i8] c"Failed to connect to server\0A\00"
@.str.3 = [46 x i8] c"Connected successfully - Please enter string\0A\00"
@stdin = global i32 0
@.str.4 = [6 x i8] c"Error\00"
@.str.5 = [18 x i8] c"data receive: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [255 x i8]
%5 = alloca [255 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = call i32 @bzero(i8* %13, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = call i32 @htons(i32 8000)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_in* %3 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 = %12
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %58
28: ; preds = %12
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %52, %28
%31 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%32 = load i32, i32* @stdin
%33 = call i32* @fgets(i8* %31, i32 255, i32 %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %55
35: ; preds = %30
%36 = load i32, i32* %2
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%38 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = call i32 @send(i32 %36, i8* %37, i32 %39, i32 0)
%41 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%42 = call i32 @bzero(i8* %41, i32 255)
%43 = load i32, i32* %2
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%45 = call i64 @recv(i32 %43, i8* %44, i32 255, i32 0)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %35
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %52
49: ; preds = %35
%50 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %47
%53 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%54 = call i32 @bzero(i8* %53, i32 255)
br label %30
55: ; preds = %30
%56 = load i32, i32* %2
%57 = call i32 @close(i32 %56)
store i32 0, i32* %1
br label %58
58: ; preds = %55, %26
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, 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"
@stdin = global i32 0
@.str.4 = [6 x i8] c"Error\00"
@.str.5 = [18 x i8] c"data receive: %s\0A\00"
@str = [45 x i8] c"Connected successfully - Please enter string\00"
@str.6 = [28 x i8] c"Failed to connect to server\00"
@str.7 = [23 x i8] c"Failed creating socket\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [255 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%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 slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = call i32 @bzero(i8* %3, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail 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* %1, i64 0, i32 1, i32 0
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 8000)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%20 = call i64 @connect(i32 %7, %struct.sockaddr* %19, i32 12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
br label %44
24: ; preds = %11
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* @stdin
%27 = call i32* @fgets(i8* %4, i32 255, i32 %26)
%28 = icmp eq i32* %27, null
br i1 %28, label %42, label %29
29: ; preds = %24, %38
%30 = call i32 @strlen(i8* %4)
%31 = call i32 @send(i32 %7, i8* %4, i32 %30, i32 0)
%32 = call i64 @recv(i32 %7, i8* %4, i32 255, i32 0)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %38
36: ; preds = %29
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %4)
br label %38
38: ; preds = %36, %34
%39 = load i32, i32* @stdin
%40 = call i32* @fgets(i8* %4, i32 255, i32 %39)
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %29
42: ; preds = %38, %24
%43 = call i32 @close(i32 %7)
br label %44
44: ; preds = %42, %22
%45 = phi i32 [ -1, %22 ], [ 0, %42 ]
ret i32 %45
}