#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servAddr;
typedef int /*<<< orphan*/ clntAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
unsigned short atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             unsigned int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  unsigned short servPort;
  int sock;
  struct sockaddr_in servAddr;
  struct sockaddr_in clntAddr;
  unsigned int clntAddrLen;
  char msgBuffer[(512)];
  int recvMsgLen;
  int sendMsgLen;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <Echo Port>\n", argv[0]);
    exit(1);
  }
  servPort = atoi(argv[1]);
  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 = htonl(INADDR_ANY);
  servAddr.sin_port = htons(servPort);
  if (bind(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
    fprintf(stderr, "bind() failed\n");
    exit(1);
  }
  for (;;) {
    clntAddrLen = sizeof(clntAddr);
    recvMsgLen = recvfrom(sock, msgBuffer, (512), 0,
                          (struct sockaddr *)&clntAddr, &clntAddrLen);
    if (recvMsgLen < 0) {
      fprintf(stderr, "recvfrom() failed\n");
      exit(1);
    }
    printf("Handling client %s\n", inet_ntoa(clntAddr.sin_addr));
    sendMsgLen = sendto(sock, msgBuffer, recvMsgLen, 0,
                        (struct sockaddr *)&clntAddr, sizeof(clntAddr));
    if (recvMsgLen != sendMsgLen) {
      fprintf(stderr,
              "sendto() sent a differrent number of bytes than expected\n");
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120b <main+0x4b>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %ax,-0x12(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    126a <main+0xaa>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x28(%rbp)
movzwl -0x12(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12df <main+0x11f>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xd5a(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    12e4 <main+0x124>
movl   $0xc,-0x3c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x240(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x0,-0x244(%rbp)
jge    133d <main+0x17d>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
lea    0xd0b(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xcfb(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    -0x244(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
mov    %eax,-0x248(%rbp)
mov    -0x244(%rbp),%eax
cmp    -0x248(%rbp),%eax
je     13ae <main+0x1ee>
mov    0x2c34(%rip),%rax
mov    (%rax),%edi
lea    0xcc1(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    12e4 <main+0x124>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
cmp    $0x2,%edi
jne    121a <main+0x5a>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebp
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    123e <main+0x7e>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
jmp    1336 <main+0x176>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdd3(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x10(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jns    12a8 <main+0xe8>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
jmp    1336 <main+0x176>
lea    0x30(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xd8c(%rip),%r13
xchg   %ax,%ax
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   10a0 <recvfrom@plt>
test   %eax,%eax
js     1326 <main+0x166>
mov    %eax,%ebp
mov    0x20(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
cmp    %eax,%ebp
je     12c0 <main+0x100>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
jmp    1336 <main+0x176>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
lea    0xd01(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s <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
@INADDR_ANY = global i32 0
@.str.2 = [15 x i8] c"bind() failed\0A\00"
@.str.3 = [19 x i8] c"recvfrom() failed\0A\00"
@.str.4 = [20 x i8] c"Handling client %s\0A\00"
@.str.5 = [58 x i8] c"sendto() sent a differrent number of bytes than expected\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i16
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca [512 x i8]
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call zeroext i16 @atoi(i8* %26)
store i16 %27, i16* %6
%28 = load i32, i32* @PF_INET
%29 = load i32, i32* @SOCK_DGRAM
%30 = load i32, i32* @IPPROTO_UDP
%31 = call i32 @socket(i32 %28, i32 %29, i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %23
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %23
%39 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %40, i32* %41
%42 = load i32, i32* @INADDR_ANY
%43 = call i32 @htonl(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%45 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %44, i32 0, i32 0
store i32 %43, i32* %45
%46 = load i16, i16* %6
%47 = call i32 @htons(i16 zeroext %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %47, i32* %48
%49 = load i32, i32* %7
%50 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%51 = call i64 @bind(i32 %49, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %38
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %38
br label %58
58: ; preds = %87, %57
store i32 12, i32* %10
%59 = load i32, i32* %7
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%61 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%62 = call i32 @recvfrom(i32 %59, i8* %60, i32 512, i32 0, %struct.sockaddr* %61, i32* %10)
store i32 %62, i32* %12
%63 = load i32, i32* %12
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %58
%66 = load i32, i32* @stderr
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %58
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%71 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i8* @inet_ntoa(i32 %72)
%74 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %73)
%75 = load i32, i32* %7
%76 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%77 = load i32, i32* %12
%78 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%79 = call i32 @sendto(i32 %75, i8* %76, i32 %77, i32 0, %struct.sockaddr* %78, i32 12)
store i32 %79, i32* %13
%80 = load i32, i32* %12
%81 = load i32, i32* %13
%82 = icmp ne i32 %80, %81
br i1 %82, label %83, label %87
83: ; preds = %69
%84 = load i32, i32* @stderr
%85 = call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %69
br label %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s <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
@INADDR_ANY = global i32 0
@.str.2 = [15 x i8] c"bind() failed\0A\00"
@.str.3 = [19 x i8] c"recvfrom() failed\0A\00"
@.str.4 = [20 x i8] c"Handling client %s\0A\00"
@.str.5 = [58 x i8] c"sendto() sent a differrent number of bytes than expected\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%11 = icmp eq i32 %0, 2
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call zeroext i16 @atoi(i8* %19)
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = load i32, i32* @IPPROTO_UDP
%24 = tail call i32 @socket(i32 %21, i32 %22, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %17
%31 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* @INADDR_ANY
%35 = call i32 @htonl(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %35, i32* %36
%37 = call i32 @htons(i16 zeroext %20)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i64 @bind(i32 %24, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %30
%43 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %49
45: ; preds = %30
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %42, %56
store i32 12, i32* %5
%50 = call i32 @recvfrom(i32 %24, i8* %10, i32 512, i32 0, %struct.sockaddr* %43, i32* %5)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %49
%57 = load i32, i32* %44
%58 = call i8* @inet_ntoa(i32 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %58)
%60 = call i32 @sendto(i32 %24, i8* %10, i32 %50, i32 0, %struct.sockaddr* %43, i32 12)
%61 = icmp eq i32 %50, %60
br i1 %61, label %49, label %62
62: ; preds = %56
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
}