#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ op;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(void) {
  int fd;
  struct sockaddr_in addr = {};
  if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
    do {
      fprintf(stderr, "[%s][%d] %s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day13/bcsts.c",
              24, "socket", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  int op = 1;
  setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &op, sizeof(op));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9898);
  inet_pton(AF_INET, "192.168.0.255", &addr.sin_addr);
  sendto(fd, "我是广播", strlen("我是广播"), 0, (struct sockaddr *)&addr,
         sizeof(addr));
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1246 <main+0x96>
jmp    11f9 <main+0x49>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%r9
lea    0xde0(%rip),%rsi
lea    0xde9(%rip),%rdx
mov    $0x18,%ecx
lea    0xe71(%rip),%r8
call   1090 <fprintf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x26aa,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
lea    0xe0d(%rip),%rsi
call   10a0 <inet_pton@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xe09(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    -0x18(%rbp),%r8
lea    0xdf4(%rip),%rsi
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movl   $0x0,0x10(%rsp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     126d <main+0xcd>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2dd8(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x26aa,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r14),%edi
lea    0xe84(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdx
call   1090 <inet_pton@plt>
lea    0xe7e(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d4c(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd75(%rip),%rsi
lea    0xd7e(%rip),%rdx
lea    0xe0b(%rip),%r8
mov    %ebx,%edi
mov    $0x18,%ecx
mov    %rax,%r9
call   1080 <fprintf@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d] %s %s\0A\00"
@.str.1 = [148 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day13/bcsts.c\00"
@.str.2 = [7 x i8] c"socket\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@AF_INET = global i32 0
@.str.3 = [14 x i8] c"192.168.0.255\00"
@.str.4 = [13 x i8] c"\E6\88\91\E6\98\AF\E5\B9\BF\E6\92\AD\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
store i32 0, i32* %1
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %19
10: ; preds = %0
br label %11
11: ; preds = %10
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str.1, i64 0, i64 0), i32 24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; No predecessors!
br label %19
19: ; preds = %18, %0
store i32 1, i32* %4
%20 = load i32, i32* %2
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_BROADCAST
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %4, i32 4)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @htons(i32 9898)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%30 = call i32 @inet_pton(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32* %29)
%31 = load i32, i32* %2
%32 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i32 @sendto(i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 0, %struct.sockaddr* %33, i32 12)
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d] %s %s\0A\00"
@.str.1 = [148 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day13/bcsts.c\00"
@.str.2 = [7 x i8] c"socket\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@AF_INET = global i32 0
@.str.3 = [14 x i8] c"192.168.0.255\00"
@.str.4 = [13 x i8] c"\E6\88\91\E6\98\AF\E5\B9\BF\E6\92\AD\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 0, i32* %4
%5 = load i32, i32* @PF_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 %16
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str.1, i64 0, i64 0), i32 24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_BROADCAST
%20 = call i32 @setsockopt(i32 %7, i32 %18, i32 %19, i32* %2, i32 4)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 9898)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%27 = call i32 @inet_pton(i32 %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32* %26)
%28 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%29 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%30 = call i32 @sendto(i32 %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 0, %struct.sockaddr* %29, i32 12)
%31 = call i32 @close(i32 %7)
ret i32 0
}