#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*/ remaddr;
typedef int /*<<< orphan*/ myaddr;

/* 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*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
scalar_t__ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                  struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  struct sockaddr_in myaddr;
  struct sockaddr_in remaddr;
  socklen_t addrlen = sizeof(remaddr);
  int fd;
  int port;
  char buf[100];
  if (argc == 2) {
    port = atoi(argv[1]);
  } else {
    printf("Usage: %s <port_number>\n", argv[0]);
    exit(1);
  }
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("cannot create socket\n");
    exit(1);
  }
  memset((char *)&myaddr, 0, sizeof(myaddr));
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(port);
  if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
    perror("bind failed");
    exit(1);
  }
  for (;;) {
    printf("\nListening on port %d ...\n", port);
    int recvlen =
        recvfrom(fd, buf, 100, 0, (struct sockaddr *)&remaddr, &addrlen);
    if (recvlen >= 0) {
      buf[recvlen] = 0;
      printf("\nPing Message Received\n");
    } else {
      printf("\nMessage Receive Failed\n");
      printf("\n---------------\n");
      continue;
    }
    if (sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen) <
        0) {
      perror("\nMessage Send Failed\n");
    } else {
      printf("\nPong Message Send\n");
    }
    printf("\n---------------\n");
  }
  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)
movl   $0xc,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11ff <main+0x3f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x3c(%rbp)
jmp    121e <main+0x5e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    1259 <main+0x99>
lea    0xdcf(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12c2 <main+0x102>
lea    0xd7c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    12c7 <main+0x107>
mov    -0x3c(%rbp),%esi
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%edi
lea    -0xa0(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x64,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jl     132b <main+0x16b>
movslq -0xa4(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    134c <main+0x18c>
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12c7 <main+0x107>
mov    -0x38(%rbp),%eax
mov    %eax,-0xb4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0xa0(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
lea    -0x30(%rbp),%r8
mov    -0x34(%rbp),%r9d
xor    %ecx,%ecx
call   1080 <sendto@plt>
cmp    $0x0,%rax
jge    13a8 <main+0x1e8>
lea    0xcfb(%rip),%rdi
call   10b0 <perror@plt>
jmp    13b6 <main+0x1f6>
lea    0xd00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xcca(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12c7 <main+0x107>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
movl   $0xc,0xc(%rsp)
cmp    $0x2,%edi
jne    132c <main+0x15c>
mov    0x8(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%r15d
mov    0x2de3(%rip),%r14
mov    (%r14),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1347 <main+0x177>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     1350 <main+0x180>
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0xe25(%rip),%r14
jmp    12a4 <main+0xd4>
nopl   0x0(%rax,%rax,1)
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
lea    0xd90(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   10a0 <recvfrom@plt>
test   %eax,%eax
js     1290 <main+0xc0>
mov    %eax,%eax
movb   $0x0,0x30(%rsp,%rax,1)
lea    0xda6(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0xc(%rsp),%r9d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1090 <sendto@plt>
test   %rax,%rax
jns    1320 <main+0x150>
lea    0xd49(%rip),%rdi
call   10c0 <perror@plt>
jmp    129c <main+0xcc>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xd74(%rip),%rdi
jmp    1297 <main+0xc7>
mov    (%rsi),%rsi
lea    0xcca(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xccb(%rip),%rdi
jmp    1357 <main+0x187>
lea    0xcd8(%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 = [25 x i8] c"Usage: %s <port_number>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [22 x i8] c"cannot create socket\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [12 x i8] c"bind failed\00"
@.str.3 = [27 x i8] c"\0AListening on port %d ...\0A\00"
@.str.4 = [24 x i8] c"\0APing Message Received\0A\00"
@.str.5 = [25 x i8] c"\0AMessage Receive Failed\0A\00"
@.str.6 = [18 x i8] c"\0A---------------\0A\00"
@.str.7 = [22 x i8] c"\0AMessage Send Failed\0A\00"
@.str.8 = [20 x i8] c"\0APong Message Send\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [100 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12, i32* %8
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %10
br label %26
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %9
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = bitcast %struct.sockaddr_in* %6 to i8*
%36 = call i32 @memset(i8* %35, i32 0, i32 12)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %37, i32* %38
%39 = load i32, i32* @INADDR_ANY
%40 = call i32 @htonl(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
store i32 %40, i32* %42
%43 = load i32, i32* %10
%44 = call i32 @htons(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %9
%47 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%48 = call i64 @bind(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %34
%51 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %34
br label %54
54: ; preds = %84, %68, %53
%55 = load i32, i32* %10
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %9
%58 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%59 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%60 = call i32 @recvfrom(i32 %57, i8* %58, i32 100, i32 0, %struct.sockaddr* %59, i32* %8)
store i32 %60, i32* %12
%61 = load i32, i32* %12
%62 = icmp sge i32 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %54
%64 = load i32, i32* %12
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 %65
store i8 0, i8* %66
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %71
68: ; preds = %54
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %54
71: ; preds = %63
%72 = load i32, i32* %9
%73 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%74 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%75 = call i32 @strlen(i8* %74)
%76 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%77 = load i32, i32* %8
%78 = call i64 @sendto(i32 %72, i8* %73, i32 %75, i32 0, %struct.sockaddr* %76, i32 %77)
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %71
%81 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %84
82: ; preds = %71
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
br label %84
84: ; preds = %82, %80
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %54
}

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