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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, char, char, char, char);
int /*<<< orphan*/ recvfrom(int, void *, int, int /*<<< orphan*/,
                            struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncmp(char *, char *, int);
int main(int argc, char **argv) {
  if (argc != 1) {
    fprintf(stderr, "usage: %s ARGUMENTS...\n", argv[0]);
    return EXIT_FAILURE;
  }
  struct sockaddr_in sockaddr;
  sockaddr.sin_family = AF_INET;
  sockaddr.sin_port = htons(9999);
  sockaddr.sin_addr.s_addr = INADDR_ANY;
  int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  for (;;) {
    char buf[4];
    struct sockaddr_in sender;
    socklen_t socklen;
    recvfrom(sock, (void *)buf, 4, 0, (struct sockaddr *)&sender, &socklen);
    printf("received %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
    if (!strncmp(buf, "PING", 4)) {
      sendto(sock, "PONG", 4, 0, (struct sockaddr *)&sender, socklen);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     11cb <main+0x4b>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x270f,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0x4,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    -0x28(%rbp),%dl
mov    -0x27(%rbp),%cl
mov    -0x26(%rbp),%al
lea    0xdc9(%rip),%rdi
movsbl %dl,%esi
movsbl %cl,%edx
movsbl %al,%ecx
movsbl -0x25(%rbp),%r8d
call   1050 <printf@plt>
lea    -0x28(%rbp),%rdi
lea    0xdbe(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    129e <main+0x11e>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%r8
mov    -0x3c(%rbp),%r9d
lea    0xd9e(%rip),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
jmp    1224 <main+0xa4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x1,%edi
jne    1276 <main+0xf6>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x270f,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %rsp,%r14
lea    0x18(%rsp),%r15
lea    0x4(%rsp),%r12
lea    0xe12(%rip),%r13
lea    0xe1e(%rip),%rbp
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1080 <recvfrom@plt>
movsbl (%rsp),%esi
movsbl 0x1(%rsp),%edx
movsbl 0x2(%rsp),%ecx
movsbl 0x3(%rsp),%r8d
mov    %r13,%rdi
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1210 <main+0x90>
mov    0x4(%rsp),%r9d
mov    %ebx,%edi
lea    0xdcb(%rip),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1060 <sendto@plt>
jmp    1210 <main+0x90>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd77(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage: %s ARGUMENTS...\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [19 x i8] c"received %c%c%c%c\0A\00"
@.str.2 = [5 x i8] c"PING\00"
@.str.3 = [5 x i8] c"PONG\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 [4 x i8]
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 1
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
ret i32 %19
20: ; preds = %2
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 9999)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* @PF_INET
%29 = load i32, i32* @SOCK_DGRAM
%30 = load i32, i32* @IPPROTO_UDP
%31 = call i32 @socket(i32 %28, i32 %29, i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%34 = call i32 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
br label %35
35: ; preds = %57, %20
%36 = load i32, i32* %7
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%38 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%39 = call i32 @recvfrom(i32 %36, i8* %37, i32 4, i32 0, %struct.sockaddr* %38, i32* %10)
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%41 = load i8, i8* %40
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 1
%43 = load i8, i8* %42
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 2
%45 = load i8, i8* %44
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 3
%47 = load i8, i8* %46
%48 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8 signext %41, i8 signext %43, i8 signext %45, i8 signext %47)
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%50 = call i32 @strncmp(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%51 = icmp ne i32 %50, 0
br i1 %51, label %57, label %52
52: ; preds = %35
%53 = load i32, i32* %7
%54 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%55 = load i32, i32* %10
%56 = call i32 @sendto(i32 %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4, i32 0, %struct.sockaddr* %54, i32 %55)
br label %57
57: ; preds = %52, %35
br label %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage: %s ARGUMENTS...\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [19 x i8] c"received %c%c%c%c\0A\00"
@.str.2 = [5 x i8] c"PING\00"
@.str.3 = [5 x i8] c"PONG\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [4 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = icmp eq i32 %0, 1
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
ret i32 %12
13: ; preds = %2
%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 = tail call i32 @htons(i32 9999)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = load i32, i32* @IPPROTO_UDP
%24 = tail call i32 @socket(i32 %21, i32 %22, i32 %23)
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%28 = bitcast %struct.sockaddr_in* %5 to i8*
%29 = bitcast i32* %6 to i8*
%30 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
br label %34
34: ; preds = %46, %13
%35 = call i32 @recvfrom(i32 %24, i8* %27, i32 4, i32 0, %struct.sockaddr* %30, i32* %6)
%36 = load i8, i8* %27
%37 = load i8, i8* %31
%38 = load i8, i8* %32
%39 = load i8, i8* %33
%40 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8 signext %36, i8 signext %37, i8 signext %38, i8 signext %39)
%41 = call i32 @strncmp(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = load i32, i32* %6
%45 = call i32 @sendto(i32 %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4, i32 0, %struct.sockaddr* %30, i32 %44)
br label %46
46: ; preds = %43, %34
br label %34
}