#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*/ echoServAddr;
typedef int /*<<< orphan*/ echoClntAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ DieWithError(char *);
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 *, 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[]) {
  int sock;
  struct sockaddr_in echoServAddr;
  struct sockaddr_in echoClntAddr;
  unsigned int cliAddrLen;
  char echoBuffer[255];
  unsigned short echoServPort;
  int recvMsgSize;
  if (argc != 2) {
    fprintf(stderr, "Usage:  %s <UDP SERVER PORT>\n", argv[0]);
    exit(1);
  }
  echoServPort = atoi(argv[1]);
  if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    DieWithError("socket() failed");
  memset(&echoServAddr, 0, sizeof(echoServAddr));
  echoServAddr.sin_family = AF_INET;
  echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  echoServAddr.sin_port = htons(echoServPort);
  if (bind(sock, (struct sockaddr *)&echoServAddr, sizeof(echoServAddr)) < 0)
    DieWithError("bind() failed");
  for (;;) {
    cliAddrLen = sizeof(echoClntAddr);
    if ((recvMsgSize =
             recvfrom(sock, echoBuffer, 255, 0,
                      (struct sockaddr *)&echoClntAddr, &cliAddrLen)) < 0)
      DieWithError("recvfrom() failed");
    printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
    if (sendto(sock, echoBuffer, recvMsgSize, 0,
               (struct sockaddr *)&echoClntAddr,
               sizeof(echoClntAddr)) != recvMsgSize)
      DieWithError("sendto() sent a different number of bytes than expected");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1219 <main+0x49>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf6(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %ax,-0x142(%rbp)
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1265 <main+0x95>
lea    0xdbe(%rip),%rdi
call   10b0 <DieWithError@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x20(%rbp)
movzwl -0x142(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    12c8 <main+0xf8>
lea    0xd6b(%rip),%rdi
call   10b0 <DieWithError@plt>
jmp    12cd <main+0xfd>
movl   $0xc,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0xff,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x148(%rbp)
cmp    $0x0,%eax
jge    130d <main+0x13d>
lea    0xd34(%rip),%rdi
call   10b0 <DieWithError@plt>
mov    -0x30(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd2f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    -0x148(%rbp),%edx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
cmp    -0x148(%rbp),%eax
je     135d <main+0x18d>
lea    0xd0a(%rip),%rdi
call   10b0 <DieWithError@plt>
jmp    12cd <main+0xfd>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
cmp    $0x2,%edi
jne    1311 <main+0x141>
mov    0x8(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%r14d
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1228 <main+0x58>
lea    0xdfb(%rip),%rdi
call   10b0 <DieWithError@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x10(%rsp)
movzwl %r14w,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jns    1288 <main+0xb8>
lea    0xdab(%rip),%rdi
call   10b0 <DieWithError@plt>
lea    0x30(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xdb0(%rip),%rbp
xchg   %ax,%ax
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   10a0 <recvfrom@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    12d2 <main+0x102>
lea    0xd6f(%rip),%rdi
call   10b0 <DieWithError@plt>
mov    0x20(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r13d,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
cmp    %r13d,%eax
je     12a0 <main+0xd0>
lea    0xd58(%rip),%rdi
call   10b0 <DieWithError@plt>
jmp    12a0 <main+0xd0>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xcdc(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <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 = [30 x i8] c"Usage: %s <UDP SERVER PORT>\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [14 x i8] c"bind() failed\00"
@.str.3 = [18 x i8] c"recvfrom() failed\00"
@.str.4 = [20 x i8] c"Handling client %s\0A\00"
@.str.5 = [56 x i8] c"sendto() sent a different number of bytes than expected\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca [255 x i8]
%11 = alloca i16
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call zeroext i16 @atoi(i8* %25)
store i16 %26, i16* %11
%27 = load i32, i32* @PF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = load i32, i32* @IPPROTO_UDP
%30 = call i32 @socket(i32 %27, i32 %28, i32 %29)
store i32 %30, i32* %6
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = call i32 @DieWithError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %22
%35 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* @INADDR_ANY
%39 = call i32 @htonl(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i16, i16* %11
%43 = call i32 @htons(i16 zeroext %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %43, i32* %44
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%47 = call i64 @bind(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %34
%50 = call i32 @DieWithError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %34
br label %52
52: ; preds = %75, %51
store i32 12, i32* %9
%53 = load i32, i32* %6
%54 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%55 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%56 = call i32 @recvfrom(i32 %53, i8* %54, i32 255, i32 0, %struct.sockaddr* %55, i32* %9)
store i32 %56, i32* %12
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = call i32 @DieWithError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %58, %52
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%62 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i8* @inet_ntoa(i32 %63)
%65 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %64)
%66 = load i32, i32* %6
%67 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%68 = load i32, i32* %12
%69 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%70 = call i32 @sendto(i32 %66, i8* %67, i32 %68, i32 0, %struct.sockaddr* %69, i32 12)
%71 = load i32, i32* %12
%72 = icmp ne i32 %70, %71
br i1 %72, label %73, label %75
73: ; preds = %60
%74 = call i32 @DieWithError(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0))
br label %75
75: ; preds = %73, %60
br label %52
}

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