#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
typedef int /*<<< orphan*/ servAddr;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
scalar_t__ ReceiveEchoMessage(int, struct sockaddr_in *);
int /*<<< orphan*/ SOCK_DGRAM;
int STDIN_FILENO;
scalar_t__ SendEchoMessage(int, struct sockaddr_in *);
unsigned short atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char *servIP;
  unsigned short servPort;
  int sock;
  struct sockaddr_in servAddr;
  int maxDescriptor;
  fd_set fdSet;
  struct timeval tout;
  if ((argc < 2) || (argc > 3)) {
    fprintf(stderr, "Usage: %s <Server IP> [<Echo Port>]\n", argv[0]);
    exit(1);
  }
  servIP = argv[1];
  if (argc == 3) {
    servPort = atoi(argv[2]);
  } else {
    servPort = 7;
  }
  sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0) {
    fprintf(stderr, "socket() failed\n");
    exit(1);
  }
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = inet_addr(servIP);
  servAddr.sin_port = htons(servPort);
  maxDescriptor = sock;
  for (;;) {
    FD_ZERO(&fdSet);
    FD_SET(STDIN_FILENO, &fdSet);
    FD_SET(sock, &fdSet);
    tout.tv_sec = (2);
    tout.tv_usec = 0;
    if (select(maxDescriptor + 1, &fdSet, NULL, NULL, &tout) == 0) {
      continue;
    }
    if (FD_ISSET(STDIN_FILENO, &fdSet)) {
      if (SendEchoMessage(sock, &servAddr) < 0) {
        break;
      }
    }
    if (FD_ISSET(sock, &fdSet)) {
      if (ReceiveEchoMessage(sock, &servAddr) < 0) {
        break;
      }
    }
  }
  close(sock);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     120a <main+0x2a>
