#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;
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
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) {
  if (argc != 2) {
    printf("Usage: %s port\n", argv[0]);
    exit(1);
  }
  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(atoi(argv[1]));
  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)
cmpl   $0x2,-0x8(%rbp)
je     1202 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    127f <main+0xbf>
lea    0xe04(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12b0 <main+0xf0>
lea    0xdda(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <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    1388 <main+0x1c8>
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    0xd4c(%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    1383 <main+0x1c3>
lea    0xd17(%rip),%rdi
call   10b0 <perror@plt>
jmp    139e <main+0x1de>
jmp    1399 <main+0x1d9>
lea    0xd08(%rip),%rdi
call   10b0 <perror@plt>
jmp    139e <main+0x1de>
jmp    12ba <main+0xfa>
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
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1313 <main+0x143>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de8(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    0x0(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     132e <main+0x15e>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     1337 <main+0x167>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%r15
lea    0x20(%rsp),%r12
lea    0xd9f(%rip),%rbp
nopl   (%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    12f3 <main+0x123>
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    1280 <main+0xb0>
lea    0xd3b(%rip),%rdi
jmp    12fa <main+0x12a>
lea    0xd39(%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
mov    (%rbx),%rsi
lea    0xce3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xcdb(%rip),%rdi
jmp    133e <main+0x16e>
lea    0xcd9(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"Usage: %s port\0A\00"
@.str.1 = [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.2 = [7 x i8] c"socket\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [16 x i8] c"%s %u says: %s\0A\00"
@.str.5 = [7 x i8] c"sendto\00"
@.str.6 = [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 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = call i32 @htonl(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* @AF_INET
%35 = load i32, i32* @SOCK_DGRAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
store i32 %36, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %20
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %20
%42 = load i32, i32* %7
%43 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%44 = call i64 @bind(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
store i32 12, i32* %11
br label %50
50: ; preds = %49, %82
%51 = load i32, i32* %7
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%53 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%54 = call i32 @recvfrom(i32 %51, i8* %52, i32 511, i32 0, %struct.sockaddr* %53, i32* %11)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %80
57: ; preds = %50
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %59
store i8 0, i8* %60
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%62 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i8* @inet_ntoa(i32 %63)
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 @ntohs(i32 %66)
%68 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %64, i32 %67, i8* %68)
%70 = load i32, i32* %7
%71 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%72 = load i32, i32* %10
%73 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%74 = call i32 @sendto(i32 %70, i8* %71, i32 %72, i32 0, %struct.sockaddr* %73, i32 12)
store i32 %74, i32* %10
%75 = load i32, i32* %10
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %57
%78 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %83
79: ; preds = %57
br label %82
80: ; preds = %50
%81 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %83
82: ; preds = %79
br label %50
83: ; preds = %80, %77
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"Usage: %s port\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [16 x i8] c"%s %u says: %s\0A\00"
@.str.5 = [7 x i8] c"sendto\00"
@.str.6 = [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 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [512 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([99 x i8], [99 x i8]* @str, i64 0, i64 0))
%14 = bitcast %struct.sockaddr_in* %3 to i8*
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = tail call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = tail call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_DGRAM
%27 = tail call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %12
%30 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %12
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @bind(i32 %27, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%41 = bitcast %struct.sockaddr_in* %5 to i8*
%42 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%43 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
br label %46
46: ; preds = %49, %39
%47 = call i32 @recvfrom(i32 %27, i8* %40, i32 511, i32 0, %struct.sockaddr* %43, i32* %6)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %59
49: ; preds = %46
%50 = zext i32 %47 to i64
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %50
store i8 0, i8* %51
%52 = load i32, i32* %44
%53 = call i8* @inet_ntoa(i32 %52)
%54 = load i32, i32* %45
%55 = call i32 @ntohs(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %53, i32 %55, i8* %40)
%57 = call i32 @sendto(i32 %27, i8* %40, i32 %47, i32 0, %struct.sockaddr* %43, i32 12)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %46
59: ; preds = %46, %49
%60 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %49 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), %46 ]
%61 = call i32 @perror(i8* %60)
ret i32 0
}