#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;
  void *sin_port;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ cliaddr;
typedef int /*<<< orphan*/ broadcast;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
void *htons(int);
scalar_t__ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ sendto(int, char *, scalar_t__, int /*<<< orphan*/,
                  struct sockaddr *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int main(int argc, char **argv) {
  int sockfd;
  int n, len;
  char sendline[1000];
  struct sockaddr_in servaddr, cliaddr;
  if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
    puts("Socket error");
    exit(1);
  }
  int broadcast = 1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast,
                 sizeof(broadcast)) == -1) {
    puts("error sockopt");
    exit(1);
  }
  bzero(&cliaddr, sizeof(cliaddr));
  cliaddr.sin_family = AF_INET;
  cliaddr.sin_port = htons(0);
  cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr)) < 0) {
    perror(NULL);
    close(sockfd);
    exit(1);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(5001);
  if (inet_aton("192.168.1.255", &servaddr.sin_addr) == 0) {
    printf("Invalid IP address\n");
    close(sockfd);
    exit(1);
  }
  strcpy(sendline, "Hello world");
  if (sendto(sockfd, sendline, strlen(sendline) + 1, 0,
             (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror(NULL);
    close(sockfd);
    exit(1);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1244 <main+0x54>
lea    0xdcb(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
movl   $0x1,-0x444(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
lea    -0x444(%rbp),%rcx
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1294 <main+0xa4>
lea    0xd88(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x440(%rbp),%rdi
mov    $0x18,%esi
call   10f0 <bzero@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x430(%rbp)
xor    %edi,%edi
call   1070 <htons@plt>
mov    %rax,-0x438(%rbp)
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,-0x440(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x18,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
jge    1311 <main+0x121>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <perror@plt>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x428(%rbp),%rdi
mov    $0x18,%esi
call   10f0 <bzero@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x418(%rbp)
mov    $0x1389,%edi
call   1070 <htons@plt>
mov    %rax,-0x420(%rbp)
lea    -0x428(%rbp),%rsi
lea    0xcc9(%rip),%rdi
call   10c0 <inet_aton@plt>
cmp    $0x0,%rax
jne    137f <main+0x18f>
lea    0xcc1(%rip),%rdi
call   1080 <printf@plt>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x410(%rbp),%rdi
lea    0xcb0(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x454(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x450(%rbp)
lea    -0x410(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
lea    -0x428(%rbp),%r8
xor    %ecx,%ecx
mov    $0x18,%r9d
call   10a0 <sendto@plt>
cmp    $0x0,%rax
jge    1402 <main+0x212>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <perror@plt>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
xor    %eax,%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x430,%rsp
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1331 <main+0x151>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     133a <main+0x15a>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x18,%esi
call   10e0 <bzero@plt>
mov    0x2d84(%rip),%rbx
mov    (%rbx),%rax
mov    %rax,0x20(%rsp)
xor    %edi,%edi
call   1070 <htons@plt>
mov    %rax,0x18(%rsp)
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     1328 <main+0x148>
lea    0x28(%rsp),%r14
mov    %r14,%rdi
mov    $0x18,%esi
call   10e0 <bzero@plt>
mov    (%rbx),%rax
mov    %rax,0x38(%rsp)
mov    $0x1389,%edi
call   1070 <htons@plt>
mov    %rax,0x30(%rsp)
lea    0xd51(%rip),%rdi
mov    %r14,%rsi
call   10b0 <inet_aton@plt>
test   %rax,%rax
je     1350 <main+0x170>
lea    0xd4b(%rip),%rsi
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
lea    0x28(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1090 <sendto@plt>
test   %rax,%rax
js     1328 <main+0x148>
mov    %ebp,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   10d0 <perror@plt>
jmp    135c <main+0x17c>
lea    0xcc8(%rip),%rdi
jmp    1341 <main+0x161>
lea    0xccc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xcde(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"Socket error\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.1 = [14 x i8] c"error sockopt\00"
@AF_INET = global i8* null
@INADDR_ANY = global i32 0
@.str.2 = [14 x i8] c"192.168.1.255\00"
@.str.3 = [20 x i8] c"Invalid IP address\0A\00"
@.str.4 = [12 x i8] c"Hello world\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 i32
%9 = alloca [1000 x i8]
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
store i32 1, i32* %12
%21 = load i32, i32* %6
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_BROADCAST
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i32* %12, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = call i32 @bzero(%struct.sockaddr_in* %11, i32 24)
%31 = load i8*, i8** @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i8* %31, i8** %32
%33 = call i8* @htons(i32 0)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i8* %33, i8** %34
%35 = load i32, i32* @INADDR_ANY
%36 = call i32 @htonl(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
store i32 %36, i32* %38
%39 = load i32, i32* %6
%40 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%41 = call i64 @bind(i32 %39, %struct.sockaddr* %40, i32 24)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %29
%44 = call i32 @perror(i32* null)
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %29
%49 = call i32 @bzero(%struct.sockaddr_in* %10, i32 24)
%50 = load i8*, i8** @AF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i8* %50, i8** %51
%52 = call i8* @htons(i32 5001)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i8* %52, i8** %53
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%55 = call i64 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %struct.TYPE_2__* %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %48
%58 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %48
%63 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%64 = call i32 @strcpy(i8* %63, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%65 = load i32, i32* %6
%66 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%67 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%68 = call i64 @strlen(i8* %67)
%69 = add nsw i64 %68, 1
%70 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%71 = call i64 @sendto(i32 %65, i8* %66, i64 %69, i32 0, %struct.sockaddr* %70, i32 24)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %78
73: ; preds = %62
%74 = call i32 @perror(i32* null)
%75 = load i32, i32* %6
%76 = call i32 @close(i32 %75)
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %62
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"Socket error\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.1 = [14 x i8] c"error sockopt\00"
@AF_INET = global i8* null
@INADDR_ANY = global i32 0
@.str.2 = [14 x i8] c"192.168.1.255\00"
@.str.4 = [12 x i8] c"Hello world\00"
@str = [19 x i8] c"Invalid IP address\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1000 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_BROADCAST
%21 = call i32 @setsockopt(i32 %12, i32 %19, i32 %20, i32* %6, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = call i32 @bzero(%struct.sockaddr_in* %5, i32 24)
%28 = load i8*, i8** @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i8* %28, i8** %29
%30 = call i8* @htons(i32 0)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i8* %30, i8** %31
%32 = load i32, i32* @INADDR_ANY
%33 = call i32 @htonl(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i64 @bind(i32 %12, %struct.sockaddr* %35, i32 24)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %26
%39 = call i32 @perror(i32* null)
%40 = call i32 @close(i32 %12)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %26
%43 = call i32 @bzero(%struct.sockaddr_in* %4, i32 24)
%44 = load i8*, i8** @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i8* %44, i8** %45
%46 = call i8* @htons(i32 5001)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i8* %46, i8** %47
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%49 = call i64 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %struct.TYPE_2__* %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %42
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%53 = call i32 @close(i32 %12)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %42
%56 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%57 = call i64 @strlen(i8* %7)
%58 = add nsw i64 %57, 1
%59 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%60 = call i64 @sendto(i32 %12, i8* %7, i64 %58, i32 0, %struct.sockaddr* %59, i32 24)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %55
%63 = call i32 @perror(i32* null)
%64 = call i32 @close(i32 %12)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %55
%67 = call i32 @close(i32 %12)
ret i32 0
}