#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;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ clientaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
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__);
char *malloc(int);
int /*<<< orphan*/ printf(char *, int, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int strlen(char *);
int main(int argc, char **argv) {
  int sockfd;
  int portno;
  int clientlen;
  struct sockaddr_in serveraddr;
  struct sockaddr_in clientaddr;
  struct hostent *hostp;
  char *buf = malloc(65535);
  char *hostaddrp;
  int optval;
  int n;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <port>\n", argv[0]);
    exit(1);
  }
  portno = atoi(argv[1]);
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  optval = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval,
             sizeof(int));
  bzero((char *)&serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  serveraddr.sin_port = htons((unsigned short)portno);
  if (bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
    error("ERROR on binding");
  clientlen = sizeof(clientaddr);
  while (1) {
    bzero(buf, 65535);
    int rxcount = recvfrom(sockfd, buf, 65535, 0,
                           (struct sockaddr *)&clientaddr, &clientlen);
    if (rxcount < 0)
      error("ERROR in recvfrom");
    hostaddrp = inet_ntoa(clientaddr.sin_addr);
    if (hostaddrp == NULL)
      error("ERROR on inet_ntoa\n");
    n = sendto(sockfd, buf, 1 ? 6 : strlen(buf), 0,
               (struct sockaddr *)&clientaddr, clientlen);
    if (n < 0)
      error("ERROR in sendto");
    printf("server received %d bytes from %s\n", rxcount, hostaddrp);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xffff,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1244 <main+0x54>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdcb(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1286 <main+0x96>
lea    0xd91(%rip),%rdi
call   10d0 <error@plt>
movl   $0x1,-0x54(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    0x2d18(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
movzwl %ax,%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    1311 <main+0x121>
lea    0xd1b(%rip),%rdi
call   10d0 <error@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x48(%rbp),%rdi
mov    $0xffff,%esi
call   10e0 <bzero@plt>
mov    -0x14(%rbp),%edi
mov    -0x48(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0xffff,%edx
xor    %ecx,%ecx
lea    -0x1c(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    135a <main+0x16a>
lea    0xce3(%rip),%rdi
call   10d0 <error@plt>
mov    -0x38(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    137d <main+0x18d>
lea    0xcd2(%rip),%rdi
call   10d0 <error@plt>
mov    -0x14(%rbp),%edi
mov    -0x48(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    $0x6,%edx
xor    %ecx,%ecx
call   1080 <sendto@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    13b1 <main+0x1c1>
lea    0xcb2(%rip),%rdi
call   10d0 <error@plt>
mov    -0x5c(%rbp),%esi
mov    -0x50(%rbp),%rdx
lea    0xcaf(%rip),%rdi
call   1060 <printf@plt>
jmp    1318 <main+0x128>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0xffff,%edi
call   10a0 <malloc@plt>
cmp    $0x2,%ebp
jne    137f <main+0x18f>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r14d
mov    0x2db4(%rip),%r12
mov    (%r12),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1252 <main+0x62>
lea    0xdc5(%rip),%rdi
call   10d0 <error@plt>
movl   $0x1,0x4(%rsp)
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d4e(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x8(%rsp)
movzwl %r14w,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jns    12d7 <main+0xe7>
lea    0xd55(%rip),%rdi
call   10d0 <error@plt>
movl   $0xc,(%rsp)
lea    0x18(%rsp),%r14
lea    0xd84(%rip),%r13
jmp    12fe <main+0x10e>
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    $0xffff,%esi
call   10e0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xffff,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %rsp,%r9
call   10b0 <recvfrom@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1335 <main+0x145>
lea    0xd08(%rip),%rdi
call   10d0 <error@plt>
mov    0x18(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1352 <main+0x162>
lea    0xcfd(%rip),%rdi
call   10d0 <error@plt>
mov    (%rsp),%r9d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1080 <sendto@plt>
test   %eax,%eax
jns    12f0 <main+0x100>
lea    0xce9(%rip),%rdi
call   10d0 <error@plt>
jmp    12f0 <main+0x100>
mov    0x2c3a(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xc6e(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s <port>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@.str.3 = [18 x i8] c"ERROR in recvfrom\00"
@.str.4 = [20 x i8] c"ERROR on inet_ntoa\0A\00"
@.str.5 = [16 x i8] c"ERROR in sendto\00"
@.str.6 = [34 x i8] c"server received %d bytes from %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 i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = call i8* @malloc(i32 65535)
store i8* %17, i8** %12
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %27
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %7
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_DGRAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %27
store i32 1, i32* %14
%40 = load i32, i32* %6
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_REUSEADDR
%43 = bitcast i32* %14 to i8*
%44 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i8* %43, i32 4)
%45 = bitcast %struct.sockaddr_in* %9 to i8*
%46 = call i32 @bzero(i8* %45, i32 12)
%47 = load i32, i32* @AF_INET
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %47, i32* %48
%49 = load i32, i32* @INADDR_ANY
%50 = call i32 @htonl(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
store i32 %50, i32* %52
%53 = load i32, i32* %7
%54 = trunc i32 %53 to i16
%55 = call i32 @htons(i16 zeroext %54)
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %55, i32* %56
%57 = load i32, i32* %6
%58 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%59 = call i64 @bind(i32 %57, %struct.sockaddr* %58, i32 12)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %39
%62 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %61, %39
store i32 12, i32* %8
br label %64
64: ; preds = %63, %94
%65 = load i8*, i8** %12
%66 = call i32 @bzero(i8* %65, i32 65535)
%67 = load i32, i32* %6
%68 = load i8*, i8** %12
%69 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%70 = call i32 @recvfrom(i32 %67, i8* %68, i32 65535, i32 0, %struct.sockaddr* %69, i32* %8)
store i32 %70, i32* %16
%71 = load i32, i32* %16
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %64
%74 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %75
75: ; preds = %73, %64
%76 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%77 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %76, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i8* @inet_ntoa(i32 %78)
store i8* %79, i8** %13
%80 = load i8*, i8** %13
%81 = icmp eq i8* %80, null
br i1 %81, label %82, label %84
82: ; preds = %75
%83 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %84
84: ; preds = %82, %75
%85 = load i32, i32* %6
%86 = load i8*, i8** %12
%87 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%88 = load i32, i32* %8
%89 = call i32 @sendto(i32 %85, i8* %86, i32 6, i32 0, %struct.sockaddr* %87, i32 %88)
store i32 %89, i32* %15
%90 = load i32, i32* %15
%91 = icmp slt i32 %90, 0
br i1 %91, label %92, label %94
92: ; preds = %84
%93 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %94
94: ; preds = %92, %84
%95 = load i32, i32* %16
%96 = load i8*, i8** %13
%97 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 %95, i8* %96)
br label %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s <port>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@.str.3 = [18 x i8] c"ERROR in recvfrom\00"
@.str.4 = [20 x i8] c"ERROR on inet_ntoa\0A\00"
@.str.5 = [16 x i8] c"ERROR in sendto\00"
@.str.6 = [34 x i8] c"server received %d bytes from %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = tail call dereferenceable_or_null(65535) i8* @malloc(i32 65535)
%11 = bitcast i32* %6 to i8*
%12 = icmp eq i32 %0, 2
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %1
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @atoi(i8* %20)
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_DGRAM
%24 = tail call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %18
store i32 1, i32* %6
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEADDR
%31 = call i32 @setsockopt(i32 %24, i32 %29, i32 %30, i8* %11, i32 4)
%32 = call i32 @bzero(i8* %8, i32 12)
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %33, i32* %34
%35 = load i32, i32* @INADDR_ANY
%36 = call i32 @htonl(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %36, i32* %37
%38 = trunc i32 %21 to i16
%39 = call i32 @htons(i16 zeroext %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%42 = call i64 @bind(i32 %24, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %28
%45 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %28
store i32 12, i32* %3
%47 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
br label %49
49: ; preds = %67, %46
%50 = call i32 @bzero(i8* %10, i32 65535)
%51 = call i32 @recvfrom(i32 %24, i8* %10, i32 65535, i32 0, %struct.sockaddr* %47, i32* %3)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %49
%56 = load i32, i32* %48
%57 = call i8* @inet_ntoa(i32 %56)
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %59, %55
%62 = load i32, i32* %3
%63 = call i32 @sendto(i32 %24, i8* %10, i32 6, i32 0, %struct.sockaddr* %47, i32 %62)
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %67
67: ; preds = %65, %61
%68 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 %51, i8* %57)
br label %49
}