cmpl   $0x3,-0x8(%rbp)
jle    1232 <main+0x52>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    125e <main+0x7e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %ax,-0x1a(%rbp)
jmp    1264 <main+0x84>
movw   $0x7,-0x1a(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12b2 <main+0xd2>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
movzwl -0x1a(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x38(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10e0 <FD_SET@plt>
movl   $0x2,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x34(%rbp),%edi
add    $0x1,%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   10b0 <select@plt>
cmp    $0x0,%rax
jne    134b <main+0x16b>
jmp    12ec <main+0x10c>
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1387 <main+0x1a7>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1090 <SendEchoMessage@plt>
cmp    $0x0,%rax
jge    1382 <main+0x1a2>
jmp    13c2 <main+0x1e2>
jmp    1387 <main+0x1a7>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13bd <main+0x1dd>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10f0 <ReceiveEchoMessage@plt>
cmp    $0x0,%rax
jge    13b8 <main+0x1d8>
jmp    13c2 <main+0x1e2>
jmp    13bd <main+0x1dd>
jmp    12ec <main+0x10c>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    -0x4(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     121a <main+0x3a>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdf7(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x8(%rsi),%r15
mov    $0x7,%bp
cmp    $0x3,%edi
jne    1232 <main+0x52>
mov    0x10(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebp
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
test   %eax,%eax
jns    1277 <main+0x97>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0xc(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%r15d
add    $0x1,%r15d
lea    0x14(%rsp),%rbp
mov    0x2cfb(%rip),%r13
lea    0x18(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   10a0 <FD_ZERO@plt>
mov    0x0(%r13),%edi
mov    %rbp,%rsi
call   10e0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10e0 <FD_SET@plt>
movl   $0x2,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   10b0 <select@plt>
test   %rax,%rax
je     12d0 <main+0xf0>
mov    0x0(%r13),%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1336 <main+0x156>
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <SendEchoMessage@plt>
test   %rax,%rax
js     1358 <main+0x178>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12d0 <main+0xf0>
mov    %ebx,%edi
mov    %r14,%rsi
call   10f0 <ReceiveEchoMessage@plt>
test   %rax,%rax
jns    12d0 <main+0xf0>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
@stderr = global i32 0
@.str = [37 x i8] c"Usage: %s <Server IP> [<Echo Port>]\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [17 x i8] c"socket() failed\0A\00"
@AF_INET = global i32 0
@STDIN_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i16
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 3
br i1 %17, label %18, label %25
18: ; preds = %15, %2
%19 = load i32, i32* @stderr
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %6
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 3
br i1 %30, label %31, label %36
31: ; preds = %25
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call zeroext i16 @atoi(i8* %34)
store i16 %35, i16* %7
br label %37
36: ; preds = %25
store i16 7, i16* %7
br label %37
37: ; preds = %36, %31
%38 = load i32, i32* @PF_INET
%39 = load i32, i32* @SOCK_DGRAM
%40 = load i32, i32* @IPPROTO_UDP
%41 = call i32 @socket(i32 %38, i32 %39, i32 %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %37
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %37
%49 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%50 = load i32, i32* @AF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %50, i32* %51
%52 = load i8*, i8** %6
%53 = call i32 @inet_addr(i8* %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%55 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %54, i32 0, i32 0
store i32 %53, i32* %55
%56 = load i16, i16* %7
%57 = call i32 @htons(i16 zeroext %56)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %57, i32* %58
%59 = load i32, i32* %8
store i32 %59, i32* %10
br label %60
60: ; preds = %93, %72, %48
%61 = call i32 @FD_ZERO(i32* %11)
%62 = load i32, i32* @STDIN_FILENO
%63 = call i32 @FD_SET(i32 %62, i32* %11)
%64 = load i32, i32* %8
%65 = call i32 @FD_SET(i32 %64, i32* %11)
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 2, i32* %66
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %67
%68 = load i32, i32* %10
%69 = add nsw i32 %68, 1
%70 = call i64 @select(i32 %69, i32* %11, i32* null, i32* null, %struct.timeval* %12)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %60
br label %60
73: ; preds = %60
%74 = load i32, i32* @STDIN_FILENO
%75 = call i64 @FD_ISSET(i32 %74, i32* %11)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %83
77: ; preds = %73
%78 = load i32, i32* %8
%79 = call i64 @SendEchoMessage(i32 %78, %struct.sockaddr_in* %9)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %77
br label %94
82: ; preds = %77
br label %83
83: ; preds = %82, %73
%84 = load i32, i32* %8
%85 = call i64 @FD_ISSET(i32 %84, i32* %11)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %93
87: ; preds = %83
%88 = load i32, i32* %8
%89 = call i64 @ReceiveEchoMessage(i32 %88, %struct.sockaddr_in* %9)
%90 = icmp slt i64 %89, 0
br i1 %90, label %91, label %92
91: ; preds = %87
br label %94
92: ; preds = %87
br label %93
93: ; preds = %92, %83
br label %60
94: ; preds = %91, %81
%95 = load i32, i32* %8
%96 = call i32 @close(i32 %95)
%97 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
@stderr = global i32 0
@.str = [37 x i8] c"Usage: %s <Server IP> [<Echo Port>]\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [17 x i8] c"socket() failed\0A\00"
@AF_INET = global i32 0
@STDIN_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = add i32 %0, -4
%10 = icmp ult i32 %9, -2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = icmp eq i32 %0, 3
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call zeroext i16 @atoi(i8* %22)
br label %24
24: ; preds = %16, %20
%25 = phi i16 [ %23, %20 ], [ 7, %16 ]
%26 = load i32, i32* @PF_INET
%27 = load i32, i32* @SOCK_DGRAM
%28 = load i32, i32* @IPPROTO_UDP
%29 = tail call i32 @socket(i32 %26, i32 %27, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %24
%36 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %37, i32* %38
%39 = call i32 @inet_addr(i8* %18)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %39, i32* %40
%41 = call i32 @htons(i16 zeroext %25)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%45 = add nuw nsw i32 %29, 1
br label %46
46: ; preds = %63, %35
%47 = call i32 @FD_ZERO(i32* %4)
%48 = load i32, i32* @STDIN_FILENO
%49 = call i32 @FD_SET(i32 %48, i32* %4)
%50 = call i32 @FD_SET(i32 %29, i32* %4)
store i32 2, i32* %43
store i64 0, i64* %44
%51 = call i64 @select(i32 %45, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%52 = icmp eq i64 %51, 0
br i1 %52, label %63, label %53
53: ; preds = %46
%54 = load i32, i32* @STDIN_FILENO
%55 = call i64 @FD_ISSET(i32 %54, i32* %4)
%56 = icmp eq i64 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %53
%58 = call i64 @SendEchoMessage(i32 %29, %struct.sockaddr_in* %3)
%59 = icmp slt i64 %58, 0
br i1 %59, label %67, label %60
60: ; preds = %57, %53
%61 = call i64 @FD_ISSET(i32 %29, i32* %4)
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %60, %64, %46
br label %46
64: ; preds = %60
%65 = call i64 @ReceiveEchoMessage(i32 %29, %struct.sockaddr_in* %3)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %63
67: ; preds = %64, %57
%68 = call i32 @close(i32 %29)
%69 = call i32 @exit(i32 0)
unreachable
}