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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("simple client usage -h\t help\n./clinet IP port\n");
    return -1;
  }
  char *ip_addr = argv[1];
  int port = atoi(argv[2]);
  struct sockaddr_in conn_addr;
  int conn_fd;
  conn_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == conn_fd) {
    printf("无法获取文件描述符\n");
    return -1;
  }
  printf("获取文件描述符成功:%d\n", conn_fd);
  bzero(&conn_addr, sizeof(conn_addr));
  conn_addr.sin_family = AF_INET;
  inet_aton(ip_addr, &conn_addr.sin_addr);
  conn_addr.sin_port = htons(port);
  printf("连接[%s:%d]\n", inet_ntoa(conn_addr.sin_addr),
         ntohs(conn_addr.sin_port));
  int result =
      connect(conn_fd, (struct sockaddr *)&conn_addr, sizeof(conn_addr));
  if (0 != result) {
    printf("连接失败,错误:%s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11da <main+0x3a>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <main+0x15a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    123a <main+0x9a>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <main+0x15a>
mov    -0x2c(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
call   1060 <inet_aton@plt>
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <ntohs@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%eax
je     12f3 <main+0x153>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <main+0x15a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jne    129e <main+0xee>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r15d
mov    0x2e0a(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12a7 <main+0xf7>
mov    %eax,%ebp
lea    0xdfa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
call   1070 <inet_aton@plt>
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0xc(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x8(%rsp),%edi
call   1080 <ntohs@plt>
lea    0xdc2(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %eax,%eax
je     12b8 <main+0x108>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd9d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12b3 <main+0x103>
lea    0xdbe(%rip),%rdi
jmp    12ae <main+0xfe>
lea    0xd99(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [47 x i8] c"simple client usage -h\09 help\0A./clinet IP port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [29 x i8] c"\E6\97\A0\E6\B3\95\E8\8E\B7\E5\8F\96\E6\96\87\E4\BB\B6\E6\8F\8F\E8\BF\B0\E7\AC\A6\0A\00"
@.str.2 = [32 x i8] c"\E8\8E\B7\E5\8F\96\E6\96\87\E4\BB\B6\E6\8F\8F\E8\BF\B0\E7\AC\A6\E6\88\90\E5\8A\9F:%d\0A\00"
@.str.3 = [15 x i8] c"\E8\BF\9E\E6\8E\A5[%s:%d]\0A\00"
@.str.4 = [24 x i8] c"\E8\BF\9E\E6\8E\A5\E5\A4\B1\E8\B4\A5,\E9\94\99\E8\AF\AF:%s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %6
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %7
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp eq i32 -1, %26
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
30: ; preds = %15
%31 = load i32, i32* %9
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i8*, i8** %6
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%38 = call i32 @inet_aton(i8* %36, i32* %37)
%39 = load i32, i32* %7
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%43 = load i32, i32* %42
%44 = call i8* @inet_ntoa(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %47)
%49 = load i32, i32* %9
%50 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%51 = call i32 @connect(i32 %49, %struct.sockaddr* %50, i32 12)
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = icmp ne i32 0, %52
br i1 %53, label %54, label %58
54: ; preds = %30
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %56)
store i32 -1, i32* %3
br label %59
58: ; preds = %30
store i32 0, i32* %3
br label %59
59: ; preds = %58, %54, %28, %13
%60 = load i32, i32* %3
ret i32 %60
}

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.2 = [32 x i8] c"\E8\8E\B7\E5\8F\96\E6\96\87\E4\BB\B6\E6\8F\8F\E8\BF\B0\E7\AC\A6\E6\88\90\E5\8A\9F:%d\0A\00"
@.str.3 = [15 x i8] c"\E8\BF\9E\E6\8E\A5[%s:%d]\0A\00"
@.str.4 = [24 x i8] c"\E8\BF\9E\E6\8E\A5\E5\A4\B1\E8\B4\A5,\E9\94\99\E8\AF\AF:%s\0A\00"
@errno = global i32 0
@str = [28 x i8] c"\E6\97\A0\E6\B3\95\E8\8E\B7\E5\8F\96\E6\96\87\E4\BB\B6\E6\8F\8F\E8\BF\B0\E7\AC\A6\00"
@str.5 = [46 x i8] c"simple client usage -h\09 help\0A./clinet IP port\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.5, i64 0, i64 0))
br label %43
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %41
20: ; preds = %7
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%22 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%26 = call i32 @inet_aton(i8* %9, i32* %25)
%27 = call i32 @htons(i32 %12)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %25
%30 = call i8* @inet_ntoa(i32 %29)
%31 = load i32, i32* %28
%32 = call i32 @ntohs(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %30, i32 %32)
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i32 @connect(i32 %16, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %20
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %37, %20, %18
%42 = phi i32 [ -1, %18 ], [ -1, %37 ], [ 0, %20 ]
br label %43
43: ; preds = %41, %5
%44 = phi i32 [ -1, %5 ], [ %42, %41 ]
ret i32 %44
}