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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ handle_connection(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd;
  char char_port[30];
  struct sockaddr_in servaddr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(8787);
  inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
  int retval = 0;
  retval = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (retval < 0) {
    fprintf(stderr, "connect fail,error:%s\n", strerror(errno));
    return -1;
  }
  printf("client send to server .\n");
  sprintf(char_port, "----->  hello server");
  write(sockfd, char_port, 32);
  handle_connection(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    $0x2253,%edi
call   1040 <htons@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rdx
lea    0xde3(%rip),%rsi
call   1080 <inet_pton@plt>
movl   $0x0,-0x54(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    127f <main+0xcf>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xd9c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <main+0x10b>
lea    0xd9b(%rip),%rdi
call   1050 <printf@plt>
lea    -0x40(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <handle_connection@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e37(%rip),%r15
mov    (%r15),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0x2253,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edi
lea    0xe08(%rip),%rsi
mov    %rbx,%rdx
call   1080 <inet_pton@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     1254 <main+0xb4>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
movabs $0x72657672657320,%rax
mov    %rax,0x1d(%rsp)
movups 0xdec(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x20,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <handle_connection@plt>
jmp    1282 <main+0xe2>
mov    0x2d7d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd98(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"connect fail,error:%s\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"client send to server .\0A\00"
@.str.3 = [21 x i8] c"-----> hello server\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [30 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%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* %6
%13 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 8787)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%20 = call i32 @inet_pton(i32 %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %19)
store i32 0, i32* %9
%21 = load i32, i32* %6
%22 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%23 = call i32 @connect(i32 %21, %struct.sockaddr* %22, i32 12)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %2
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 -1, i32* %3
br label %40
31: ; preds = %2
%32 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%33 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%34 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* %6
%36 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%37 = call i32 @write(i32 %35, i8* %36, i32 32)
%38 = load i32, i32* %6
%39 = call i32 @handle_connection(i32 %38)
store i32 0, i32* %3
br label %40
40: ; preds = %31, %26
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"connect fail,error:%s\0A\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"-----> hello server\00"
@str = [24 x i8] c"client send to server .\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [30 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 8787)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%17 = call i32 @inet_pton(i32 %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %16)
%18 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%19 = call i32 @connect(i32 %9, %struct.sockaddr* %18, i32 12)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %30
26: ; preds = %2
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%28 = call i32 @write(i32 %9, i8* %5, i32 32)
%29 = call i32 @handle_connection(i32 %9)
br label %30
30: ; preds = %26, %21
%31 = phi i32 [ -1, %21 ], [ 0, %26 ]
ret i32 %31
}