#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ src_addr;
typedef int socklen_t;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_NUMERICHOST;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memcpy(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  struct addrinfo *result = NULL;
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_NUMERICHOST;
  int rc = getaddrinfo(argv[1], argv[2], &hints, &result);
  int udp_socket = socket(result->ai_family, result->ai_socktype, 0);
  struct sockaddr_storage src_addr;
  socklen_t addrlen = sizeof(src_addr);
  memcpy((void *)&src_addr, result->ai_addr, result->ai_addrlen);
  freeaddrinfo(result);
  char buf[2];
  char buf_read[80];
  while (1) {
    scanf("%s", buf);
    sendto(udp_socket, buf, 1, 0, (struct sockaddr *)&src_addr, addrlen);
    int bytes = recvfrom(udp_socket, buf_read, 79, 0,
                         (struct sockaddr *)&src_addr, &addrlen);
    buf_read[bytes] = '\0';
    printf("%s\n", buf_read);
  }
  close(udp_socket);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x38(%rbp)
movl   $0x4,-0x44(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
lea    -0x46(%rbp),%rsi
lea    0xdb2(%rip),%rdi
call   1080 <scanf@plt>
mov    -0x38(%rbp),%edi
lea    -0x46(%rbp),%rsi
lea    -0x40(%rbp),%r8
mov    -0x44(%rbp),%r9d
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    -0x38(%rbp),%edi
lea    -0xa0(%rbp),%rsi
lea    -0x40(%rbp),%r8
mov    $0x4f,%edx
xor    %ecx,%ecx
lea    -0x44(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0xa4(%rbp)
movslq -0xa4(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rsi
lea    0xd54(%rip),%rdi
call   1030 <printf@plt>
jmp    1243 <main+0xb3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%rbx
movq   $0x0,0x10(%rsp)
lea    0x28(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
lea    0x10(%rsp),%rcx
mov    %rbp,%rdx
call   1090 <getaddrinfo@plt>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%esi
mov    0xc(%rax),%edi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
movl   $0x4,0xc(%rsp)
mov    0x10(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
call   1060 <memcpy@plt>
mov    0x10(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
lea    0x1e(%rsp),%r12
lea    0x40(%rsp),%r13
lea    0xc(%rsp),%rbp
lea    0xdb3(%rip),%r15
lea    0xda9(%rip),%rdi
mov    %r12,%rsi
call   1080 <scanf@plt>
mov    0xc(%rsp),%r9d
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1050 <sendto@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x4f,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %rbp,%r9
call   1070 <recvfrom@plt>
cltq
movb   $0x0,0x40(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r13,%rsi
call   1030 <printf@plt>
jmp    1250 <main+0xc0>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@AI_NUMERICHOST = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_storage
%11 = alloca i32
%12 = alloca [2 x i8]
%13 = alloca [80 x i8]
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.addrinfo* null, %struct.addrinfo** %6
%15 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 20)
%16 = load i32, i32* @AF_UNSPEC
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_DGRAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @AI_NUMERICHOST
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %20, i32* %21
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @getaddrinfo(i8* %24, i8* %27, %struct.addrinfo* %7, %struct.addrinfo** %6)
store i32 %28, i32* %8
%29 = load %struct.addrinfo*, %struct.addrinfo** %6
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 3
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %6
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %31, i32 %34, i32 0)
store i32 %35, i32* %9
store i32 4, i32* %11
%36 = bitcast %struct.sockaddr_storage* %10 to i8*
%37 = load %struct.addrinfo*, %struct.addrinfo** %6
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %6
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(i8* %36, i32 %39, i32 %42)
%44 = load %struct.addrinfo*, %struct.addrinfo** %6
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
br label %46
46: ; preds = %2, %46
%47 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%48 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %47)
%49 = load i32, i32* %9
%50 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%51 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%52 = load i32, i32* %11
%53 = call i32 @sendto(i32 %49, i8* %50, i32 1, i32 0, %struct.sockaddr* %51, i32 %52)
%54 = load i32, i32* %9
%55 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%56 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%57 = call i32 @recvfrom(i32 %54, i8* %55, i32 79, i32 0, %struct.sockaddr* %56, i32* %11)
store i32 %57, i32* %14
%58 = load i32, i32* %14
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 %59
store i8 0, i8* %60
%61 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%62 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %61)
br label %46
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@AI_NUMERICHOST = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = alloca [2 x i8]
%8 = alloca [80 x i8]
%9 = bitcast %struct.addrinfo** %3 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%10 = bitcast %struct.addrinfo* %4 to i8*
%11 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 20)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_DGRAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @AI_NUMERICHOST
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %16, i32* %17
%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 @getaddrinfo(i8* %19, i8* %21, %struct.addrinfo* %4, %struct.addrinfo** %3)
%23 = load %struct.addrinfo*, %struct.addrinfo** %3
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %25, i32 %27, i32 0)
%29 = bitcast %struct.sockaddr_storage* %5 to i8*
%30 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%31 = load %struct.addrinfo*, %struct.addrinfo** %3
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i8* %29, i32 %33, i32 %35)
%37 = load %struct.addrinfo*, %struct.addrinfo** %3
%38 = call i32 @freeaddrinfo(%struct.addrinfo* %37)
%39 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%40 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%41 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
br label %42
42: ; preds = %42, %2
%43 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %39)
%44 = load i32, i32* %6
%45 = call i32 @sendto(i32 %28, i8* %39, i32 1, i32 0, %struct.sockaddr* %41, i32 %44)
%46 = call i32 @recvfrom(i32 %28, i8* %40, i32 79, i32 0, %struct.sockaddr* %41, i32* %6)
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %42
}