#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 addrinfo {
  int ai_flags;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ *ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ *ai_next;
  int /*<<< orphan*/ *ai_canonname;
};
typedef int /*<<< orphan*/ argv;
typedef int /*<<< orphan*/ addr_hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int AI_NUMERICHOST;
int AI_NUMERICSERV;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ exit(int);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ printf(char *, ...);
int sendto(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
           int /*<<< orphan*/);
scalar_t__ socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sockfd, res;
  struct addrinfo *addr_dets = 0;
  struct addrinfo addr_hints;
  if (argc != 4) {
    printf("Usage:  udp-send4 <Dest IPv4 address> <Dest Portnum> <Message>\n");
    exit(1);
  }
  bzero(&addr_hints, sizeof(addr_hints));
  addr_hints.ai_family = AF_UNSPEC;
  addr_hints.ai_socktype = SOCK_DGRAM;
  addr_hints.ai_protocol = IPPROTO_UDP;
  addr_hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
  addr_hints.ai_canonname = NULL;
  addr_hints.ai_addr = NULL;
  addr_hints.ai_next = NULL;
  if (getaddrinfo(argv[1], argv[2], &addr_hints, &addr_dets) != 0) {
    printf("Invalid Dest Address/Portnum. Exiting.\n");
    exit(2);
  }
  if (sockfd = socket(addr_dets->ai_family, addr_dets->ai_socktype,
                      addr_dets->ai_protocol) < 0) {
    printf("Couldn't open socket. Exiting.\n");
    exit(3);
  }
  res = sendto(sockfd, argv[3], sizeof(argv[3]) + 1, 0, addr_dets->ai_addr,
               addr_dets->ai_addrlen);
  if (res < 0) {
    printf("Error sending datagram:\nRET:%d\nExiting.\n", res);
    exit(4);
  }
  printf("%d Bytes Sent Successfully. Finished.\n", res);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11a0 <main+0x40>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    -0x50(%rbp),%rdi
