#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ clientAddr;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, 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) {
  printf("Welcome! This is a UDP server, I can only received message from "
         "client and reply with same message\n");
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(6668);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  int sock;
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    exit(1);
  }
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    exit(1);
  }
  char buff[512];
  struct sockaddr_in clientAddr;
  int n;
  int len = sizeof(clientAddr);
  while (1) {
    n = recvfrom(sock, buff, 511, 0, (struct sockaddr *)&clientAddr, &len);
    if (n > 0) {
      buff[n] = 0;
      printf("%s %u says: %s\n", inet_ntoa(clientAddr.sin_addr),
             ntohs(clientAddr.sin_port), buff);
      n = sendto(sock, buff, n, 0, (struct sockaddr *)&clientAddr,
                 sizeof(clientAddr));
      if (n < 0) {
        perror("sendto");
        break;
      }
    } else {
      perror("recv");
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1a0c,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    123c <main+0x8c>
lea    0xe37(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    126d <main+0xbd>
lea    0xe0d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0xc,-0x248(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x240(%rbp),%r8
mov    $0x1ff,%edx
xor    %ecx,%ecx
lea    -0x248(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x0,-0x244(%rbp)
jle    1345 <main+0x195>
movslq -0x244(%rbp),%rax
movb   $0x0,-0x230(%rbp,%rax,1)
mov    -0x23c(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x250(%rbp)
mov    -0x240(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0x250(%rbp),%rsi
mov    %eax,%edx
lea    -0x230(%rbp),%rcx
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x244(%rbp),%edx
lea    -0x240(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x0,-0x244(%rbp)
jge    1340 <main+0x190>
lea    0xd4a(%rip),%rdi
call   10b0 <perror@plt>
jmp    135b <main+0x1ab>
jmp    1356 <main+0x1a6>
lea    0xd3b(%rip),%rdi
call   10b0 <perror@plt>
jmp    135b <main+0x1ab>
jmp    1277 <main+0xc7>
xor    %eax,%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e04(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x1a0c,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    (%rbx),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12f3 <main+0x133>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     12fc <main+0x13c>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%r15
lea    0x20(%rsp),%r12
lea    0xdb3(%rip),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1ff,%edx
xor    %ecx,%ecx
mov    %r12,%r8
lea    0xc(%rsp),%r9
call   1090 <recvfrom@plt>
test   %eax,%eax
jle    12d3 <main+0x113>
mov    %eax,%r14d
mov    %eax,%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    0x24(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r13
mov    0x20(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %eax,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
test   %eax,%eax
jns    1260 <main+0xa0>
lea    0xd4b(%rip),%rdi
jmp    12da <main+0x11a>
lea    0xd49(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
add    $0x238,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd06(%rip),%rdi
jmp    1303 <main+0x143>
lea    0xd04(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [100 x i8] c"Welcome! This is a UDP server, I can only received message from client and reply with same message\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [16 x i8] c"%s %u says: %s\0A\00"
@.str.4 = [7 x i8] c"sendto\00"
@.str.5 = [5 x i8] c"recv\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 6668)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %2
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
store i32 12, i32* %11
br label %37
37: ; preds = %36, %69
%38 = load i32, i32* %7
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%40 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%41 = call i32 @recvfrom(i32 %38, i8* %39, i32 511, i32 0, %struct.sockaddr* %40, i32* %11)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %67
44: ; preds = %37
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %46
store i8 0, i8* %47
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i8* @inet_ntoa(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @ntohs(i32 %53)
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %51, i32 %54, i8* %55)
%57 = load i32, i32* %7
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%59 = load i32, i32* %10
%60 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%61 = call i32 @sendto(i32 %57, i8* %58, i32 %59, i32 0, %struct.sockaddr* %60, i32 12)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %44
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %70
66: ; preds = %44
br label %69
67: ; preds = %37
%68 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %70
69: ; preds = %66
br label %37
70: ; preds = %67, %64
ret i32 0
}

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
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [16 x i8] c"%s %u says: %s\0A\00"
@.str.4 = [7 x i8] c"sendto\00"
@.str.5 = [5 x i8] c"recv\00"
@str = [99 x i8] c"Welcome! This is a UDP server, I can only received message from client and reply with same message\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [512 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([99 x i8], [99 x i8]* @str, i64 0, i64 0))
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 6668)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = tail call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %18, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%32 = bitcast %struct.sockaddr_in* %5 to i8*
%33 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%34 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
br label %37
37: ; preds = %40, %30
%38 = call i32 @recvfrom(i32 %18, i8* %31, i32 511, i32 0, %struct.sockaddr* %34, i32* %6)
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %50
40: ; preds = %37
%41 = zext i32 %38 to i64
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %41
store i8 0, i8* %42
%43 = load i32, i32* %35
%44 = call i8* @inet_ntoa(i32 %43)
%45 = load i32, i32* %36
%46 = call i32 @ntohs(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %46, i8* %31)
%48 = call i32 @sendto(i32 %18, i8* %31, i32 %38, i32 0, %struct.sockaddr* %34, i32 12)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %37
50: ; preds = %37, %40
%51 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %40 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), %37 ]
%52 = call i32 @perror(i8* %51)
ret i32 0
}