#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_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr_to;
typedef int /*<<< orphan*/ addr_from;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
char getchar();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int open_udp_socket(int);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr const *, int);
int /*<<< orphan*/ util_dump_addr(char *, struct sockaddr_in *);
void test_udp() {
  int sock, sock2, len, addr_from_len;
  char buffer[4096];
  struct sockaddr_in addr_from, addr_to;
  addr_from_len = sizeof(addr_from);
  sock = open_udp_socket(8890);
  sock2 = open_udp_socket(8900);
  memset(&addr_to, 0, sizeof(addr_to));
  addr_to.sin_family = AF_INET;
  addr_to.sin_port = htons(10000);
  inet_pton(AF_INET, "115.195.176.249", &addr_to.sin_addr);
  for (;;) {
    buffer[0] = getchar();
    len = sendto(sock, buffer, 1, 0, (const struct sockaddr *)&addr_to,
                 sizeof(addr_to));
    len = recvfrom(sock, buffer, 4096, 0, (struct sockaddr *)&addr_from,
                   &addr_from_len);
    printf("[%s ]<----", buffer);
    util_dump_addr("client", &addr_from);
    len = sendto(sock2, buffer, len, 0, (const struct sockaddr *)&addr_from,
                 addr_from_len);
  }
}


x86 assembly (clang, -O0):
<test_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0xc,-0x10(%rbp)
mov    $0x22ba,%edi
call   1030 <open_udp_socket@plt>
mov    %eax,-0x4(%rbp)
mov    $0x22c4,%edi
call   1030 <open_udp_socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1028(%rbp)
mov    $0x2710,%edi
call   1040 <htons@plt>
mov    %eax,-0x102c(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    -0x1030(%rbp),%rdx
lea    0xdfb(%rip),%rsi
call   10a0 <inet_pton@plt>
mov    $0x0,%al
call   1090 <getchar@plt>
mov    %al,-0x1010(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
lea    -0x1030(%rbp),%r8
mov    $0x1,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
lea    -0x1020(%rbp),%r8
mov    $0x1000,%edx
xor    %ecx,%ecx
lea    -0x10(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
lea    -0x1010(%rbp),%rsi
lea    0xda1(%rip),%rdi
call   1050 <printf@plt>
lea    0xda0(%rip),%rdi
lea    -0x1020(%rbp),%rsi
call   1080 <util_dump_addr@plt>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x1020(%rbp),%r8
mov    -0x10(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
mov    %eax,-0xc(%rbp)
jmp    120a <test_udp+0x7a>

x86 assembly (clang, -O2):
<test_udp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
movl   $0xc,0xc(%rsp)
mov    $0x22ba,%edi
call   1030 <open_udp_socket@plt>
mov    %eax,%r12d
mov    $0x22c4,%edi
call   1030 <open_udp_socket@plt>
mov    %eax,%r15d
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e0a(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x2710,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%rbx),%edi
lea    0xe05(%rip),%rsi
mov    %rbp,%rdx
call   10a0 <inet_pton@plt>
lea    0x30(%rsp),%rbp
lea    0x20(%rsp),%rbx
lea    0xe07(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %al,0x30(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
lea    0xc(%rsp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,%r14d
lea    0xda4(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1080 <util_dump_addr@plt>
mov    0xc(%rsp),%r9d
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1070 <sendto@plt>
jmp    1220 <test_udp+0x90>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [16 x i8] c"115.195.176.249\00"
@.str.1 = [11 x i8] c"[%s ]<----\00"
@.str.2 = [7 x i8] c"client\00"
define void @test_udp() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
store i32 12, i32* %4
%8 = call i32 @open_udp_socket(i32 8890)
store i32 %8, i32* %1
%9 = call i32 @open_udp_socket(i32 8900)
store i32 %9, i32* %2
%10 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 10000)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%17 = call i32 @inet_pton(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* %16)
br label %18
18: ; preds = %18, %0
%19 = call signext i8 (...) @getchar()
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
store i8 %19, i8* %20
%21 = load i32, i32* %1
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%23 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%24 = call i32 @sendto(i32 %21, i8* %22, i32 1, i32 0, %struct.sockaddr* %23, i32 12)
store i32 %24, i32* %3
%25 = load i32, i32* %1
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%27 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%28 = call i32 @recvfrom(i32 %25, i8* %26, i32 4096, i32 0, %struct.sockaddr* %27, i32* %4)
store i32 %28, i32* %3
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%30 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @util_dump_addr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %struct.sockaddr_in* %6)
%32 = load i32, i32* %2
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%34 = load i32, i32* %3
%35 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%36 = load i32, i32* %4
%37 = call i32 @sendto(i32 %32, i8* %33, i32 %34, i32 0, %struct.sockaddr* %35, i32 %36)
store i32 %37, i32* %3
br label %18
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [16 x i8] c"115.195.176.249\00"
@.str.1 = [11 x i8] c"[%s ]<----\00"
@.str.2 = [7 x i8] c"client\00"
define void @test_udp() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %1 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
store i32 12, i32* %1
%9 = tail call i32 @open_udp_socket(i32 8890)
%10 = tail call i32 @open_udp_socket(i32 8900)
%11 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 10000)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%18 = call i32 @inet_pton(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* %17)
%19 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %21
21: ; preds = %21, %0
%22 = call signext i8 (...) @getchar()
store i8 %22, i8* %6
%23 = call i32 @sendto(i32 %9, i8* %6, i32 1, i32 0, %struct.sockaddr* %19, i32 12)
%24 = call i32 @recvfrom(i32 %9, i8* %6, i32 4096, i32 0, %struct.sockaddr* %20, i32* %1)
%25 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%26 = call i32 @util_dump_addr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %struct.sockaddr_in* %3)
%27 = load i32, i32* %1
%28 = call i32 @sendto(i32 %10, i8* %6, i32 %24, i32 0, %struct.sockaddr* %20, i32 %27)
br label %21
}