#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*/ src_addr;
typedef int /*<<< orphan*/ dst_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sockfd, res, len;
  struct sockaddr_in src_addr, dst_addr;
  char mesg[1000];
  if (argc != 2) {
    printf("Usage:  server4 <Listen Portnum>\n");
    exit(1);
  }
  sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sockfd < 0) {
    printf("Couldn't open socket. Exiting.\n");
    exit(2);
  }
  bzero(&dst_addr, sizeof(dst_addr));
  dst_addr.sin_family = AF_INET;
  dst_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  dst_addr.sin_port = htons(atoi(argv[1]));
  bind(sockfd, (struct sockaddr *)&dst_addr, sizeof(dst_addr));
  while (1) {
    len = sizeof(src_addr);
    bzero(&src_addr, len);
    res = recvfrom(sockfd, mesg, 1000, 0, (struct sockaddr *)&src_addr, &len);
    if (res < 0) {
      printf("Error sending datagram %d. Exiting.\n", res);
      exit(4);
    }
    mesg[res] = 0;
    printf("%d Bytes Revcieved Successfully.\nMESSAGE:%s\nFinished.\n", res,
           mesg);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cb <main+0x3b>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1210 <main+0x80>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1080 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x1c(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12bc <main+0x12c>
mov    -0x18(%rbp),%esi
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x18(%rbp),%esi
lea    -0x420(%rbp),%rdx
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1263 <main+0xd3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
cmp    $0x2,%edi
jne    11f2 <main+0x52>
mov    %rsi,%rbx
mov    0x2e2a(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
jns    1208 <main+0x68>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %eax,%r15d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
lea    0x30(%rsp),%rsi
lea    0xc(%rsp),%r9
mov    %r15d,%edi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1070 <recvfrom@plt>
test   %eax,%eax
js     12f7 <main+0x157>
lea    0xd8a(%rip),%r14
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%rbp
lea    0xc(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %eax,%ecx
movb   $0x0,0x30(%rsp,%rcx,1)
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0xc,0xc(%rsp)
mov    %rbp,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r12,%r9
call   1070 <recvfrom@plt>
test   %eax,%eax
jns    12b0 <main+0x110>
lea    0xd02(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [34 x i8] c"Usage: server4 <Listen Portnum>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [32 x i8] c"Couldn't open socket. Exiting.\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [37 x i8] c"Error sending datagram %d. Exiting.\0A\00"
@.str.3 = [55 x i8] c"%d Bytes Revcieved Successfully.\0AMESSAGE:%s\0AFinished.\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 [1000 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = load i32, i32* @IPPROTO_UDP
%21 = call i32 @socket(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 2)
unreachable
27: ; preds = %17
%28 = call i32 @bzero(%struct.sockaddr_in* %10, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
%39 = call i32 @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %6
%42 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%43 = call i32 @bind(i32 %41, %struct.sockaddr* %42, i32 12)
br label %44
44: ; preds = %27, %57
store i32 12, i32* %8
%45 = load i32, i32* %8
%46 = call i32 @bzero(%struct.sockaddr_in* %9, i32 %45)
%47 = load i32, i32* %6
%48 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%49 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%50 = call i32 @recvfrom(i32 %47, i8* %48, i32 1000, i32 0, %struct.sockaddr* %49, i32* %8)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %44
%54 = load i32, i32* %7
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = call i32 @exit(i32 4)
unreachable
57: ; preds = %44
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %59
store i8 0, i8* %60
%61 = load i32, i32* %7
%62 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %61, i8* %62)
br label %44
}

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_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [37 x i8] c"Error sending datagram %d. Exiting.\0A\00"
@.str.3 = [55 x i8] c"%d Bytes Revcieved Successfully.\0AMESSAGE:%s\0AFinished.\0A\00"
@str = [31 x i8] c"Couldn't open socket. Exiting.\00"
@str.4 = [33 x i8] c"Usage: server4 <Listen Portnum>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [1000 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%11 = icmp eq i32 %0, 2
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.4, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = load i32, i32* @IPPROTO_UDP
%19 = tail call i32 @socket(i32 %16, i32 %17, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 2)
unreachable
24: ; preds = %15
%25 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = tail call i32 @htonl(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = tail call i32 @atoi(i8* %32)
%34 = tail call i32 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %34, i32* %35
%36 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%37 = call i32 @bind(i32 %19, %struct.sockaddr* %36, i32 12)
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
store i32 12, i32* %3
%39 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%40 = call i32 @recvfrom(i32 %19, i8* %10, i32 1000, i32 0, %struct.sockaddr* %38, i32* %3)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %46, %24
%43 = phi i32 [ %40, %24 ], [ %52, %46 ]
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = call i32 @exit(i32 4)
unreachable
46: ; preds = %24, %46
%47 = phi i32 [ %52, %46 ], [ %40, %24 ]
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %48
store i8 0, i8* %49
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %47, i8* %10)
store i32 12, i32* %3
%51 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%52 = call i32 @recvfrom(i32 %19, i8* %10, i32 1000, i32 0, %struct.sockaddr* %38, i32* %3)
%53 = icmp slt i32 %52, 0
br i1 %53, label %42, label %46
}