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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ getchar();
int htons(unsigned short);
int inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int cfd;
  int recbytes;
  int sin_size;
  char buffer[1024] = {0};
  struct sockaddr_in s_add, c_add;
  unsigned short portnum = 7274;
  printf("Hello,welcome to client !\r\n");
  cfd = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == cfd) {
    printf("socket fail ! \r\n");
    return -1;
  }
  printf("socket ok !\r\n");
  bzero(&s_add, sizeof(struct sockaddr_in));
  s_add.sin_family = AF_INET;
  s_add.sin_addr.s_addr = inet_addr("192.168.1.2");
  s_add.sin_port = htons(portnum);
  printf("s_addr = %#x ,port : %#x\r\n", s_add.sin_addr.s_addr, s_add.sin_port);
  if (-1 ==
      connect(cfd, (struct sockaddr *)(&s_add), sizeof(struct sockaddr))) {
    printf("connect fail !\r\n");
    return -1;
  }
  printf("connect ok !\r\n");
  if (-1 == (recbytes = read(cfd, buffer, 1024))) {
    printf("read data fail !\r\n");
    return -1;
  }
  printf("read ok\r\nREC:\r\n");
  buffer[recbytes] = '\0';
  printf("%s\r\n", buffer);
  getchar();
  close(cfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
movw   $0x1c6a,-0x432(%rbp)
6a 1c
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1220 <main+0x80>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <main+0x1bc>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
lea    0xde6(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x41c(%rbp)
movzwl -0x432(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x420(%rbp)
mov    -0x41c(%rbp),%esi
mov    -0x420(%rbp),%edx
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12c9 <main+0x129>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <main+0x1bc>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1317 <main+0x177>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <main+0x1bc>
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <getchar@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe59(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e09(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c2 <main+0x112>
mov    %eax,%ebx
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
lea    0xdd9(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,%ebp
mov    %eax,0x4(%rsp)
mov    $0x1c6a,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
lea    0xdc6(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
je     12cb <main+0x11b>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     12d4 <main+0x124>
mov    %eax,%ebp
lea    0xdd2(%rip),%rdi
call   1030 <puts@plt>
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd83(%rip),%rdi
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    12e5 <main+0x135>
lea    0xdca(%rip),%rdi
jmp    12db <main+0x12b>
lea    0xdb1(%rip),%rdi
jmp    12db <main+0x12b>
lea    0xd96(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [28 x i8] c"Hello,welcome to client !\0D\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"socket fail ! \0D\0A\00"
@.str.2 = [14 x i8] c"socket ok !\0D\0A\00"
@.str.3 = [12 x i8] c"192.168.1.2\00"
@.str.4 = [27 x i8] c"s_addr = %#x ,port : %#x\0D\0A\00"
@.str.5 = [17 x i8] c"connect fail !\0D\0A\00"
@.str.6 = [15 x i8] c"connect ok !\0D\0A\00"
@.str.7 = [19 x i8] c"read data fail !\0D\0A\00"
@.str.8 = [16 x i8] c"read ok\0D\0AREC:\0D\0A\00"
@.str.9 = [5 x i8] c"%s\0D\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i16
store i32 0, i32* %1
%9 = bitcast [1024 x i8]* %5 to i8*
store i16 7274, i16* %8
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @inet_addr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i16, i16* %8
%27 = call i32 @htons(i16 zeroext %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %31, i32 %33)
%35 = load i32, i32* %2
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 4)
%38 = icmp eq i32 -1, %37
br i1 %38, label %39, label %41
39: ; preds = %18
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
41: ; preds = %18
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%43 = load i32, i32* %2
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 1024)
store i32 %45, i32* %3
%46 = icmp eq i32 -1, %45
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
49: ; preds = %41
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %52
store i8 0, i8* %53
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i8* %54)
%56 = call i32 (...) @getchar()
%57 = load i32, i32* %2
%58 = call i32 @close(i32 %57)
store i32 0, i32* %1
br label %59
59: ; preds = %49, %47, %39, %16
%60 = load i32, i32* %1
ret i32 %60
}

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.3 = [12 x i8] c"192.168.1.2\00"
@.str.4 = [27 x i8] c"s_addr = %#x ,port : %#x\0D\0A\00"
@.str.9 = [5 x i8] c"%s\0D\0A\00"
@str = [27 x i8] c"Hello,welcome to client !\0D\00"
@str.10 = [13 x i8] c"socket ok !\0D\00"
@str.11 = [14 x i8] c"connect ok !\0D\00"
@str.12 = [15 x i8] c"read ok\0D\0AREC:\0D\00"
@str.13 = [18 x i8] c"read data fail !\0D\00"
@str.14 = [16 x i8] c"connect fail !\0D\00"
@str.15 = [16 x i8] c"socket fail ! \0D\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 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.15, i64 0, i64 0))
br label %40
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
%14 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @inet_addr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i16 zeroext 7274)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %19, i32* %20
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %17, i32 %19)
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @connect(i32 %8, %struct.sockaddr* %22, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %12
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.14, i64 0, i64 0))
br label %40
27: ; preds = %12
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
%29 = call i32 @read(i32 %8, i8* %3, i32 1024)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.13, i64 0, i64 0))
br label %40
33: ; preds = %27
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.12, i64 0, i64 0))
%35 = sext i32 %29 to i64
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %35
store i8 0, i8* %36
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i8* %3)
%38 = call i32 (...) @getchar()
%39 = call i32 @close(i32 %8)
br label %40
40: ; preds = %33, %31, %25, %10
%41 = phi i32 [ -1, %10 ], [ -1, %25 ], [ -1, %31 ], [ 0, %33 ]
ret i32 %41
}