#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 */
typedef int /*<<< orphan*/ time_t;
struct sockaddr_storage {
  int dummy;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
typedef size_t ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ ip;
typedef int /*<<< orphan*/ cliaddr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ EXIT_FAILURE;
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *inet_ntop(int const, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
size_t recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int udp_server(char *, char *, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int sockfd;
  ssize_t n;
  socklen_t len;
  time_t ticks;
  char buf[256];
  char ip[256];
  struct sockaddr_storage cliaddr;
  struct sockaddr *sa;
  struct sockaddr_in *sin;
  struct sockaddr_in6 *sin6;
  if (2 == argc) {
    sockfd = udp_server(NULL, argv[1], NULL);
  } else if (3 == argc) {
    sockfd = udp_server(argv[1], argv[2], NULL);
  } else {
    printf("USAGE: %s [host/IP] <service/port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  while (1) {
    len = sizeof(cliaddr);
    n = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&cliaddr,
                 &len);
    buf[n] = '\0';
    sa = (struct sockaddr *)&cliaddr;
    switch (sa->sa_family) {
    case AF_INET:
      sin = (struct sockaddr_in *)&cliaddr;
      printf("[IPv4] datagram from %s\n",
             inet_ntop(AF_INET, &(sin->sin_addr), ip, sizeof(ip)));
      break;
    case AF_INET6:
      sin6 = (struct sockaddr_in6 *)&cliaddr;
      printf("[IPv6] datagram from %s\n",
             inet_ntop(AF_INET6, &(sin6->sin6_addr), ip, sizeof(ip)));
      break;
    default:
      printf("unknown type.\n");
      break;
    }
    ticks = time(NULL);
    snprintf(buf, sizeof(buf), "%.24s\n", ctime(&ticks));
    sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&cliaddr, len);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
jne    11e3 <main+0x43>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   10b0 <udp_server@plt>
mov    %eax,-0x14(%rbp)
jmp    123a <main+0x9a>
mov    $0x3,%eax
cmp    -0x8(%rbp),%eax
jne    1212 <main+0x72>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <udp_server@plt>
mov    %eax,-0x14(%rbp)
jmp    1235 <main+0x95>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    123a <main+0x9a>
jmp    123f <main+0x9f>
movl   $0x4,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x238(%rbp),%r8
mov    %r8,-0x260(%rbp)
lea    -0x24(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %rax,%rcx
mov    -0x260(%rbp),%rax
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
movb   $0x0,-0x130(%rbp,%rcx,1)
mov    %rax,-0x240(%rbp)
mov    -0x240(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x254(%rbp)
sub    $0x80,%eax
je     1305 <main+0x165>
jmp    12ae <main+0x10e>
mov    -0x254(%rbp),%eax
sub    $0x81,%eax
jne    1346 <main+0x1a6>
jmp    12c4 <main+0x124>
lea    -0x238(%rbp),%rax
mov    %rax,-0x248(%rbp)
mov    -0x248(%rbp),%rsi
lea    -0x230(%rbp),%rdx
mov    $0x81,%edi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rsi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1354 <main+0x1b4>
lea    -0x238(%rbp),%rax
mov    %rax,-0x250(%rbp)
mov    -0x250(%rbp),%rsi
lea    -0x230(%rbp),%rdx
mov    $0x80,%edi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rsi
lea    0xd03(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1354 <main+0x1b4>
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,-0x28(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x278(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <ctime@plt>
mov    -0x278(%rbp),%rdi
mov    %rax,%rcx
mov    $0x100,%esi
lea    0xcd8(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x26c(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x268(%rbp)
lea    -0x130(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x26c(%rbp),%edi
mov    -0x268(%rbp),%rsi
mov    %eax,%edx
lea    -0x238(%rbp),%r8
mov    -0x24(%rbp),%r9d
xor    %ecx,%ecx
call   1080 <sendto@plt>
jmp    123f <main+0x9f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
cmp    $0x2,%edi
je     11da <main+0x2a>
cmp    $0x3,%edi
jne    12f1 <main+0x141>
mov    0x8(%rsi),%rdi
mov    $0x2,%eax
jmp    11e1 <main+0x31>
mov    $0x1,%eax
xor    %edi,%edi
mov    (%rsi,%rax,8),%rsi
xor    %edx,%edx
call   10c0 <udp_server@plt>
mov    %eax,%ebp
lea    0x110(%rsp),%rbx
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xc(%rsp),%r13
lea    0xe4a(%rip),%r12
jmp    127c <main+0xcc>
xchg   %ax,%ax
mov    $0x80,%edi
mov    %r14,%rsi
lea    0x10(%rsp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
lea    0xe0f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,0xc(%rsp)
mov    %r13,%rdi
call   1050 <ctime@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    0x4(%rsp),%r9d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1090 <sendto@plt>
movl   $0x4,0x4(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   10b0 <recvfrom@plt>
movb   $0x0,0x110(%rsp,%rax,1)
mov    0x8(%rsp),%eax
cmp    $0x80,%eax
je     1210 <main+0x60>
cmp    $0x81,%eax
jne    12e0 <main+0x130>
mov    $0x81,%edi
mov    %r14,%rsi
lea    0x10(%rsp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
lea    0xd4d(%rip),%rdi
jmp    122e <main+0x7e>
nopl   0x0(%rax)
lea    0xd76(%rip),%rdi
call   1040 <puts@plt>
jmp    1238 <main+0x88>
mov    (%rsi),%rsi
lea    0xd05(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [36 x i8] c"USAGE: %s [host/IP] <service/port>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"[IPv4] datagram from %s\0A\00"
@.str.2 = [25 x i8] c"[IPv6] datagram from %s\0A\00"
@.str.3 = [15 x i8] c"unknown type.\0A\00"
@.str.4 = [7 x i8] c"%.24s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca [256 x i8]
%12 = alloca %struct.sockaddr_storage
%13 = alloca %struct.sockaddr*
%14 = alloca %struct.sockaddr_in*
%15 = alloca %struct.sockaddr_in6*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp eq i32 2, %16
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @udp_server(i8* null, i8* %21, i32* null)
store i32 %22, i32* %6
br label %42
23: ; preds = %2
%24 = load i32, i32* %4
%25 = icmp eq i32 3, %24
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i32 @udp_server(i8* %29, i8* %32, i32* null)
store i32 %33, i32* %6
br label %41
34: ; preds = %23
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %26
br label %42
42: ; preds = %41, %18
br label %43
43: ; preds = %42, %70
store i32 4, i32* %8
%44 = load i32, i32* %6
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%46 = bitcast %struct.sockaddr_storage* %12 to %struct.sockaddr*
%47 = call i64 @recvfrom(i32 %44, i8* %45, i32 256, i32 0, %struct.sockaddr* %46, i32* %8)
store i64 %47, i64* %7
%48 = load i64, i64* %7
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %48
store i8 0, i8* %49
%50 = bitcast %struct.sockaddr_storage* %12 to %struct.sockaddr*
store %struct.sockaddr* %50, %struct.sockaddr** %13
%51 = load %struct.sockaddr*, %struct.sockaddr** %13
%52 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %51, i32 0, i32 0
%53 = load i32, i32* %52
switch i32 %53, label %68 [
i32 129, label %54
i32 128, label %61
]
54: ; preds = %43
%55 = bitcast %struct.sockaddr_storage* %12 to %struct.sockaddr_in*
store %struct.sockaddr_in* %55, %struct.sockaddr_in** %14
%56 = load %struct.sockaddr_in*, %struct.sockaddr_in** %14
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %56, i32 0, i32 0
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%59 = call i8* @inet_ntop(i32 129, i32* %57, i8* %58, i32 256)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %59)
br label %70
61: ; preds = %43
%62 = bitcast %struct.sockaddr_storage* %12 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %62, %struct.sockaddr_in6** %15
%63 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %15
%64 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %63, i32 0, i32 0
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%66 = call i8* @inet_ntop(i32 128, i32* %64, i8* %65, i32 256)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %66)
br label %70
68: ; preds = %43
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %70
70: ; preds = %68, %61, %54
%71 = call i32 @time(i32* null)
store i32 %71, i32* %9
%72 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%73 = call i8* @ctime(i32* %9)
%74 = call i32 @snprintf(i8* %72, i32 256, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %73)
%75 = load i32, i32* %6
%76 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%78 = call i32 @strlen(i8* %77)
%79 = bitcast %struct.sockaddr_storage* %12 to %struct.sockaddr*
%80 = load i32, i32* %8
%81 = call i32 @sendto(i32 %75, i8* %76, i32 %78, i32 0, %struct.sockaddr* %79, i32 %80)
br label %43
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [36 x i8] c"USAGE: %s [host/IP] <service/port>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"[IPv4] datagram from %s\0A\00"
@.str.2 = [25 x i8] c"[IPv6] datagram from %s\0A\00"
@.str.4 = [7 x i8] c"%.24s\0A\00"
@str = [14 x i8] c"unknown type.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = alloca %struct.sockaddr_storage
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%12 = bitcast %struct.sockaddr_storage* %7 to i8*
switch i32 %0, label %16 [
i32 2, label %21
i32 3, label %13
]
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
br label %21
16: ; preds = %2
%17 = load i8*, i8** %1
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %2, %13
%22 = phi i64 [ 2, %13 ], [ 1, %2 ]
%23 = phi i8* [ %15, %13 ], [ null, %2 ]
%24 = getelementptr inbounds i8*, i8** %1, i64 %22
%25 = load i8*, i8** %24
%26 = tail call i32 @udp_server(i8* %23, i8* %25, i32* null)
%27 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%28 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %7, i64 0, i32 0
br label %29
29: ; preds = %41, %21
store i32 4, i32* %3
%30 = call i64 @recvfrom(i32 %26, i8* %10, i32 256, i32 0, %struct.sockaddr* %27, i32* %3)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %30
store i8 0, i8* %31
%32 = load i32, i32* %28
switch i32 %32, label %39 [
i32 129, label %33
i32 128, label %36
]
33: ; preds = %29
%34 = call i8* @inet_ntop(i32 129, i32* %28, i8* %11, i32 256)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %41
36: ; preds = %29
%37 = call i8* @inet_ntop(i32 128, i32* %28, i8* %11, i32 256)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %41
39: ; preds = %29
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %41
41: ; preds = %39, %36, %33
%42 = call i32 @time(i32* null)
store i32 %42, i32* %4
%43 = call i8* @ctime(i32* %4)
%44 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = call i32 @strlen(i8* %10)
%46 = load i32, i32* %3
%47 = call i32 @sendto(i32 %26, i8* %10, i32 %45, i32 0, %struct.sockaddr* %27, i32 %46)
br label %29
}