#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 socklen_t;
typedef int /*<<< orphan*/ ip;
typedef int /*<<< orphan*/ cliaddr;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc != 3)
    err_quit("usage:./tcp_client ip port");
  int ret1, ret2;
  int sockfd;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    err_sys("socket error");
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(atoi(argv[2]));
  inet_pton(AF_INET, argv[1], &addr.sin_addr);
  ret1 = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  if (ret1 < 0)
    err_sys("bind error");
  ret2 = listen(sockfd, 2);
  if (ret2 < 0)
    err_sys("listen error");
  for (;;) {
    int clifd;
    char ip[16];
    struct sockaddr_in cliaddr;
    socklen_t len = sizeof(cliaddr);
    clifd = accept(sockfd, (struct sockaddr *)&cliaddr, &len);
    inet_ntop(AF_INET, &cliaddr.sin_addr, ip, sizeof(ip));
    printf("connect with ip %s  port %d\n", ip, ntohs(cliaddr.sin_port));
    for (;;) {
      int n;
      char buffer[1024];
      n = recv(clifd, buffer, sizeof(buffer), 0);
      if (n < 0)
        break;
      if (n == 0) {
        close(clifd);
        printf("disconnect\n");
        break;
      } else {
        buffer[n] = '\0';
        printf("%s\n", buffer);
      }
    }
  }
  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)
