#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_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int sa_family;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ recv_line;
typedef int /*<<< orphan*/ ip;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
char *inet_ntop(int const, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int udp_client(char *, char *, struct sockaddr **, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int sockfd, n;
  char recv_line[256];
  char ip[256];
  socklen_t len;
  struct sockaddr *sa;
  struct sockaddr_in *sin;
  struct sockaddr_in6 *sin6;
  if (argc != 3) {
    printf("USAGE: %s <hostname/IP> <service/port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  sockfd = udp_client(argv[1], argv[2], &sa, &len);
  switch (sa->sa_family) {
  case AF_INET:
    sin = (struct sockaddr_in *)sa;
    printf("[IPv4]sending to %s\n",
           inet_ntop(AF_INET, &(sin->sin_addr), ip, sizeof(ip)));
    break;
  case AF_INET6:
    sin6 = (struct sockaddr_in6 *)sa;
    printf("[IPv6]sending to %s\n",
           inet_ntop(AF_INET6, &(sin6->sin6_addr), ip, sizeof(ip)));
    break;
  default:
    printf("unknown type.\n");
    break;
  }
  sendto(sockfd, "hello", sizeof("hello"), 0, sa, len);
  n = recvfrom(sockfd, recv_line, sizeof(recv_line), 0, NULL, NULL);
  recv_line[n] = '\0';
  fputs(recv_line, stdout);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b6 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x10(%rax),%rsi
lea    -0x230(%rbp),%rdx
lea    -0x224(%rbp),%rcx
call   1070 <udp_client@plt>
mov    %eax,-0x14(%rbp)
mov    -0x230(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x244(%rbp)
sub    $0x80,%eax
je     124e <main+0xde>
jmp    11f7 <main+0x87>
mov    -0x244(%rbp),%eax
sub    $0x81,%eax
jne    128f <main+0x11f>
jmp    120d <main+0x9d>
mov    -0x230(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rsi
lea    -0x220(%rbp),%rdx
mov    $0x81,%edi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rsi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129d <main+0x12d>
mov    -0x230(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x240(%rbp),%rsi
lea    -0x220(%rbp),%rdx
mov    $0x80,%edi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129d <main+0x12d>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x230(%rbp),%r8
mov    -0x224(%rbp),%r9d
lea    0xdac(%rip),%rsi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1080 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
mov    0x2cdc(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x218,%rsp
cmp    $0x3,%edi
jne    11d9 <main+0x59>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rsi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
call   1080 <udp_client@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rsi
mov    (%rsi),%eax
cmp    $0x80,%eax
je     11f8 <main+0x78>
cmp    $0x81,%eax
jne    121f <main+0x9f>
lea    0x10(%rsp),%rdx
mov    $0x81,%edi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
lea    0xe51(%rip),%rdi
jmp    1213 <main+0x93>
mov    (%rsi),%rsi
lea    0xe1d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0x10(%rsp),%rdx
mov    $0x80,%edi
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
lea    0xe2a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    122b <main+0xab>
lea    0xe32(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%r8
mov    0x4(%rsp),%r9d
lea    0xe16(%rip),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1070 <sendto@plt>
lea    0x110(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <recvfrom@plt>
cltq
movb   $0x0,0x110(%rsp,%rax,1)
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <fputs@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

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