#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ reuse;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct sockaddr_in server = {}, client = {};
  bzero(&server, sizeof(server));
  server.sin_family = PF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(1995);
  int fd = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
  if (fd == -1) {
    perror("socket");
    return -1;
  }
  int reuse = 1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  if (bind(fd, (const struct sockaddr *)&server, sizeof(server)) == -1) {
    perror("bind");
    return -1;
  }
  int client_len = sizeof(struct sockaddr);
  while (1) {
    char buf[128] = {};
    printf("等待接受来自客户端发送数据...\n");
    if (recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&client,
                 (socklen_t *)&client_len) == -1) {
      perror("recvfrom");
      break;
    }
    printf("receive from %s : %s\n", inet_ntoa(client.sin_addr), buf);
    if (sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&client,
               sizeof(server)) == -1) {
      perror("sendto");
      break;
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x7cb,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1271 <main+0xb1>
lea    0xda0(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139c <main+0x1dc>
movl   $0x1,-0x38(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    0x2d25(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12ce <main+0x10e>
lea    0xd4a(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139c <main+0x1dc>
movl   $0x4,-0x3c(%rbp)
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x34(%rbp),%edi
lea    -0xc0(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x80,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   10a0 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    132e <main+0x16e>
lea    0xd14(%rip),%rdi
call   10c0 <perror@plt>
jmp    138d <main+0x1cd>
mov    -0x30(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    -0xc0(%rbp),%rdx
lea    0xcfa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x34(%rbp),%edi
lea    -0xc0(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x80,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1388 <main+0x1c8>
lea    0xcd9(%rip),%rdi
call   10c0 <perror@plt>
jmp    138d <main+0x1cd>
jmp    12d5 <main+0x115>
mov    -0x34(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
movl   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x20(%rsp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    $0x7cb,%edi
call   1060 <htons@plt>
mov    %eax,0x24(%rsp)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
jne    1247 <main+0x87>
lea    0xdbb(%rip),%rdi
jmp    1292 <main+0xd2>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a1 <main+0xe1>
lea    0xd75(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    13f4 <main+0x234>
movl   $0x4,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
lea    0xd4e(%rip),%rdi
call   1030 <puts@plt>
lea    0x30(%rsp),%rsi
xor    %ebp,%ebp
lea    0x10(%rsp),%r8
lea    0x8(%rsp),%r9
mov    %ebx,%edi
mov    $0x80,%edx
xor    %ecx,%ecx
call   10a0 <recvfrom@plt>
lea    0xcfd(%rip),%rdi
cmp    $0xffffffff,%eax
je     13e8 <main+0x228>
lea    0xcf6(%rip),%r12
lea    0x30(%rsp),%rbp
lea    0x10(%rsp),%r13
lea    0xd02(%rip),%r14
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %r12,%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
je     13df <main+0x21f>
xorps  %xmm0,%xmm0
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    %r15,%r9
call   10a0 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    1340 <main+0x180>
xor    %ebp,%ebp
lea    0xc2f(%rip),%rdi
jmp    13e8 <main+0x228>
lea    0xc45(%rip),%rdi
xor    %ebp,%ebp
call   10c0 <perror@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %ebp,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@PF_UNSPEC = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [44 x i8] c"\E7\AD\89\E5\BE\85\E6\8E\A5\E5\8F\97\E6\9D\A5\E8\87\AA\E5\AE\A2\E6\88\B7\E7\AB\AF\E5\8F\91\E9\80\81\E6\95\B0\E6\8D\AE...\0A\00"
@.str.3 = [9 x i8] c"recvfrom\00"
@.str.4 = [22 x i8] c"receive from %s : %s\0A\00"
@.str.5 = [7 x i8] c"sendto\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast %struct.sockaddr_in* %6 to i8*
%13 = bitcast %struct.sockaddr_in* %7 to i8*
%14 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%15 = load i32, i32* @PF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = call i32 @htons(i32 1995)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_DGRAM
%24 = load i32, i32* @PF_UNSPEC
%25 = call i32 @socket(i32 %22, i32 %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %2
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %70
30: ; preds = %2
store i32 1, i32* %9
%31 = load i32, i32* %8
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i32 @setsockopt(i32 %31, i32 %32, i32 %33, i32* %9, i32 4)
%35 = load i32, i32* %8
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i32 @bind(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %70
41: ; preds = %30
store i32 4, i32* %10
br label %42
42: ; preds = %41, %66
%43 = bitcast [128 x i8]* %11 to i8*
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* %8
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%47 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%48 = call i32 @recvfrom(i32 %45, i8* %46, i32 128, i32 0, %struct.sockaddr* %47, i32* %10)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %67
52: ; preds = %42
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i8* @inet_ntoa(i32 %55)
%57 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %56, i8* %57)
%59 = load i32, i32* %8
%60 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%61 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%62 = call i32 @sendto(i32 %59, i8* %60, i32 128, i32 0, %struct.sockaddr* %61, i32 12)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %52
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %67
66: ; preds = %52
br label %42
67: ; preds = %64, %50
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
store i32 0, i32* %3
br label %70
70: ; preds = %67, %39, %28
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@PF_UNSPEC = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.3 = [9 x i8] c"recvfrom\00"
@.str.4 = [22 x i8] c"receive from %s : %s\0A\00"
@.str.5 = [7 x i8] c"sendto\00"
@str = [43 x i8] c"\E7\AD\89\E5\BE\85\E6\8E\A5\E5\8F\97\E6\9D\A5\E8\87\AA\E5\AE\A2\E6\88\B7\E7\AB\AF\E5\8F\91\E9\80\81\E6\95\B0\E6\8D\AE...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%11 = load i32, i32* @PF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 1995)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = load i32, i32* @PF_UNSPEC
%20 = tail call i32 @socket(i32 %17, i32 %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %59
24: ; preds = %2
%25 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = call i32 @setsockopt(i32 %20, i32 %26, i32 %27, i32* %5, i32 4)
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @bind(i32 %20, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %57
34: ; preds = %24
%35 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @recvfrom(i32 %20, i8* %36, i32 128, i32 0, %struct.sockaddr* %38, i32* %6)
%40 = icmp eq i32 %39, -1
br i1 %40, label %53, label %41
41: ; preds = %34
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %43
43: ; preds = %41, %49
%44 = load i32, i32* %42
%45 = call i8* @inet_ntoa(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %45, i8* %36)
%47 = call i32 @sendto(i32 %20, i8* %36, i32 128, i32 0, %struct.sockaddr* %38, i32 12)
%48 = icmp eq i32 %47, -1
br i1 %48, label %53, label %49
49: ; preds = %43
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%51 = call i32 @recvfrom(i32 %20, i8* %36, i32 128, i32 0, %struct.sockaddr* %38, i32* %6)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %43
53: ; preds = %43, %49, %34
%54 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), %34 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), %49 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %43 ]
%55 = call i32 @perror(i8* %54)
%56 = call i32 @close(i32 %20)
br label %57
57: ; preds = %53, %32
%58 = phi i32 [ -1, %32 ], [ 0, %53 ]
br label %59
59: ; preds = %57, %22
%60 = phi i32 [ -1, %22 ], [ %58, %57 ]
ret i32 %60
}