#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sockListen;
  if ((sockListen = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    printf("socket fail\n");
    return -1;
  }
  int set = 1;
  setsockopt(sockListen, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(int));
  struct sockaddr_in recvAddr;
  memset(&recvAddr, 0, sizeof(struct sockaddr_in));
  recvAddr.sin_family = AF_INET;
  recvAddr.sin_port = htons(4001);
  recvAddr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sockListen, (struct sockaddr *)&recvAddr, sizeof(struct sockaddr)) ==
      -1) {
    printf("bind fail\n");
    return -1;
  }
  int recvbytes;
  char recvbuf[128];
  int addrLen = sizeof(struct sockaddr_in);
  if ((recvbytes = recvfrom(sockListen, recvbuf, 128, 0,
                            (struct sockaddr *)&recvAddr, &addrLen)) != -1) {
    recvbuf[recvbytes] = '\0';
    printf("receive a broadCast messgse:%s\n", recvbuf);
  } else {
    printf("recvfrom fail\n");
  }
  close(sockListen);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11d1 <main+0x51>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <main+0x15f>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0xfa1,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1265 <main+0xe5>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <main+0x15f>
movl   $0xc,-0xa4(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
lea    -0x18(%rbp),%r8
mov    $0x80,%edx
xor    %ecx,%ecx
lea    -0xa4(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     12c2 <main+0x142>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rsi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d0 <main+0x150>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xa0,%rsp
mov    0x2e3d(%rip),%r15
mov    (%r15),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     127d <main+0xed>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    $0xfa1,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     1286 <main+0xf6>
movl   $0xc,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%r8
lea    0x8(%rsp),%r9
mov    %ebx,%edi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1299 <main+0x109>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xd91(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12a5 <main+0x115>
lea    0xdb4(%rip),%rdi
jmp    128d <main+0xfd>
lea    0xda1(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    12ae <main+0x11e>
lea    0xd80(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket fail\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [11 x i8] c"bind fail\0A\00"
@.str.2 = [32 x i8] c"receive a broadCast messgse:%s\0A\00"
@.str.3 = [15 x i8] c"recvfrom fail\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [128 x i8]
%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 eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %50
14: ; preds = %0
store i32 1, i32* %3
%15 = load i32, i32* %2
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %3, i32 4)
%19 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 4001)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %14
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %50
33: ; preds = %14
store i32 12, i32* %7
%34 = load i32, i32* %2
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i32 @recvfrom(i32 %34, i8* %35, i32 128, i32 0, %struct.sockaddr* %36, i32* %7)
store i32 %37, i32* %5
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %33
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %41
store i8 0, i8* %42
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %47
45: ; preds = %33
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %39
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
store i32 0, i32* %1
br label %50
50: ; preds = %47, %31, %12
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [32 x i8] c"receive a broadCast messgse:%s\0A\00"
@str = [14 x i8] c"recvfrom fail\00"
@str.4 = [10 x i8] c"bind fail\00"
@str.5 = [12 x i8] c"socket fail\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %44
11: ; preds = %0
%12 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %7, i32 %13, i32 %14, i32* %1, i32 4)
%16 = bitcast %struct.sockaddr_in* %2 to i8*
%17 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 4001)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%25 = call i32 @bind(i32 %7, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %11
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
br label %42
29: ; preds = %11
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%31 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%32 = call i32 @recvfrom(i32 %7, i8* %30, i32 128, i32 0, %struct.sockaddr* %24, i32* %4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %38, label %34
34: ; preds = %29
%35 = sext i32 %32 to i64
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %35
store i8 0, i8* %36
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %40
38: ; preds = %29
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = call i32 @close(i32 %7)
br label %42
42: ; preds = %40, %27
%43 = phi i32 [ -1, %27 ], [ 0, %40 ]
br label %44
44: ; preds = %42, %9
%45 = phi i32 [ -1, %9 ], [ %43, %42 ]
ret i32 %45
}