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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int count, char *strings[]) {
  int sd;
  int port = 9999;
  struct sockaddr_in addr;
  char buffer[1024];
  if (count != 2)
    printf("usage: %s <port>\n...Using default port (%d).\n", strings[0], port);
  else
    port = atoi(strings[1]);
  sd = socket(PF_INET, SOCK_DGRAM, 0);
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
    perror("bind");
  while (1) {
    int bytes, addr_len = sizeof(addr);
    bytes = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&addr,
                     &addr_len);
    printf("msg from %s:%d (%d bytes)\n", inet_ntoa(addr.sin_addr),
           ntohs(addr.sin_port), bytes);
    sendto(sd, buffer, bytes, 0, (struct sockaddr *)&addr, sizeof(addr));
  }
  close(sd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x270f,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f7 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%edx
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1207 <main+0x57>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     127b <main+0xcb>
lea    0xdb8(%rip),%rdi
call   10a0 <perror@plt>
jmp    1280 <main+0xd0>
movl   $0xc,-0x438(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x438(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x434(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x440(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <ntohs@plt>
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
mov    -0x434(%rbp),%ecx
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x434(%rbp),%edx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
jmp    1280 <main+0xd0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
cmp    $0x2,%edi
jne    11d4 <main+0x24>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
jmp    11f0 <main+0x40>
mov    (%rsi),%rsi
lea    0xe22(%rip),%rdi
mov    $0x270f,%r14d
mov    $0x270f,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jne    12d1 <main+0x121>
lea    0x10(%rsp),%r14
mov    %rsp,%r15
lea    0xdd0(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    0xc(%rsp),%r9
call   1070 <recvfrom@plt>
mov    %eax,%ebp
mov    0x4(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r12
mov    (%rsp),%edi
call   1080 <ntohs@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
jmp    1270 <main+0xc0>
lea    0xd56(%rip),%rdi
call   10a0 <perror@plt>
jmp    1254 <main+0xa4>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [46 x i8] c"usage: %s <port>\0A...Using default port (%d).\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [27 x i8] c"msg from %s:%d (%d bytes)\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 %struct.sockaddr_in
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 9999, i32* %7
%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 = load i32, i32* %7
%19 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
br label %25
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %7
br label %25
25: ; preds = %20, %14
%26 = load i32, i32* @PF_INET
%27 = load i32, i32* @SOCK_DGRAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %6
%29 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* %7
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* @INADDR_ANY
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%40 = call i64 @bind(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %25
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %42, %25
br label %45
45: ; preds = %44, %45
store i32 12, i32* %11
%46 = load i32, i32* %6
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%48 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%49 = call i32 @recvfrom(i32 %46, i8* %47, i32 1024, i32 0, %struct.sockaddr* %48, i32* %11)
store i32 %49, i32* %10
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i8* @inet_ntoa(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @ntohs(i32 %55)
%57 = load i32, i32* %10
%58 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %53, i32 %56, i32 %57)
%59 = load i32, i32* %6
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%61 = load i32, i32* %10
%62 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%63 = call i32 @sendto(i32 %59, i8* %60, i32 %61, i32 0, %struct.sockaddr* %62, i32 12)
br label %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [46 x i8] c"usage: %s <port>\0A...Using default port (%d).\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [27 x i8] c"msg from %s:%d (%d bytes)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = icmp eq i32 %0, 2
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %10, i32 9999)
br label %16
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
br label %16
16: ; preds = %12, %9
%17 = phi i32 [ 9999, %9 ], [ %15, %12 ]
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = tail call i32 @htons(i32 %17)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @bind(i32 %20, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %16
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %16
%34 = bitcast i32* %5 to i8*
br label %35
35: ; preds = %35, %33
store i32 12, i32* %5
%36 = call i32 @recvfrom(i32 %20, i8* %7, i32 1024, i32 0, %struct.sockaddr* %28, i32* %5)
%37 = load i32, i32* %27
%38 = call i8* @inet_ntoa(i32 %37)
%39 = load i32, i32* %25
%40 = call i32 @ntohs(i32 %39)
%41 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %38, i32 %40, i32 %36)
%42 = call i32 @sendto(i32 %20, i8* %7, i32 %36, i32 0, %struct.sockaddr* %28, i32 12)
br label %35
}