#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 socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ send_msg;
typedef int /*<<< orphan*/ recv_msg;
typedef int /*<<< orphan*/ cli_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*/ bzero(void *, int);
int /*<<< orphan*/ close(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 *, char *, int, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd;
  socklen_t len;
  int n;
  char recv_msg[256] = {'\0'};
  char send_msg[256] = {'\0'};
  struct sockaddr_in serv_addr, cli_addr;
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    exit(-1);
  }
  bzero((void *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(5002);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("bind");
    exit(-1);
  }
  while (1) {
    len = sizeof(cli_addr);
    bzero((void *)recv_msg, sizeof(recv_msg));
    bzero((void *)send_msg, sizeof(send_msg));
    n = recvfrom(sockfd, recv_msg, sizeof(recv_msg), 0,
                 (struct sockaddr *)&cli_addr, &len);
    if (-1 == n || 0 == n) {
      perror("recvfrom");
      close(sockfd);
      exit(-1);
    }
    printf("[server] recv msg from %s:%d, msg = %s.\n",
           inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), recv_msg);
  }
  exit(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    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    124a <main+0x7a>
lea    0xdc5(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
lea    -0x230(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x228(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x22c(%rbp)
mov    $0x138a,%edi
call   1040 <htons@plt>
mov    %eax,-0x230(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12c2 <main+0xf2>
lea    0xd54(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
jmp    12c7 <main+0xf7>
movl   $0xc,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
lea    -0x220(%rbp),%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x240(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x18(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
je     132d <main+0x15d>
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    134b <main+0x17b>
lea    0xcd8(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x23c(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x248(%rbp)
mov    -0x240(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x248(%rbp),%rsi
mov    %eax,%edx
lea    -0x120(%rbp),%rcx
lea    0xc96(%rip),%rdi
call   1050 <printf@plt>
jmp    12c7 <main+0xf7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
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    0x2d9c(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    126c <main+0xac>
lea    0xd96(%rip),%rdi
jmp    12c4 <main+0x104>
mov    %eax,%r14d
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x24(%rsp)
mov    $0x138a,%edi
call   1040 <htons@plt>
mov    %eax,0x20(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    12d3 <main+0x113>
lea    0xd43(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
add    $0x1,%eax
cmp    $0x2,%eax
jb     138a <main+0x1ca>
lea    0xcfd(%rip),%r15
lea    0x30(%rsp),%rbp
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    0x14(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%edi
call   1090 <ntohs@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %rbp,%rcx
call   1050 <printf@plt>
movl   $0xc,0xc(%rsp)
mov    %rbp,%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   1080 <recvfrom@plt>
add    $0x1,%eax
cmp    $0x2,%eax
jae    1330 <main+0x170>
lea    0xc7b(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%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 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [9 x i8] c"recvfrom\00"
@.str.3 = [41 x i8] c"[server] recv msg from %s:%d, msg = %s.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca [256 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = bitcast [256 x i8]* %9 to i8*
%14 = bitcast [256 x i8]* %10 to i8*
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = bitcast %struct.sockaddr_in* %11 to i8*
%24 = call i32 @bzero(i8* %23, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = call i32 @htons(i32 5002)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %22
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %22
br label %41
41: ; preds = %40, %60
store i32 12, i32* %7
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%43 = call i32 @bzero(i8* %42, i32 256)
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%45 = call i32 @bzero(i8* %44, i32 256)
%46 = load i32, i32* %6
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%48 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%49 = call i32 @recvfrom(i32 %46, i8* %47, i32 256, i32 0, %struct.sockaddr* %48, i32* %7)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = icmp eq i32 -1, %50
br i1 %51, label %55, label %52
52: ; preds = %41
%53 = load i32, i32* %8
%54 = icmp eq i32 0, %53
br i1 %54, label %55, label %60
55: ; preds = %52, %41
%56 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = call i32 @exit(i32 -1)
unreachable
60: ; preds = %52
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, 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* %12, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 @ntohs(i32 %66)
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%69 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %64, i32 %67, i8* %68)
br label %41
}

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
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [9 x i8] c"recvfrom\00"
@.str.3 = [41 x i8] c"[server] recv msg from %s:%d, msg = %s.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = call i32 @bzero(i8* %9, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
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* %5, i64 0, i32 1, i32 0
store i32 %23, i32* %24
%25 = tail call i32 @htons(i32 5002)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%28 = call i64 @bind(i32 %13, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %39, label %30
30: ; preds = %18
%31 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
store i32 12, i32* %3
%32 = call i32 @bzero(i8* %8, i32 256)
%33 = call i32 @recvfrom(i32 %13, i8* %8, i32 256, i32 0, %struct.sockaddr* %31, i32* %3)
%34 = add i32 %33, 1
%35 = icmp ult i32 %34, 2
br i1 %35, label %42, label %36
36: ; preds = %30
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
br label %46
39: ; preds = %18
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %46, %30
%43 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @close(i32 %13)
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %36, %46
%47 = load i32, i32* %37
%48 = call i8* @inet_ntoa(i32 %47)
%49 = load i32, i32* %38
%50 = call i32 @ntohs(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %48, i32 %50, i8* %8)
store i32 12, i32* %3
%52 = call i32 @bzero(i8* %8, i32 256)
%53 = call i32 @recvfrom(i32 %13, i8* %8, i32 256, i32 0, %struct.sockaddr* %31, i32* %3)
%54 = add i32 %53, 1
%55 = icmp ult i32 %54, 2
br i1 %55, label %42, label %46
}