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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int sockfd, n, k = 1000;
  struct sockaddr_in servaddr;
  char banner[] = "request";
  char buffer[1000];
  if (argc != 2) {
    printf("usage:  ./%s <IP address>\n", argv[0]);
    return -1;
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = inet_addr(argv[1]);
  servaddr.sin_port = htons(32000);
  connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  sendto(sockfd, banner, strlen(banner), 0, (struct sockaddr *)&servaddr,
         sizeof(servaddr));
  n = recvfrom(sockfd, buffer, 10000, 0, NULL, NULL);
  buffer[n] = 0;
  while (k == 1000) {
    k = recvfrom(sockfd, buffer, 1000, 0, NULL, NULL);
    memset(buffer, 0, sizeof(buffer));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3e8,-0x1c(%rbp)
mov    0xe49(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e4 <main+0x54>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <main+0x161>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    $0x7d00,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x42c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x2710,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1090 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
cmpl   $0x3e8,-0x1c(%rbp)
jne    12ea <main+0x15a>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1090 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x3e8,%edx
call   1060 <memset@plt>
jmp    12a4 <main+0x114>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
movabs $0x74736575716572,%rax
mov    %rax,0x18(%rsp)
cmp    $0x2,%edi
jne    1293 <main+0x103>
mov    0x2e27(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%r12d
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rbx),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    $0x7d00,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r15
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1070 <sendto@plt>
lea    0x20(%rsp),%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x2710,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <recvfrom@plt>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <recvfrom@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x3e8,%edx
call   1060 <memset@plt>
cmp    $0x3e8,%ebp
je     1260 <main+0xd0>
jmp    12a8 <main+0x118>
mov    (%rbx),%rsi
lea    0xd63(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.banner = [8 x i8] c"request\00"
@.str = [27 x i8] c"usage: ./%s <IP address>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
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 [8 x i8]
%11 = alloca [1000 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1000, i32* %8
%12 = bitcast [8 x i8]* %10 to i8*
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %59
20: ; preds = %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* %6
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @inet_addr(i8* %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = call i32 @htons(i32 32000)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %6
%35 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%36 = call i32 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = load i32, i32* %6
%38 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%39 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
%41 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%42 = call i32 @sendto(i32 %37, i8* %38, i32 %40, i32 0, %struct.sockaddr* %41, i32 12)
%43 = load i32, i32* %6
%44 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%45 = call i32 @recvfrom(i32 %43, i8* %44, i32 10000, i32 0, i32* null, i32* null)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %47
store i8 0, i8* %48
br label %49
49: ; preds = %52, %20
%50 = load i32, i32* %8
%51 = icmp eq i32 %50, 1000
br i1 %51, label %52, label %58
52: ; preds = %49
%53 = load i32, i32* %6
%54 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%55 = call i32 @recvfrom(i32 %53, i8* %54, i32 1000, i32 0, i32* null, i32* null)
store i32 %55, i32* %8
%56 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%57 = call i32 @memset(i8* %56, i32 0, i32 1000)
br label %49
58: ; preds = %49
store i32 0, i32* %3
br label %59
59: ; preds = %58, %15
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"usage: ./%s <IP address>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = alloca [1000 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i64* %4 to i8*
store i64 32777976897693042, i64* %4
%8 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %36
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @inet_addr(i8* %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 32000)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @connect(i32 %16, %struct.sockaddr* %25, i32 12)
%27 = call i32 @strlen(i8* %7)
%28 = call i32 @sendto(i32 %16, i8* %7, i32 %27, i32 0, %struct.sockaddr* %25, i32 12)
%29 = call i32 @recvfrom(i32 %16, i8* %8, i32 10000, i32 0, i32* null, i32* null)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %13, %32
%33 = call i32 @recvfrom(i32 %16, i8* %8, i32 1000, i32 0, i32* null, i32* null)
%34 = call i32 @memset(i8* %8, i32 0, i32 1000)
%35 = icmp eq i32 %33, 1000
br i1 %35, label %32, label %36
36: ; preds = %32, %10
%37 = phi i32 [ -1, %10 ], [ 0, %32 ]
ret i32 %37
}