mov    $0x30,%esi
call   1050 <bzero@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    0x2e16(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x50(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     123e <main+0xde>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edx
call   1080 <socket@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x14(%rbp)
test   $0x1,%al
jne    1277 <main+0x117>
jmp    128f <main+0x12f>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%r8
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%r9d
mov    $0x9,%edx
xor    %ecx,%ecx
call   1040 <sendto@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12de <main+0x17e>
mov    -0x18(%rbp),%esi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x4,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%esi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movq   $0x0,(%rsp)
cmp    $0x4,%edi
jne    1230 <main+0xc0>
mov    %rsi,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x30,%esi
call   1060 <bzero@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e0d(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rsp)
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
mov    %rsp,%rcx
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
jne    1246 <main+0xd6>
mov    (%rsp),%rax
mov    0x18(%rax),%edi
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
call   1090 <socket@plt>
test   %rax,%rax
jns    125c <main+0xec>
lea    0xe2f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
shr    $0x3f,%rax
mov    0x18(%rbx),%rsi
mov    (%rsp),%rcx
mov    0x8(%rcx),%r8
mov    0x4(%rcx),%r9d
mov    %eax,%edi
mov    $0x9,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    %eax,%esi
test   %eax,%eax
jns    129c <main+0x12c>
lea    0xd75(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x4,%edi
call   1070 <exit@plt>
lea    0xd86(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32*, i32, i32, i32, i32*, i32* }
@.str = [64 x i8] c"Usage: udp-send4 <Dest IPv4 address> <Dest Portnum> <Message>\0A\00"
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@AI_NUMERICHOST = global i32 0
@AI_NUMERICSERV = global i32 0
@.str.1 = [40 x i8] c"Invalid Dest Address/Portnum. Exiting.\0A\00"
@.str.2 = [32 x i8] c"Couldn't open socket. Exiting.\0A\00"
@.str.3 = [41 x i8] c"Error sending datagram:\0ARET:%d\0AExiting.\0A\00"
@.str.4 = [39 x i8] c"%d Bytes Sent Successfully. Finished.\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 %struct.addrinfo*
%9 = alloca %struct.addrinfo
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.addrinfo* null, %struct.addrinfo** %8
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 4
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 @bzero(%struct.addrinfo* %9, i32 48)
%17 = load i32, i32* @AF_UNSPEC
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 5
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_DGRAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 4
store i32 %19, i32* %20
%21 = load i32, i32* @IPPROTO_UDP
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 3
store i32 %21, i32* %22
%23 = load i32, i32* @AI_NUMERICHOST
%24 = load i32, i32* @AI_NUMERICSERV
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 7
store i32* null, i32** %27
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 2
store i32* null, i32** %28
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 6
store i32* null, i32** %29
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i64 @getaddrinfo(i8* %32, i8* %35, %struct.addrinfo* %9, %struct.addrinfo** %8)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %15
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %15
%42 = load %struct.addrinfo*, %struct.addrinfo** %8
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 5
%44 = load i32, i32* %43
%45 = load %struct.addrinfo*, %struct.addrinfo** %8
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 4
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %8
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 3
%50 = load i32, i32* %49
%51 = call i64 @socket(i32 %44, i32 %47, i32 %50)
%52 = icmp slt i64 %51, 0
%53 = zext i1 %52 to i32
store i32 %53, i32* %6
br i1 %52, label %54, label %57
54: ; preds = %41
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 3)
unreachable
57: ; preds = %41
%58 = load i32, i32* %6
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 3
%61 = load i8*, i8** %60
%62 = load %struct.addrinfo*, %struct.addrinfo** %8
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 2
%64 = load i32*, i32** %63
%65 = load %struct.addrinfo*, %struct.addrinfo** %8
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 1
%67 = load i32, i32* %66
%68 = call i32 @sendto(i32 %58, i8* %61, i32 9, i32 0, i32* %64, i32 %67)
store i32 %68, i32* %7
%69 = load i32, i32* %7
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %57
%72 = load i32, i32* %7
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %72)
%74 = call i32 @exit(i32 4)
unreachable
75: ; preds = %57
%76 = load i32, i32* %7
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %76)
%78 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32*, i32, i32, i32, i32*, i32* }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@AI_NUMERICHOST = global i32 0
@AI_NUMERICSERV = global i32 0
@.str.3 = [41 x i8] c"Error sending datagram:\0ARET:%d\0AExiting.\0A\00"
@.str.4 = [39 x i8] c"%d Bytes Sent Successfully. Finished.\0A\00"
@str = [31 x i8] c"Couldn't open socket. Exiting.\00"
@str.5 = [39 x i8] c"Invalid Dest Address/Portnum. Exiting.\00"
@str.6 = [63 x i8] c"Usage: udp-send4 <Dest IPv4 address> <Dest Portnum> <Message>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = bitcast %struct.addrinfo** %3 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = icmp eq i32 %0, 4
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @str.6, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = call i32 @bzero(%struct.addrinfo* %4, i32 48)
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_DGRAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %15, i32* %16
%17 = load i32, i32* @IPPROTO_UDP
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %17, i32* %18
%19 = load i32, i32* @AI_NUMERICHOST
%20 = load i32, i32* @AI_NUMERICSERV
%21 = or i32 %20, %19
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32* null, i32** %23
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = bitcast i32** %24 to i8*
%27 = load i8*, i8** %25
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = call i64 @getaddrinfo(i8* %27, i8* %29, %struct.addrinfo* %4, %struct.addrinfo** %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %11
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.5, i64 0, i64 0))
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %11
%36 = load %struct.addrinfo*, %struct.addrinfo** %3
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 5
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 4
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 3
%42 = load i32, i32* %41
%43 = call i64 @socket(i32 %38, i32 %40, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%47 = call i32 @exit(i32 3)
unreachable
48: ; preds = %35
%49 = lshr i64 %43, 63
%50 = trunc i64 %49 to i32
%51 = getelementptr inbounds i8*, i8** %1, i64 3
%52 = load i8*, i8** %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %3
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i64 0, i32 2
%55 = load i32*, i32** %54
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i64 0, i32 1
%57 = load i32, i32* %56
%58 = call i32 @sendto(i32 %50, i8* %52, i32 9, i32 0, i32* %55, i32 %57)
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %48
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %58)
%62 = call i32 @exit(i32 4)
unreachable
63: ; preds = %48
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%65 = call i32 @exit(i32 0)
unreachable
}