#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;
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);
  }
  while (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 (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);
    }
    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    1217 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <usage@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    121c <main+0x4c>
mov    0x2dc5(%rip),%rax
mov    (%rax),%rdi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    125a <main+0x8a>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <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   10b0 <bzero@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   10e0 <rand@plt>
add    $0x4e21,%eax
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1040 <htons@plt>
mov    %rax,-0x50(%rbp)
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x48(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x18,%esi
call   10b0 <bzero@plt>
mov    0x2d1c(%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    -0x38(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    132f <main+0x15f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <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    1372 <main+0x1a2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xccb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x160(%rbp),%rdx
lea    0xcd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    121c <main+0x4c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jge    120b <main+0x2b>
mov    (%rbx),%rdi
call   10b0 <usage@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2dd6(%rip),%r14
mov    (%r14),%rdi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     131a <main+0x13a>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
lea    0x20(%rsp),%r12
lea    0xe04(%rip),%r13
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@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),%edi
add    $0x4e21,%edi
call   1050 <htons@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %r15,%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    (%r14),%rax
mov    %rax,0x18(%rsp)
mov    0x8(%rbx),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <inet_aton@plt>
mov    $0x4e20,%edi
call   1050 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x18,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     1330 <main+0x150>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    0x8(%rbx),%rsi
test   %eax,%eax
js     133d <main+0x15d>
mov    %r13,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    (%r14),%rdi
mov    0x2cc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1240 <main+0x60>
lea    0xd39(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x8(%rbx),%rsi
lea    0xcc5(%rip),%rdi
jmp    1344 <main+0x164>
lea    0xcd2(%rip),%rdi
xor    %eax,%eax
call   1060 <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 = [22 x i8] c"create socket error!\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"can't connect to %s!\0A\00"
@.str.2 = [46 x i8] c"error comes when recieve data from server %s!\00"
@.str.3 = [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
br label %22
22: ; preds = %21, %78
%23 = load i8*, i8** @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i8* %23, i32 %24, i32 0)
store i32 %25, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = call i32 @time(i32* null)
%32 = call i32 @srand(i32 %31)
%33 = call i32 @bzero(%struct.sockaddr_in* %10, i32 24)
%34 = load i8*, i8** @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i8* %34, i8** %35
%36 = call i32 (...) @rand()
%37 = add nsw i32 20001, %36
%38 = srem i32 %37, 65536
%39 = call i8* @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i8* %39, i8** %40
%41 = load i32, i32* @INADDR_ANY
%42 = call i8* @htons(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
store i8* %42, i8** %44
%45 = call i32 @bzero(%struct.sockaddr_in* %9, i32 24)
%46 = load i8*, i8** @AF_INET
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i8* %46, i8** %47
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%52 = call i32 @inet_aton(i8* %50, %struct.TYPE_2__* %51)
%53 = call i8* @htons(i32 20000)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i8* %53, i8** %54
%55 = load i32, i32* %7
%56 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%57 = load i32, i32* %11
%58 = call i64 @connect(i32 %55, %struct.sockaddr* %56, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %30
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %30
%67 = load i32, i32* %7
%68 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%69 = call i32 @recv(i32 %67, i8* %68, i32 255, i32 0)
store i32 %69, i32* %8
%70 = load i32, i32* %8
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %78
72: ; preds = %66
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 1
%75 = load i8*, i8** %74
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %75)
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %66
%79 = load i8**, i8*** %5
%80 = getelementptr inbounds i8*, i8** %79, i64 1
%81 = load i8*, i8** %80
%82 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %81, i8* %82)
%84 = load i32, i32* %7
%85 = call i32 @close(i32 %84)
br label %22
}

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