#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 */

/* Type definitions */
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ src_addr;
typedef int /*<<< orphan*/ dst_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in6 *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ in6addr_any;
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_in6 src_addr, dst_addr;
  char mesg[1000];
  if (argc != 2) {
    printf("Usage:  server6 <Listen Portnum>\n");
    exit(1);
  }
  sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  if (sockfd < 0) {
    printf("Couldn't open socket. Exiting.\n");
    exit(2);
  }
  bzero(&dst_addr, sizeof(dst_addr));
  dst_addr.sin6_family = AF_INET6;
  dst_addr.sin6_addr = in6addr_any;
  dst_addr.sin6_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     11bb <main+0x3b>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1200 <main+0x80>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <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   1060 <bind@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1070 <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   1050 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a7 <main+0x127>
mov    -0x18(%rbp),%esi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x18(%rbp),%esi
lea    -0x420(%rbp),%rdx
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    124e <main+0xce>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
cmp    $0x2,%edi
jne    11e2 <main+0x52>
mov    %rsi,%rbx
mov    0x2e2a(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
jns    11f8 <main+0x68>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %eax,%r15d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
lea    0x30(%rsp),%rsi
lea    0xc(%rsp),%r9
mov    %r15d,%edi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1060 <recvfrom@plt>
test   %eax,%eax
js     12e7 <main+0x157>
lea    0xd9f(%rip),%r14
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%rbp
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
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   1080 <bzero@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r12,%r9
call   1060 <recvfrom@plt>
test   %eax,%eax
jns    12a0 <main+0x110>
lea    0xd12(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [34 x i8] c"Usage: server6 <Listen Portnum>\0A\00"
@AF_INET6 = 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"
@in6addr_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_in6
%10 = alloca %struct.sockaddr_in6
%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_INET6
%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_in6* %10, i32 12)
%29 = load i32, i32* @AF_INET6
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @in6addr_any
%32 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %6
%40 = bitcast %struct.sockaddr_in6* %10 to %struct.sockaddr*
%41 = call i32 @bind(i32 %39, %struct.sockaddr* %40, i32 12)
br label %42
42: ; preds = %27, %55
store i32 12, i32* %8
%43 = load i32, i32* %8
%44 = call i32 @bzero(%struct.sockaddr_in6* %9, i32 %43)
%45 = load i32, i32* %6
%46 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%47 = bitcast %struct.sockaddr_in6* %9 to %struct.sockaddr*
%48 = call i32 @recvfrom(i32 %45, i8* %46, i32 1000, i32 0, %struct.sockaddr* %47, i32* %8)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %42
%52 = load i32, i32* %7
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %52)
%54 = call i32 @exit(i32 4)
unreachable
55: ; preds = %42
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %57
store i8 0, i8* %58
%59 = load i32, i32* %7
%60 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %59, i8* %60)
br label %42
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@in6addr_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: server6 <Listen Portnum>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in6
%5 = alloca %struct.sockaddr_in6
%6 = alloca [1000 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in6* %4 to i8*
%9 = bitcast %struct.sockaddr_in6* %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_INET6
%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_in6* %5, i32 12)
%26 = load i32, i32* @AF_INET6
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i64 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @in6addr_any
%29 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i64 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds i8*, i8** %1, i64 1
%31 = load i8*, i8** %30
%32 = tail call i32 @atoi(i8* %31)
%33 = tail call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i64 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%36 = call i32 @bind(i32 %19, %struct.sockaddr* %35, i32 12)
%37 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
store i32 12, i32* %3
%38 = call i32 @bzero(%struct.sockaddr_in6* %4, i32 12)
%39 = call i32 @recvfrom(i32 %19, i8* %10, i32 1000, i32 0, %struct.sockaddr* %37, i32* %3)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %45, %24
%42 = phi i32 [ %39, %24 ], [ %51, %45 ]
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 4)
unreachable
45: ; preds = %24, %45
%46 = phi i32 [ %51, %45 ], [ %39, %24 ]
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %46, i8* %10)
store i32 12, i32* %3
%50 = call i32 @bzero(%struct.sockaddr_in6* %4, i32 12)
%51 = call i32 @recvfrom(i32 %19, i8* %10, i32 1000, i32 0, %struct.sockaddr* %37, i32* %3)
%52 = icmp slt i32 %51, 0
br i1 %52, label %41, label %45
}