#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*/ request;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int handle_msg(char *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int sockfd, n_bytes;
  struct sockaddr_in servaddr, cliaddr;
  char request[1500];
  int quit = 0, flag = 1;
  if (argc != 2) {
    printf("usage:  robot <IP address>\n");
    exit(1);
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = inet_addr(argv[1]);
  servaddr.sin_port = htons(10000);
  connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  while (!quit) {
    n_bytes = recv(sockfd, request, sizeof(request) - 1, 0);
    if (n_bytes == -1) {
      return 0;
    } else if (n_bytes == 0) {
      fprintf(stderr, "Socket %d closed", sockfd);
      close(sockfd);
      return 0;
    } else {
      request[n_bytes] = '\0';
      printf("Received %s\n", request);
      quit = handle_msg(request, sockfd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x630,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x624(%rbp)
movl   $0x1,-0x628(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120f <main+0x4f>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
lea    -0x628(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    $0x2710,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmpl   $0x0,-0x624(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12b3 <main+0xf3>
jmp    1358 <main+0x198>
mov    -0x14(%rbp),%edi
lea    -0x620(%rbp),%rsi
mov    $0x5db,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12e2 <main+0x122>
movl   $0x0,-0x4(%rbp)
jmp    135f <main+0x19f>
cmpl   $0x0,-0x18(%rbp)
jne    1318 <main+0x158>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xd1d(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    135f <main+0x19f>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x620(%rbp,%rax,1)
lea    -0x620(%rbp),%rsi
lea    0xcfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x620(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <handle_msg@plt>
mov    %eax,-0x624(%rbp)
jmp    1353 <main+0x193>
jmp    129a <main+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x630,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x608,%rsp
movl   $0x1,0xc(%rsp)
cmp    $0x2,%edi
jne    12ec <main+0x11c>
mov    %rsi,%r14
mov    0x2de8(%rip),%r15
mov    (%r15),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%r14),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    $0x2710,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
lea    0x20(%rsp),%rbx
lea    0xd91(%rip),%r14
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x5db,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     12dc <main+0x10c>
test   %eax,%eax
je     12be <main+0xee>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <handle_msg@plt>
test   %eax,%eax
je     1280 <main+0xb0>
jmp    12dc <main+0x10c>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xd32(%rip),%rsi
mov    %ebp,%edx
call   10b0 <fprintf@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x608,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2b(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [28 x i8] c"usage: robot <IP address>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"Socket %d closed\00"
@.str.2 = [13 x i8] c"Received %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 %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca [1500 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
store i32 1, i32* %12
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = load i32, i32* @IPPROTO_TCP
%24 = load i32, i32* @TCP_NODELAY
%25 = bitcast i32* %12 to i8*
%26 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i8* %25, i32 4)
%27 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @inet_addr(i8* %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = call i32 @htons(i32 10000)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%40 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
br label %41
41: ; preds = %71, %18
%42 = load i32, i32* %11
%43 = icmp ne i32 %42, 0
%44 = xor i1 %43, true
br i1 %44, label %45, label %72
45: ; preds = %41
%46 = load i32, i32* %6
%47 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 0
%48 = call i32 @recv(i32 %46, i8* %47, i32 1499, i32 0)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %52
51: ; preds = %45
store i32 0, i32* %3
br label %73
52: ; preds = %45
%53 = load i32, i32* %7
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = load i32, i32* %6
%58 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
store i32 0, i32* %3
br label %73
61: ; preds = %52
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %65)
%67 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 0
%68 = load i32, i32* %6
%69 = call i32 @handle_msg(i8* %67, i32 %68)
store i32 %69, i32* %11
br label %70
70: ; preds = %61
br label %71
71: ; preds = %70
br label %41
72: ; preds = %41
store i32 0, i32* %3
br label %73
73: ; preds = %72, %55, %51
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"Socket %d closed\00"
@.str.2 = [13 x i8] c"Received %s\0A\00"
@str = [27 x i8] c"usage: robot <IP address>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1500 x i8]
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [1500 x i8], [1500 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
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* @IPPROTO_TCP
%18 = load i32, i32* @TCP_NODELAY
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i8* %8, i32 4)
%20 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @inet_addr(i8* %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = call i32 @htons(i32 10000)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @connect(i32 %16, %struct.sockaddr* %29, i32 12)
br label %31
31: ; preds = %13, %37
%32 = call i32 @recv(i32 %16, i8* %7, i32 1499, i32 0)
switch i32 %32, label %37 [
i32 -1, label %43
i32 0, label %33
]
33: ; preds = %31
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%36 = call i32 @close(i32 %16)
br label %43
37: ; preds = %31
%38 = sext i32 %32 to i64
%39 = getelementptr inbounds [1500 x i8], [1500 x i8]* %4, i64 0, i64 %38
store i8 0, i8* %39
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%41 = call i32 @handle_msg(i8* %7, i32 %16)
%42 = icmp eq i32 %41, 0
br i1 %42, label %31, label %43
43: ; preds = %37, %31, %33
ret i32 0
}