#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 */
typedef int /*<<< orphan*/ toAddr;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fromAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             unsigned int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sock;
  struct sockaddr_in toAddr;
  struct sockaddr_in fromAddr;
  int recvLen;
  unsigned int addrLen;
  char recvBuffer[128];
  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0) {
    printf("create socket fail!\r\n");
    exit(0);
  }
  memset(&fromAddr, 0, sizeof(fromAddr));
  fromAddr.sin_family = AF_INET;
  fromAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  fromAddr.sin_port = htons(4000);
  if (bind(sock, (struct sockaddr *)&fromAddr, sizeof(fromAddr)) < 0) {
    printf("bind address fail, errno=%d\r\n", errno);
    close(sock);
    exit(1);
  }
  while (1) {
    addrLen = sizeof(toAddr);
    if ((recvLen = recvfrom(sock, recvBuffer, 128, 0,
                            (struct sockaddr *)&toAddr, &addrLen)) < 0) {
      printf("receive fail, errno=%d\r\n", errno);
      close(sock);
      exit(1);
    }
    printf("Server Rcv: %s\r\n", recvBuffer);
    if (sendto(sock, recvBuffer, recvLen, 0, (struct sockaddr *)&toAddr,
               sizeof(toAddr)) != recvLen) {
      printf("sendto fail\r\n");
      close(sock);
      exit(0);
    }
  }
  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)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fb <main+0x5b>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xfa0,%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1279 <main+0xd9>
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    127e <main+0xde>
movl   $0xc,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xc0(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x80,%edx
xor    %ecx,%ecx
lea    -0x38(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    12d8 <main+0x138>
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0xc0(%rbp),%rsi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    -0x34(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
cmp    -0x34(%rbp),%eax
je     1331 <main+0x191>
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    127e <main+0xde>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    0x2e20(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
test   %eax,%eax
jns    11f9 <main+0x49>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0xfa0,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jns    1261 <main+0xb1>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
lea    0xda4(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r13
lea    0xc(%rsp),%r15
lea    0xdc0(%rip),%r12
nopw   0x0(%rax,%rax,1)
movl   $0xc,0xc(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    %r15,%r9
call   10a0 <recvfrom@plt>
test   %eax,%eax
js     12ea <main+0x13a>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
cmp    %ebp,%eax
je     1280 <main+0xd0>
lea    0xd72(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
lea    0xd24(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [22 x i8] c"create socket fail!\0D\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"bind address fail, errno=%d\0D\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"receive fail, errno=%d\0D\0A\00"
@.str.3 = [17 x i8] c"Server Rcv: %s\0D\0A\00"
@.str.4 = [14 x i8] c"sendto fail\0D\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%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 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = load i32, i32* @IPPROTO_UDP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = call i32 @htons(i32 4000)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %21
%36 = load i32, i32* @errno
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %21
br label %42
42: ; preds = %41, %69
store i32 12, i32* %10
%43 = load i32, i32* %6
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%45 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%46 = call i32 @recvfrom(i32 %43, i8* %44, i32 128, i32 0, %struct.sockaddr* %45, i32* %10)
store i32 %46, i32* %9
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %42
%49 = load i32, i32* @errno
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %42
%55 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = load i32, i32* %6
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%59 = load i32, i32* %9
%60 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%61 = call i32 @sendto(i32 %57, i8* %58, i32 %59, i32 0, %struct.sockaddr* %60, i32 12)
%62 = load i32, i32* %9
%63 = icmp ne i32 %61, %62
br i1 %63, label %64, label %69
64: ; preds = %54
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
%68 = call i32 @exit(i32 0)
unreachable
69: ; preds = %54
br label %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"bind address fail, errno=%d\0D\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"receive fail, errno=%d\0D\0A\00"
@.str.3 = [17 x i8] c"Server Rcv: %s\0D\0A\00"
@str = [13 x i8] c"sendto fail\0D\00"
@str.5 = [21 x i8] c"create socket fail!\0D\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 [128 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = load i32, i32* @IPPROTO_UDP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, i64 0, i64 0))
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %2
%20 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = call i32 @htons(i32 4000)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i64 @bind(i32 %14, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %19
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %38
33: ; preds = %19
%34 = load i32, i32* @errno
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = call i32 @close(i32 %14)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31, %46
store i32 12, i32* %5
%39 = call i32 @recvfrom(i32 %14, i8* %10, i32 128, i32 0, %struct.sockaddr* %32, i32* %5)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %38
%42 = load i32, i32* @errno
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = call i32 @close(i32 %14)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %38
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%48 = call i32 @sendto(i32 %14, i8* %10, i32 %39, i32 0, %struct.sockaddr* %32, i32 12)
%49 = icmp eq i32 %48, %39
br i1 %49, label %38, label %50
50: ; preds = %46
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%52 = call i32 @close(i32 %14)
%53 = call i32 @exit(i32 0)
unreachable
}