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

/* Variables and functions */
void *AF_INET;
int INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
void *htons(int);
int /*<<< orphan*/ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int recv(int, char *, int, int /*<<< orphan*/);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int servfd, clifd, length = 0;
  struct sockaddr_in servaddr, cliaddr;
  socklen_t socklen = sizeof(servaddr);
  char buf[255];
  if (argc < 2) {
    usage(argv[0]);
    exit(1);
  }
  if ((clifd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf(" create socket error!\n ");
    exit(1);
  }
  srand(time(NULL));
  bzero(&cliaddr, sizeof(cliaddr));
  cliaddr.sin_family = AF_INET;
  cliaddr.sin_port = htons(((20001 + rand()) % 65536));
  cliaddr.sin_addr.s_addr = htons(INADDR_ANY);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  inet_aton(argv[1], &servaddr.sin_addr);
  servaddr.sin_port = htons(20000);
  if (bind(clifd, (struct sockaddr *)&cliaddr, sizeof(cliaddr)) < 0) {
    printf(" bind to port %d failure!\n ", ((20001 + rand()) % 65536));
    exit(1);
  }
  if (connect(clifd, (struct sockaddr *)&servaddr, socklen) < 0) {
    printf(" can't connect to %s!\n ", argv[1]);
    exit(1);
  }
  length = recv(clifd, buf, 255, 0);
  if (length < 0) {
    printf(" error comes when recieve data from server %s! ", argv[1]);
    exit(1);
  }
  buf[length] = 0;
  printf(" from server %s :\n\t%s", argv[1], buf);
  close(clifd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x18,-0x54(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1227 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <usage@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%rdi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1265 <main+0x85>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
lea    -0x50(%rbp),%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   10f0 <rand@plt>
add    $0x4e21,%eax
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1040 <htons@plt>
mov    %rax,-0x50(%rbp)
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x48(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x38(%rbp),%rsi
add    $0x8,%rsi
call   1090 <inet_aton@plt>
mov    $0x4e20,%edi
call   1040 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x18,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    134a <main+0x16a>
mov    $0x0,%al
call   10f0 <rand@plt>
add    $0x4e21,%eax
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
lea    0xcdf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    1383 <main+0x1a3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xcc2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x160(%rbp),%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    13c6 <main+0x1e6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xc97(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x160(%rbp,%rax,1)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x160(%rbp),%rdx
lea    0xc94(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x138,%rsp
mov    %rsi,%r15
cmp    $0x1,%edi
jle    133c <main+0x15c>
mov    0x2de8(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     134e <main+0x16e>
mov    %eax,%ebp
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
lea    0x18(%rsp),%r14
mov    %r14,%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    (%rbx),%rax
mov    %rax,0x28(%rsp)
xor    %eax,%eax
call   10f0 <rand@plt>
lea    0x4e21(%rax),%ecx
lea    0x14e20(%rax),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
and    $0xffff0000,%edx
neg    %edx
lea    (%rax,%rdx,1),%edi
add    $0x4e21,%edi
call   1040 <htons@plt>
mov    %rax,0x18(%rsp)
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,0x20(%rsp)
mov    %rsp,%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    (%rbx),%rax
mov    %rax,0x10(%rsp)
mov    0x8(%r15),%rdi
lea    0x8(%rsp),%rsi
call   1090 <inet_aton@plt>
mov    $0x4e20,%edi
call   1040 <htons@plt>
mov    %rax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     1366 <main+0x186>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     13a7 <main+0x1c7>
lea    0x30(%rsp),%rsi
mov    %ebp,%edi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     13b4 <main+0x1d4>
mov    %eax,%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    0x8(%r15),%rsi
lea    0xd63(%rip),%rdi
lea    0x30(%rsp),%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%rdi
call   10a0 <usage@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xcab(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %eax,%eax
call   10f0 <rand@plt>
lea    0x4e21(%rax),%ecx
lea    0x14e20(%rax),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
and    $0xffff0000,%edx
neg    %edx
lea    (%rax,%rdx,1),%esi
add    $0x4e21,%esi
lea    0xc82(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x8(%r15),%rsi
lea    0xc82(%rip),%rdi
jmp    13bf <main+0x1df>
mov    0x8(%r15),%rsi
lea    0xc8d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c" create socket error!\0A \00"
@INADDR_ANY = global i32 0
@.str.1 = [28 x i8] c" bind to port %d failure!\0A \00"
@.str.2 = [24 x i8] c" can't connect to %s!\0A \00"
@.str.3 = [48 x i8] c" error comes when recieve data from server %s! \00"
@.str.4 = [22 x i8] c" from server %s :\0A\09%s\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 %struct.sockaddr_in
%11 = alloca i32
%12 = alloca [255 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 24, i32* %11
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @usage(i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8*, i8** @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i8* %22, i32 %23, i32 0)
store i32 %24, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = call i32 @time(i32* null)
%31 = call i32 @srand(i32 %30)
%32 = call i32 @bzero(%struct.sockaddr_in* %10, i32 24)
%33 = load i8*, i8** @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i8* %33, i8** %34
%35 = call i32 (...) @rand()
%36 = add nsw i32 20001, %35
%37 = srem i32 %36, 65536
%38 = call i8* @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i8* %38, i8** %39
%40 = load i32, i32* @INADDR_ANY
%41 = call i8* @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
store i8* %41, i8** %43
%44 = call i32 @bzero(%struct.sockaddr_in* %9, i32 24)
%45 = load i8*, i8** @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i8* %45, i8** %46
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%51 = call i32 @inet_aton(i8* %49, %struct.TYPE_2__* %50)
%52 = call i8* @htons(i32 20000)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i8* %52, i8** %53
%54 = load i32, i32* %7
%55 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%56 = call i64 @bind(i32 %54, %struct.sockaddr* %55, i32 24)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %29
%59 = call i32 (...) @rand()
%60 = add nsw i32 20001, %59
%61 = srem i32 %60, 65536
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %61)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %29
%65 = load i32, i32* %7
%66 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%67 = load i32, i32* %11
%68 = call i64 @connect(i32 %65, %struct.sockaddr* %66, i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %76
70: ; preds = %64
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 1
%73 = load i8*, i8** %72
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %64
%77 = load i32, i32* %7
%78 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%79 = call i32 @recv(i32 %77, i8* %78, i32 255, i32 0)
store i32 %79, i32* %8
%80 = load i32, i32* %8
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %88
82: ; preds = %76
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 1
%85 = load i8*, i8** %84
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %85)
%87 = call i32 @exit(i32 1)
unreachable
88: ; preds = %76
%89 = load i32, i32* %8
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 %90
store i8 0, i8* %91
%92 = load i8**, i8*** %5
%93 = getelementptr inbounds i8*, i8** %92, i64 1
%94 = load i8*, i8** %93
%95 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %94, i8* %95)
%97 = load i32, i32* %7
%98 = call i32 @close(i32 %97)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c" create socket error!\0A \00"
@INADDR_ANY = global i32 0
@.str.1 = [28 x i8] c" bind to port %d failure!\0A \00"
@.str.2 = [24 x i8] c" can't connect to %s!\0A \00"
@.str.3 = [48 x i8] c" error comes when recieve data from server %s! \00"
@.str.4 = [22 x i8] c" from server %s :\0A\09%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [255 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 @usage(i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8*, i8** @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i8* %15, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = tail call i32 @time(i32* null)
%24 = tail call i32 @srand(i32 %23)
%25 = call i32 @bzero(%struct.sockaddr_in* %4, i32 24)
%26 = load i8*, i8** @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i8* %26, i8** %27
%28 = tail call i32 (...) @rand()
%29 = add nsw i32 %28, 20001
%30 = srem i32 %29, 65536
%31 = tail call i8* @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i8* %31, i8** %32
%33 = load i32, i32* @INADDR_ANY
%34 = tail call i8* @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i8* %34, i8** %35
%36 = call i32 @bzero(%struct.sockaddr_in* %3, i32 24)
%37 = load i8*, i8** @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i8* %37, i8** %38
%39 = getelementptr inbounds i8*, i8** %1, i64 1
%40 = load i8*, i8** %39
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%42 = call i32 @inet_aton(i8* %40, %struct.TYPE_2__* %41)
%43 = call i8* @htons(i32 20000)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i8* %43, i8** %44
%45 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%46 = call i64 @bind(i32 %17, %struct.sockaddr* %45, i32 24)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %22
%49 = call i32 (...) @rand()
%50 = add nsw i32 %49, 20001
%51 = srem i32 %50, 65536
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %22
%55 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%56 = call i64 @connect(i32 %17, %struct.sockaddr* %55, i32 24)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = load i8*, i8** %39
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %54
%63 = call i32 @recv(i32 %17, i8* %8, i32 255, i32 0)
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %62
%66 = load i8*, i8** %39
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %66)
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %62
%70 = zext i32 %63 to i64
%71 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 %70
store i8 0, i8* %71
%72 = load i8*, i8** %39
%73 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %72, i8* %8)
%74 = call i32 @close(i32 %17)
ret i32 0
}