je     120f <main+0x2f>
lea    0xdf6(%rip),%rdi
call   1090 <err_quit@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1241 <main+0x61>
lea    0xddf(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   10a0 <inet_pton@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12ac <main+0xcc>
lea    0xd81(%rip),%rdi
call   1050 <err_sys@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   10b0 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d2 <main+0xf2>
lea    0xd66(%rip),%rdi
call   1050 <err_sys@plt>
jmp    12d7 <main+0xf7>
movl   $0xc,-0x54(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x50(%rbp),%rsi
lea    -0x54(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
add    $0x4,%rsi
lea    -0x40(%rbp),%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x50(%rbp),%edi
call   10c0 <ntohs@plt>
mov    -0x468(%rbp),%rsi
mov    %eax,%edx
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x2c(%rbp),%edi
lea    -0x460(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    1362 <main+0x182>
jmp    13ad <main+0x1cd>
cmpl   $0x0,-0x58(%rbp)
jne    1387 <main+0x1a7>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13ad <main+0x1cd>
movslq -0x58(%rbp),%rax
movb   $0x0,-0x460(%rbp,%rax,1)
lea    -0x460(%rbp),%rsi
lea    0xcc8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    133a <main+0x15a>
jmp    12d7 <main+0xf7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
je     1215 <main+0x25>
lea    0xdf0(%rip),%rdi
call   10a0 <err_quit@plt>
mov    0x2dcc(%rip),%r14
mov    (%r14),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1241 <main+0x51>
lea    0xddf(%rip),%rdi
call   1060 <err_sys@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x10(%rbx),%rdi
call   1100 <atoi@plt>
mov    %eax,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r14),%edi
mov    0x8(%rbx),%rsi
lea    0x14(%rsp),%rdx
call   10b0 <inet_pton@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10e0 <bind@plt>
test   %eax,%eax
jns    128e <main+0x9e>
lea    0xd9f(%rip),%rdi
call   1060 <err_sys@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   10c0 <listen@plt>
test   %eax,%eax
jns    12aa <main+0xba>
lea    0xd8e(%rip),%rdi
call   1060 <err_sys@plt>
lea    0xc(%rsp),%r12
lea    0x30(%rsp),%rbx
lea    0xd85(%rip),%r13
lea    0x40(%rsp),%r14
jmp    12e4 <main+0xf4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
call   1090 <close@plt>
lea    0xd7e(%rip),%rdi
call   1050 <puts@plt>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
lea    0x20(%rsp),%rsi
mov    %r12,%rdx
call   10f0 <accept@plt>
mov    %eax,%r15d
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    0x24(%rsp),%rsi
mov    %rbx,%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    0x20(%rsp),%edi
call   10d0 <ntohs@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
js     12e4 <main+0xf4>
test   %eax,%eax
je     12d0 <main+0xe0>
mov    %eax,%eax
movb   $0x0,0x40(%rsp,%rax,1)
mov    %r14,%rdi
call   1050 <puts@plt>
jmp    1340 <main+0x150>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"usage:./tcp_client ip port\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
@.str.4 = [29 x i8] c"connect with ip %s port %d\0A\00"
@.str.5 = [12 x i8] c"disconnect\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca [16 x i8]
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i32
%15 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @err_quit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* @AF_INET
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%42 = call i32 @inet_pton(i32 %37, i8* %40, i32* %41)
%43 = load i32, i32* %8
%44 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%45 = call i32 @bind(i32 %43, %struct.sockaddr* %44, i32 12)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %28
%49 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %28
%51 = load i32, i32* %8
%52 = call i32 @listen(i32 %51, i32 2)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %57
57: ; preds = %55, %50
br label %58
58: ; preds = %92, %57
store i32 12, i32* %13
%59 = load i32, i32* %8
%60 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%61 = call i32 @accept(i32 %59, %struct.sockaddr* %60, i32* %13)
store i32 %61, i32* %10
%62 = load i32, i32* @AF_INET
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%64 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%65 = call i32 @inet_ntop(i32 %62, i32* %63, i8* %64, i32 16)
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @ntohs(i32 %68)
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %66, i32 %69)
br label %71
71: ; preds = %91, %58
%72 = load i32, i32* %10
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%74 = call i32 @recv(i32 %72, i8* %73, i32 1024, i32 0)
store i32 %74, i32* %14
%75 = load i32, i32* %14
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %71
br label %92
78: ; preds = %71
%79 = load i32, i32* %14
%80 = icmp eq i32 %79, 0
br i1 %80, label %81, label %85
81: ; preds = %78
%82 = load i32, i32* %10
%83 = call i32 @close(i32 %82)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %92
85: ; preds = %78
%86 = load i32, i32* %14
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 %87
store i8 0, i8* %88
%89 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %89)
br label %91
91: ; preds = %85
br label %71
92: ; preds = %81, %77
br label %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"usage:./tcp_client ip port\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
@.str.4 = [29 x i8] c"connect with ip %s port %d\0A\00"
@str = [11 x i8] c"disconnect\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [16 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = icmp eq i32 %0, 3
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @err_quit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = bitcast %struct.sockaddr_in* %3 to i8*
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %20, i32* %21
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32 @atoi(i8* %23)
%25 = tail call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%31 = call i32 @inet_pton(i32 %27, i8* %29, i32* %30)
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @bind(i32 %14, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %18
%36 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %18
%38 = call i32 @listen(i32 %14, i32 2)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%44 = bitcast %struct.sockaddr_in* %5 to i8*
%45 = bitcast i32* %6 to i8*
%46 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
br label %50
50: ; preds = %71, %42
store i32 12, i32* %6
%51 = call i32 @accept(i32 %14, %struct.sockaddr* %46, i32* %6)
%52 = load i32, i32* @AF_INET
%53 = call i32 @inet_ntop(i32 %52, i32* %47, i8* %43, i32 16)
%54 = load i32, i32* %48
%55 = call i32 @ntohs(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32 %55)
%57 = call i32 @recv(i32 %51, i8* %49, i32 1024, i32 0)
%58 = icmp slt i32 %57, 0
br i1 %58, label %71, label %59
59: ; preds = %50, %65
%60 = phi i32 [ %69, %65 ], [ %57, %50 ]
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = call i32 @close(i32 %51)
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %71
65: ; preds = %59
%66 = zext i32 %60 to i64
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %66
store i8 0, i8* %67
%68 = call i32 @puts(i8* %49)
%69 = call i32 @recv(i32 %51, i8* %49, i32 1024, i32 0)
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %59
71: ; preds = %65, %50, %62
br label %50
}