#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__ {
  scalar_t__ 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 /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main() {
  int sockfd;
  struct sockaddr_in cli_addr, serv_addr;
  char buf[524];
  int rcvlen;
  socklen_t addrlen;
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    fprintf(stderr, "Error: socket() failed!\n");
    exit(errno);
  }
  memset(&serv_addr, 0, sizeof(struct sockaddr_in));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(7878);
  inet_aton("127.0.0.1", (struct in_addr *)serv_addr.sin_addr.s_addr);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) ==
      -1) {
    fprintf(stderr, "Error: bind() failed!\n error code:%s\n", strerror(errno));
    exit(errno);
  }
  while (1) {
    rcvlen = recvfrom(sockfd, buf, sizeof(buf) - 1, 0,
                      (struct sockaddr *)&cli_addr, &addrlen);
    if (rcvlen < 0) {
      fprintf(stderr, "Error: recvfrom() failed!\n");
      exit(errno);
    }
    buf[rcvlen] = '\0';
    printf("From %s: %d receive message: %s\n", inet_ntoa(cli_addr.sin_addr),
           ntohs(cli_addr.sin_port), buf);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    121c <main+0x5c>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x1ec6,%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xdc9(%rip),%rdi
call   1080 <inet_aton@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12b1 <main+0xf1>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x25c(%rbp)
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x25c(%rbp),%edi
mov    %rax,%rdx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12b6 <main+0xf6>
mov    -0x8(%rbp),%edi
lea    -0x250(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x20b,%edx
xor    %ecx,%ecx
lea    -0x258(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x254(%rbp)
cmpl   $0x0,-0x254(%rbp)
jge    130f <main+0x14f>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
lea    0xd4f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movslq -0x254(%rbp),%rax
movb   $0x0,-0x250(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x268(%rbp)
mov    -0x20(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x268(%rbp),%rsi
mov    %eax,%edx
lea    -0x250(%rbp),%rcx
lea    0xd17(%rip),%rdi
call   1050 <printf@plt>
jmp    12b6 <main+0xf6>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x258,%rsp
mov    0x2e10(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    1204 <main+0x44>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
jmp    1329 <main+0x169>
mov    %eax,%r12d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
mov    $0x1ec6,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x18(%rsp),%rsi
lea    0xddd(%rip),%rdi
call   1080 <inet_aton@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1289 <main+0xc9>
mov    0x2d73(%rip),%rax
mov    (%rax),%ebx
mov    0x2d62(%rip),%rbp
mov    0x0(%rbp),%edi
call   10d0 <strerror@plt>
lea    0xdae(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x0(%rbp),%edi
call   10c0 <exit@plt>
lea    0x40(%rsp),%rsi
lea    0x28(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %r12d,%edi
mov    $0x20b,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
test   %eax,%eax
js     1319 <main+0x159>
lea    0xdb2(%rip),%r14
lea    0x40(%rsp),%rbx
lea    0x28(%rsp),%r15
lea    0xc(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
movb   $0x0,0x40(%rsp,%rax,1)
mov    0x30(%rsp),%rdi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x28(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1050 <printf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x20b,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r13,%r9
call   1090 <recvfrom@plt>
test   %eax,%eax
jns    12d0 <main+0x110>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
lea    0xd20(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Error: socket() failed!\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [38 x i8] c"Error: bind() failed!\0A error code:%s\0A\00"
@.str.3 = [27 x i8] c"Error: recvfrom() failed!\0A\00"
@.str.4 = [33 x i8] c"From %s: %d receive message: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [524 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @errno
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %0
%18 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 24)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 7878)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to %struct.in_addr*
%27 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.in_addr* %26)
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %17
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @errno
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %17
br label %40
40: ; preds = %39, %52
%41 = load i32, i32* %2
%42 = getelementptr inbounds [524 x i8], [524 x i8]* %5, i64 0, i64 0
%43 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%44 = call i32 @recvfrom(i32 %41, i8* %42, i32 523, i32 0, %struct.sockaddr* %43, i32* %7)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %40
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @errno
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %40
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [524 x i8], [524 x i8]* %5, i64 0, i64 %54
store i8 0, i8* %55
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
%58 = load i64, i64* %57
%59 = call i8* @inet_ntoa(i64 %58)
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @ntohs(i32 %61)
%63 = getelementptr inbounds [524 x i8], [524 x i8]* %5, i64 0, i64 0
%64 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %59, i32 %62, i8* %63)
br label %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Error: socket() failed!\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [38 x i8] c"Error: bind() failed!\0A error code:%s\0A\00"
@.str.3 = [27 x i8] c"Error: recvfrom() failed!\0A\00"
@.str.4 = [33 x i8] c"From %s: %d receive message: %s\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca [524 x i8]
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = getelementptr inbounds [524 x i8], [524 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @errno
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 24)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 7878)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to %struct.in_addr*
%27 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.in_addr* %26)
%28 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%29 = call i32 @bind(i32 %11, %struct.sockaddr* %28, i32 4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %38, label %31
31: ; preds = %18
%32 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%33 = call i32 @recvfrom(i32 %11, i8* %7, i32 523, i32 0, %struct.sockaddr* %32, i32* %4)
%34 = icmp slt i32 %33, 0
br i1 %34, label %45, label %35
35: ; preds = %31
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
br label %50
38: ; preds = %18
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @errno
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %50, %31
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* @errno
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %35, %50
%51 = phi i32 [ %33, %35 ], [ %59, %50 ]
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds [524 x i8], [524 x i8]* %3, i64 0, i64 %52
store i8 0, i8* %53
%54 = load i64, i64* %36
%55 = call i8* @inet_ntoa(i64 %54)
%56 = load i32, i32* %37
%57 = call i32 @ntohs(i32 %56)
%58 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %55, i32 %57, i8* %7)
%59 = call i32 @recvfrom(i32 %11, i8* %7, i32 523, i32 0, %struct.sockaddr* %32, i32* %4)
%60 = icmp slt i32 %59, 0
br i1 %60, label %45, label %50
}