#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*/ servAddr;
typedef int /*<<< orphan*/ cliAddr;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(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 SERVER_PORT = 6666;
  if (argc > 2)
    printf("param err:\nUsage: %s port | %s\n", argv[0], argv[0]);
  if (argc == 2)
    SERVER_PORT = atoi(argv[1]);
  int servSocket, nbytes;
  struct sockaddr_in servAddr, cliAddr;
  int addrLen = sizeof(cliAddr);
  char buffer[200];
  if ((servSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    printf("socket err");
    exit(1);
  }
  bzero(&servAddr, sizeof(servAddr));
  servAddr.sin_family = AF_INET;
  servAddr.sin_port = htons(SERVER_PORT);
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(servSocket, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
    printf("bind err");
    exit(1);
  }
  printf("Listen Port: %d\nListening ...\n", SERVER_PORT);
  while (1) {
    if ((nbytes = recvfrom(servSocket, buffer, sizeof(buffer), 0,
                           (struct sockaddr *)&cliAddr, &addrLen)) < 0) {
      printf("recvfrom err");
      exit(1);
    }
    printf("\nFrom %s:%d\n", inet_ntoa(cliAddr.sin_addr),
           ntohs(cliAddr.sin_port));
    printf("Recv: %s\nlength: %d\n\n", buffer, nbytes);
    memset(buffer, 0, 200);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1a0a,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    1206 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jne    1220 <main+0x60>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x14(%rbp)
movl   $0xc,-0x3c(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1264 <main+0xa4>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12cd <main+0x10d>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x110(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0xc8,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1320 <main+0x160>
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x34(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x118(%rbp)
mov    -0x38(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0x118(%rbp),%rsi
mov    %eax,%edx
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x110(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1070 <memset@plt>
jmp    12de <main+0x11e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xf8,%rsp
cmp    $0x3,%edi
jl     11f2 <main+0x32>
mov    (%rsi),%rdx
lea    0xe20(%rip),%rdi
mov    %rdx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1a0a,%r15d
jmp    1209 <main+0x49>
mov    $0x1a0a,%r15d
cmp    $0x2,%edi
jne    1209 <main+0x49>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r15d
movl   $0xc,0xc(%rsp)
mov    0x2dd0(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    123b <main+0x7b>
lea    0xdea(%rip),%rdi
jmp    1350 <main+0x190>
mov    %eax,%ebx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x20(%rsp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x24(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1294 <main+0xd4>
lea    0xd9c(%rip),%rdi
jmp    1350 <main+0x190>
lea    0xd99(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x30(%rsp),%rsi
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    $0xc8,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
test   %eax,%eax
js     1349 <main+0x189>
mov    %eax,%r14d
lea    0x30(%rsp),%rbp
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
nopl   0x0(%rax)
mov    0x14(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r15
mov    0x10(%rsp),%edi
call   1090 <ntohs@plt>
lea    0xd64(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd5e(%rip),%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc8,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   1080 <recvfrom@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    12e0 <main+0x120>
lea    0xd03(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@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 = [32 x i8] c"param err:\0AUsage: %s port | %s\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [11 x i8] c"socket err\00"
@INADDR_ANY = global i32 0
@.str.2 = [9 x i8] c"bind err\00"
@.str.3 = [31 x i8] c"Listen Port: %d\0AListening ...\0A\00"
@.str.4 = [13 x i8] c"recvfrom err\00"
@.str.5 = [13 x i8] c"\0AFrom %s:%d\0A\00"
@.str.6 = [22 x i8] c"Recv: %s\0Alength: %d\0A\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca [200 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 6666, i32* %6
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 2
br i1 %14, label %15, label %23
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %21)
br label %23
23: ; preds = %15, %2
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 2
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %6
br label %31
31: ; preds = %26, %23
store i32 12, i32* %11
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_DGRAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31
%40 = call i32 @bzero(%struct.sockaddr_in* %9, i32 12)
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %41, i32* %42
%43 = load i32, i32* %6
%44 = call i32 @htons(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* @INADDR_ANY
%47 = call i32 @htonl(i32 %46)
%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
store i32 %47, i32* %49
%50 = load i32, i32* %7
%51 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%52 = call i64 @bind(i32 %50, %struct.sockaddr* %51, i32 12)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %39
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %39
%58 = load i32, i32* %6
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %57, %69
%61 = load i32, i32* %7
%62 = getelementptr inbounds [200 x i8], [200 x i8]* %12, i64 0, i64 0
%63 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%64 = call i32 @recvfrom(i32 %61, i8* %62, i32 200, i32 0, %struct.sockaddr* %63, i32* %11)
store i32 %64, i32* %8
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %60
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %60
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%71 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i8* @inet_ntoa(i32 %72)
%74 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i32 @ntohs(i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %73, i32 %76)
%78 = getelementptr inbounds [200 x i8], [200 x i8]* %12, i64 0, i64 0
%79 = load i32, i32* %8
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %78, i32 %79)
%81 = getelementptr inbounds [200 x i8], [200 x i8]* %12, i64 0, i64 0
%82 = call i32 @memset(i8* %81, i32 0, i32 200)
br label %60
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [32 x i8] c"param err:\0AUsage: %s port | %s\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [11 x i8] c"socket err\00"
@INADDR_ANY = global i32 0
@.str.2 = [9 x i8] c"bind err\00"
@.str.3 = [31 x i8] c"Listen Port: %d\0AListening ...\0A\00"
@.str.4 = [13 x i8] c"recvfrom err\00"
@.str.5 = [13 x i8] c"\0AFrom %s:%d\0A\00"
@.str.6 = [22 x i8] c"Recv: %s\0Alength: %d\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [200 x i8]
%7 = icmp sgt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %9)
br label %17
11: ; preds = %2
%12 = icmp eq i32 %0, 2
br i1 %12, label %13, label %17
13: ; preds = %11
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
br label %17
17: ; preds = %8, %13, %11
%18 = phi i32 [ %16, %13 ], [ 6666, %11 ], [ 6666, %8 ]
%19 = bitcast %struct.sockaddr_in* %3 to i8*
%20 = bitcast %struct.sockaddr_in* %4 to i8*
%21 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%22 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_DGRAM
%25 = tail call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %17
%31 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = tail call i32 @htons(i32 %18)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = tail call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i64 @bind(i32 %25, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %30
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%47 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%48 = call i32 @recvfrom(i32 %25, i8* %22, i32 200, i32 0, %struct.sockaddr* %47, i32* %5)
%49 = icmp slt i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %45
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %56
53: ; preds = %56, %45
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %50, %56
%57 = phi i32 [ %48, %50 ], [ %65, %56 ]
%58 = load i32, i32* %51
%59 = call i8* @inet_ntoa(i32 %58)
%60 = load i32, i32* %52
%61 = call i32 @ntohs(i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %59, i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %22, i32 %57)
%64 = call i32 @memset(i8* %22, i32 0, i32 200)
%65 = call i32 @recvfrom(i32 %25, i8* %22, i32 200, i32 0, %struct.sockaddr* %47, i32* %5)
%66 = icmp slt i32 %65, 0
br i1 %66, label %53, label %56
}