#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 timeval {
  int member_0;
  int /*<<< orphan*/ member_1;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ srv_addr;
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int STDIN_FILENO;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 3) {
    fprintf(stderr, "缺少参数\n");
    printf("Usage: %s ip port\n", argv[0]);
    exit(-1);
  }
  char *srv_ip = argv[1];
  char *srv_port = argv[2];
  char *msg = argv[3];
  int iret;
  int sock_fd = socket(PF_INET, SOCK_STREAM, 0);
  if (sock_fd == -1) {
    do {
      perror("socket");
      exit(-1);
    } while (0);
  }
  printf("socket ok, sock_fd:%d\n", sock_fd);
  struct sockaddr_in srv_addr;
  socklen_t srv_addr_len = sizeof(srv_addr);
  bzero(&srv_addr, sizeof(srv_addr));
  srv_addr.sin_family = AF_INET;
  inet_aton(srv_ip, &srv_addr.sin_addr);
  srv_addr.sin_port = htons(atoi(srv_port));
  int conn_fd =
      connect(sock_fd, (const struct sockaddr *)&srv_addr, srv_addr_len);
  if (conn_fd == -1) {
    do {
      perror("connect");
      exit(-1);
    } while (0);
  }
  printf("connect is ok, conn_fd = %d\n", conn_fd);
  char buf[1024] = {0};
  int n;
  fd_set rd_set;
  int nfds;
  while (1) {
    FD_ZERO(&rd_set);
    FD_SET(STDIN_FILENO, &rd_set);
    FD_SET(sock_fd, &rd_set);
    nfds = sock_fd;
    struct timeval tv = {1, 0};
    n = select(nfds + 1, &rd_set, NULL, NULL, &tv);
    if (n < 0) {
      perror("select");
      break;
    } else if (n == 0) {
      continue;
    }
    if (FD_ISSET(STDIN_FILENO, &rd_set)) {
      if (fgets(buf, sizeof(buf) - 1, stdin) != NULL) {
        n = strlen(buf);
        buf[n - 1] = '\0';
        write(sock_fd, buf, n - 1);
        if (strcmp(buf, "exit") == 0) {
          close(sock_fd);
          break;
        }
      }
    }
    if (FD_ISSET(sock_fd, &rd_set)) {
      n = read(sock_fd, buf, sizeof(buf) - 1);
      if (n == 0) {
        printf("connect close by peer\n");
        close(sock_fd);
        break;
      }
      printf("server:%s(%d)\n", buf, n);
    }
  }
  close(sock_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    12a7 <main+0x57>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
call   10d0 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0xffffffff,%edi
call   1140 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
mov    0x2ced(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1170 <socket@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    130c <main+0xbc>
jmp    12f6 <main+0xa6>
lea    0xd24(%rip),%rdi
call   1110 <perror@plt>
mov    $0xffffffff,%edi
call   1140 <exit@plt>
mov    -0x30(%rbp),%esi
lea    0xd12(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xc,-0x44(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0xc,%esi
call   1120 <bzero@plt>
mov    0x2caf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rsi
add    $0x4,%rsi
call   1100 <inet_aton@plt>
mov    -0x20(%rbp),%rdi
call   1130 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1150 <connect@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    1399 <main+0x149>
jmp    1383 <main+0x133>
lea    0xcb5(%rip),%rdi
call   1110 <perror@plt>
mov    $0xffffffff,%edi
call   1140 <exit@plt>
mov    -0x48(%rbp),%esi
lea    0xca4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x450(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x458(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
mov    0x2be8(%rip),%rax
mov    (%rax),%edi
lea    -0x458(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    -0x30(%rbp),%edi
lea    -0x458(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x45c(%rbp)
mov    0xc9b(%rip),%rax
mov    %rax,-0x468(%rbp)
mov    -0x45c(%rbp),%edi
add    $0x1,%edi
lea    -0x458(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x468(%rbp),%r8
mov    %rcx,%rdx
call   10f0 <select@plt>
mov    %eax,-0x454(%rbp)
cmpl   $0x0,-0x454(%rbp)
jge    144b <main+0x1fb>
lea    0xc23(%rip),%rdi
call   1110 <perror@plt>
jmp    1591 <main+0x341>
cmpl   $0x0,-0x454(%rbp)
jne    145d <main+0x20d>
jmp    13bd <main+0x16d>
jmp    1462 <main+0x212>
mov    0x2b4f(%rip),%rax
mov    (%rax),%edi
lea    -0x458(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1516 <main+0x2c6>
lea    -0x450(%rbp),%rdi
mov    0x2b51(%rip),%rax
mov    (%rax),%edx
mov    $0x3ff,%esi
call   10b0 <fgets@plt>
cmp    $0x0,%rax
je     1511 <main+0x2c1>
lea    -0x450(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x454(%rbp)
mov    -0x454(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x450(%rbp,%rax,1)
mov    -0x30(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    -0x454(%rbp),%edx
sub    $0x1,%edx
call   1030 <write@plt>
lea    -0x450(%rbp),%rdi
lea    0xb7b(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    150c <main+0x2bc>
mov    -0x30(%rbp),%edi
call   1090 <close@plt>
jmp    1591 <main+0x341>
jmp    1511 <main+0x2c1>
jmp    1516 <main+0x2c6>
mov    -0x30(%rbp),%edi
lea    -0x458(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     158c <main+0x33c>
mov    -0x30(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x3ff,%edx
call   10a0 <read@plt>
mov    %eax,-0x454(%rbp)
cmpl   $0x0,-0x454(%rbp)
jne    1571 <main+0x321>
lea    0xb13(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%edi
call   1090 <close@plt>
jmp    1591 <main+0x341>
lea    -0x450(%rbp),%rsi
mov    -0x454(%rbp),%edx
lea    0xb02(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13bd <main+0x16d>
mov    -0x30(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    146b <main+0x20b>
mov    0x8(%rbx),%rbp
mov    0x10(%rbx),%r14
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1180 <socket@plt>
cmp    $0xffffffff,%eax
je     149b <main+0x23b>
mov    %eax,%ebx
lea    0xd78(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1130 <bzero@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rsi
mov    %rbp,%rdi
call   1110 <inet_aton@plt>
mov    %r14,%rdi
call   1140 <atoi@plt>
mov    %eax,%edi
call   1070 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1160 <connect@plt>
cmp    $0xffffffff,%eax
je     14a4 <main+0x244>
lea    0xd33(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0x20(%rsp),%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <memset@plt>
mov    %ebx,%r15d
add    $0x1,%r15d
lea    0x1c(%rsp),%rbp
mov    0x2c74(%rip),%r13
lea    0x10(%rsp),%r12
nopl   0x0(%rax)
mov    %rbp,%rdi
call   10f0 <FD_ZERO@plt>
mov    0x0(%r13),%edi
mov    %rbp,%rsi
call   1170 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1170 <FD_SET@plt>
movq   $0x1,0x10(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1100 <select@plt>
test   %eax,%eax
js     145d <main+0x1fd>
je     1350 <main+0xf0>
mov    0x0(%r13),%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     13f1 <main+0x191>
mov    0x2c35(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x3ff,%esi
call   10c0 <fgets@plt>
test   %rax,%rax
je     13f1 <main+0x191>
mov    %r14,%rdi
call   1060 <strlen@plt>
movslq %eax,%rdx
movb   $0x0,0x1f(%rsp,%rdx,1)
add    $0xffffffffffffffff,%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <write@plt>
mov    %r14,%rdi
lea    0xc84(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     143b <main+0x1db>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1350 <main+0xf0>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   10b0 <read@plt>
test   %eax,%eax
je     142f <main+0x1cf>
lea    0xc52(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1350 <main+0xf0>
lea    0xc49(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc00(%rip),%rdi
call   1120 <perror@plt>
jmp    1442 <main+0x1e2>
mov    0x2b5e(%rip),%rax
mov    (%rax),%edi
lea    0xb85(%rip),%rsi
call   10e0 <fprintf@plt>
mov    (%rbx),%rsi
lea    0xb84(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xffffffff,%edi
call   1150 <exit@plt>
lea    0xb7f(%rip),%rdi
jmp    14ab <main+0x24b>
lea    0xb94(%rip),%rdi
call   1120 <perror@plt>
mov    $0xffffffff,%edi
call   1150 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [14 x i8] c"\E7\BC\BA\E5\B0\91\E5\8F\82\E6\95\B0\0A\00"
@.str.1 = [19 x i8] c"Usage: %s ip port\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [23 x i8] c"socket ok, sock_fd:%d\0A\00"
@AF_INET = global i32 0
@.str.4 = [8 x i8] c"connect\00"
@.str.5 = [29 x i8] c"connect is ok, conn_fd = %d\0A\00"
@STDIN_FILENO = global i32 0
@__const.main.tv = %struct.timeval { i32 1, i32 0 }
@.str.6 = [7 x i8] c"select\00"
@stdin = global i32 0
@.str.7 = [5 x i8] c"exit\00"
@.str.8 = [23 x i8] c"connect close by peer\0A\00"
@.str.9 = [15 x i8] c"server:%s(%d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca i32
%14 = alloca [1024 x i8]
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %29
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %2
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
store i8* %32, i8** %6
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
store i8* %35, i8** %7
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 3
%38 = load i8*, i8** %37
store i8* %38, i8** %8
%39 = load i32, i32* @PF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %29
br label %45
45: ; preds = %44
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; No predecessors!
br label %49
49: ; preds = %48, %29
%50 = load i32, i32* %10
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %50)
store i32 12, i32* %12
%52 = call i32 @bzero(%struct.sockaddr_in* %11, i32 12)
%53 = load i32, i32* @AF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %53, i32* %54
%55 = load i8*, i8** %6
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%57 = call i32 @inet_aton(i8* %55, i32* %56)
%58 = load i8*, i8** %7
%59 = call i32 @atoi(i8* %58)
%60 = call i32 @htons(i32 %59)
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %60, i32* %61
%62 = load i32, i32* %10
%63 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%64 = load i32, i32* %12
%65 = call i32 @connect(i32 %62, %struct.sockaddr* %63, i32 %64)
store i32 %65, i32* %13
%66 = load i32, i32* %13
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %73
68: ; preds = %49
br label %69
69: ; preds = %68
%70 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%71 = call i32 @exit(i32 -1)
unreachable
72: ; No predecessors!
br label %73
73: ; preds = %72, %49
%74 = load i32, i32* %13
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %74)
%76 = bitcast [1024 x i8]* %14 to i8*
br label %77
77: ; preds = %73, %95, %144
%78 = call i32 @FD_ZERO(i32* %16)
%79 = load i32, i32* @STDIN_FILENO
%80 = call i32 @FD_SET(i32 %79, i32* %16)
%81 = load i32, i32* %10
%82 = call i32 @FD_SET(i32 %81, i32* %16)
%83 = load i32, i32* %10
store i32 %83, i32* %17
%84 = bitcast %struct.timeval* %18 to i8*
%85 = load i32, i32* %17
%86 = add nsw i32 %85, 1
%87 = call i32 @select(i32 %86, i32* %16, i32* null, i32* null, %struct.timeval* %18)
store i32 %87, i32* %15
%88 = load i32, i32* %15
%89 = icmp slt i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %77
%91 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %145
92: ; preds = %77
%93 = load i32, i32* %15
%94 = icmp eq i32 %93, 0
br i1 %94, label %95, label %96
95: ; preds = %92
br label %77
96: ; preds = %92
br label %97
97: ; preds = %96
%98 = load i32, i32* @STDIN_FILENO
%99 = call i64 @FD_ISSET(i32 %98, i32* %16)
%100 = icmp ne i64 %99, 0
br i1 %100, label %101, label %126
101: ; preds = %97
%102 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%103 = load i32, i32* @stdin
%104 = call i32* @fgets(i8* %102, i32 1023, i32 %103)
%105 = icmp ne i32* %104, null
br i1 %105, label %106, label %125
106: ; preds = %101
%107 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%108 = call i32 @strlen(i8* %107)
store i32 %108, i32* %15
%109 = load i32, i32* %15
%110 = sub nsw i32 %109, 1
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 %111
store i8 0, i8* %112
%113 = load i32, i32* %10
%114 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%115 = load i32, i32* %15
%116 = sub nsw i32 %115, 1
%117 = call i32 @write(i32 %113, i8* %114, i32 %116)
%118 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%119 = call i64 @strcmp(i8* %118, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%120 = icmp eq i64 %119, 0
br i1 %120, label %121, label %124
121: ; preds = %106
%122 = load i32, i32* %10
%123 = call i32 @close(i32 %122)
br label %145
124: ; preds = %106
br label %125
125: ; preds = %124, %101
br label %126
126: ; preds = %125, %97
%127 = load i32, i32* %10
%128 = call i64 @FD_ISSET(i32 %127, i32* %16)
%129 = icmp ne i64 %128, 0
br i1 %129, label %130, label %144
130: ; preds = %126
%131 = load i32, i32* %10
%132 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%133 = call i32 @read(i32 %131, i8* %132, i32 1023)
store i32 %133, i32* %15
%134 = load i32, i32* %15
%135 = icmp eq i32 %134, 0
br i1 %135, label %136, label %140
136: ; preds = %130
%137 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
%138 = load i32, i32* %10
%139 = call i32 @close(i32 %138)
br label %145
140: ; preds = %130
%141 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%142 = load i32, i32* %15
%143 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0), i8* %141, i32 %142)
br label %144
144: ; preds = %140, %126
br label %77
145: ; preds = %136, %121, %90
%146 = load i32, i32* %10
%147 = call i32 @close(i32 %146)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [14 x i8] c"\E7\BC\BA\E5\B0\91\E5\8F\82\E6\95\B0\0A\00"
@.str.1 = [19 x i8] c"Usage: %s ip port\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [23 x i8] c"socket ok, sock_fd:%d\0A\00"
@AF_INET = global i32 0
@.str.4 = [8 x i8] c"connect\00"
@.str.5 = [29 x i8] c"connect is ok, conn_fd = %d\0A\00"
@STDIN_FILENO = global i32 0
@.str.6 = [7 x i8] c"select\00"
@stdin = global i32 0
@.str.7 = [5 x i8] c"exit\00"
@.str.9 = [15 x i8] c"server:%s(%d)\0A\00"
@str = [22 x i8] c"connect close by peer\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i64
%7 = bitcast i64* %6 to %struct.timeval*
%8 = icmp slt i32 %0, 3
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %1
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = tail call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 -1)
unreachable
27: ; preds = %15
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%29 = bitcast %struct.sockaddr_in* %3 to i8*
%30 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%34 = call i32 @inet_aton(i8* %17, i32* %33)
%35 = call i32 @atoi(i8* %19)
%36 = call i32 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %36, i32* %37
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i32 @connect(i32 %22, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %27
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %27
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%47 = bitcast i32* %5 to i8*
%48 = bitcast i64* %6 to i8*
%49 = add nuw nsw i32 %22, 1
br label %50
50: ; preds = %90, %44
%51 = call i32 @FD_ZERO(i32* %5)
%52 = load i32, i32* @STDIN_FILENO
%53 = call i32 @FD_SET(i32 %52, i32* %5)
%54 = call i32 @FD_SET(i32 %22, i32* %5)
store i64 1, i64* %6
%55 = call i32 @select(i32 %49, i32* %5, i32* null, i32* null, %struct.timeval* %7)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %91
59: ; preds = %50
%60 = icmp eq i32 %55, 0
br i1 %60, label %90, label %61
61: ; preds = %59
%62 = load i32, i32* @STDIN_FILENO
%63 = call i64 @FD_ISSET(i32 %62, i32* %5)
%64 = icmp eq i64 %63, 0
br i1 %64, label %79, label %65
65: ; preds = %61
%66 = load i32, i32* @stdin
%67 = call i32* @fgets(i8* %46, i32 1023, i32 %66)
%68 = icmp eq i32* %67, null
br i1 %68, label %79, label %69
69: ; preds = %65
%70 = call i32 @strlen(i8* %46)
%71 = add nsw i32 %70, -1
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %72
store i8 0, i8* %73
%74 = call i32 @write(i32 %22, i8* %46, i32 %71)
%75 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %69
%78 = call i32 @close(i32 %22)
br label %91
79: ; preds = %65, %69, %61
%80 = call i64 @FD_ISSET(i32 %22, i32* %5)
%81 = icmp eq i64 %80, 0
br i1 %81, label %90, label %82
82: ; preds = %79
%83 = call i32 @read(i32 %22, i8* %46, i32 1023)
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%87 = call i32 @close(i32 %22)
br label %91
88: ; preds = %82
%89 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0), i8* %46, i32 %83)
br label %90
90: ; preds = %79, %88, %59
br label %50
91: ; preds = %57, %77, %85
%92 = call i32 @close(i32 %22)
ret i32 0
}