#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;
};
struct in_addr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ mesg;
typedef int /*<<< orphan*/ cliaddr;
typedef int /*<<< orphan*/ cli_ip;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EIO;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, struct in_addr *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int, char *);
scalar_t__ 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;
  struct sockaddr_in servaddr, cliaddr;
  socklen_t len = sizeof(cliaddr);
  char cli_ip[32];
  char mesg[1024];
  sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0) {
    perror("socket: ");
    return -EIO;
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(54321);
  if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("bind: ");
    close(sockfd);
    return -EIO;
  }
  while (1) {
    memset(mesg, 0, sizeof(mesg));
    if (recvfrom(sockfd, mesg, sizeof(mesg), 0, (struct sockaddr *)&cliaddr,
                 &len) > 0) {
      inet_ntop(AF_INET, (struct in_addr *)&cliaddr.sin_addr, cli_ip,
                sizeof(cli_ip));
      printf("From %s port %d: %s", cli_ip, ntohs(cliaddr.sin_port), mesg);
    }
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xc,-0x34(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1225 <main+0x65>
lea    0xdf3(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
mov    0x2dad(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1332 <main+0x172>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xd431,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    129f <main+0xdf>
lea    0xd8a(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
mov    0x2d33(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1332 <main+0x172>
jmp    12a4 <main+0xe4>
lea    -0x460(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x460(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1090 <recvfrom@plt>
cmp    $0x0,%rax
jle    132d <main+0x16d>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
lea    -0x60(%rbp),%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
lea    -0x60(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x30(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x468(%rbp),%rsi
mov    %eax,%edx
lea    -0x460(%rbp),%rcx
lea    0xce8(%rip),%rdi
call   1050 <printf@plt>
jmp    12a4 <main+0xe4>
mov    -0x4(%rbp),%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x458,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12d2 <main+0x112>
mov    %eax,%r13d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0xd431,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     12e0 <main+0x120>
lea    0x50(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0x30(%rsp),%rbp
lea    0xda0(%rip),%r14
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1090 <recvfrom@plt>
test   %rax,%rax
jle    1270 <main+0xb0>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0x24(%rsp),%rsi
mov    %rbp,%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
mov    0x20(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1050 <printf@plt>
jmp    1270 <main+0xb0>
lea    0xd27(%rip),%rdi
call   10c0 <perror@plt>
jmp    12f4 <main+0x134>
lea    0xd22(%rip),%rdi
call   10c0 <perror@plt>
mov    %r13d,%edi
call   1080 <close@plt>
mov    0x2ccd(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x458,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.in_addr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [9 x i8] c"socket: \00"
@EIO = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [7 x i8] c"bind: \00"
@.str.2 = [20 x i8] c"From %s port %d: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca [32 x i8]
%11 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12, i32* %9
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EIO
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %63
21: ; preds = %2
%22 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = call i32 @htons(i32 54321)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %21
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @EIO
%40 = sub nsw i32 0, %39
store i32 %40, i32* %3
br label %63
41: ; preds = %21
br label %42
42: ; preds = %41, %62
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i32 0, i32 1024)
%45 = load i32, i32* %6
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%47 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%48 = call i64 @recvfrom(i32 %45, i8* %46, i32 1024, i32 0, %struct.sockaddr* %47, i32* %9)
%49 = icmp sgt i64 %48, 0
br i1 %49, label %50, label %62
50: ; preds = %42
%51 = load i32, i32* @AF_INET
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%53 = bitcast %struct.TYPE_2__* %52 to %struct.in_addr*
%54 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%55 = call i32 @inet_ntop(i32 %51, %struct.in_addr* %53, i8* %54, i32 32)
%56 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @ntohs(i32 %58)
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%61 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %56, i32 %59, i8* %60)
br label %62
62: ; preds = %50, %42
br label %42
63: ; preds = %35, %17
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.in_addr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [9 x i8] c"socket: \00"
@EIO = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [7 x i8] c"bind: \00"
@.str.2 = [20 x i8] c"From %s port %d: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca [1024 x i8]
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %50
19: ; preds = %2
%20 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = tail call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = tail call i32 @htons(i32 54321)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @bind(i32 %15, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %19
%32 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%34 = bitcast %struct.TYPE_2__* %33 to %struct.in_addr*
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %39
36: ; preds = %19
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @close(i32 %15)
br label %50
39: ; preds = %49, %31
%40 = call i32 @memset(i8* %12, i32 0, i32 1024)
%41 = call i64 @recvfrom(i32 %15, i8* %12, i32 1024, i32 0, %struct.sockaddr* %32, i32* %5)
%42 = icmp sgt i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %39
%44 = load i32, i32* @AF_INET
%45 = call i32 @inet_ntop(i32 %44, %struct.in_addr* %34, i8* %11, i32 32)
%46 = load i32, i32* %35
%47 = call i32 @ntohs(i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %11, i32 %47, i8* %12)
br label %49
49: ; preds = %43, %39
br label %39
50: ; preds = %36, %17
%51 = load i32, i32* @EIO
%52 = sub nsw i32 0, %51
ret i32 %52
}