#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_storage {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int ai_flags;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ src_addr;
typedef int socklen_t;
typedef int /*<<< orphan*/ hints;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int AI_ADDRCONFIG;
int AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_UDP;
int NI_MAXHOST;
int NI_MAXSERV;
int /*<<< orphan*/ NI_NUMERICSERV;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int getnameinfo(struct sockaddr *, int, char *, int, char *, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
scalar_t__ isValidMagicPacket(char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printMac(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  const char *hostname = NULL;
  const char *portname = "1337";
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_protocol = IPPROTO_UDP;
  hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
  struct addrinfo *res = NULL;
  int err = getaddrinfo(hostname, portname, &hints, &res);
  if (err != 0) {
    printf("Failed to find local socket addresses. Error code: %d", err);
  }
  int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (fd == -1) {
    perror("Failed to create a socket");
  }
  const int enable_broadcast = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &enable_broadcast,
                 sizeof enable_broadcast) == -1) {
    perror("Failed to allow broadcast on socket");
    exit(1);
  }
  if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
    perror("Failed to bind a local socket");
  }
  freeaddrinfo(res);
  char buffer[549];
  struct sockaddr_storage src_addr;
  socklen_t src_addr_len = sizeof(src_addr);
  int s;
  while (1) {
    ssize_t count = recvfrom(fd, buffer, sizeof(buffer), 0,
                             (struct sockaddr *)&src_addr, &src_addr_len);
    if (count == -1) {
      perror("Error reading from socket");
    } else if (count != 102) {
      printf("The packet received is not 102 byte long and is %ld bytes long "
             "instead",
             (long)count);
    } else {
      char host[NI_MAXHOST], service[NI_MAXSERV];
      s = getnameinfo((struct sockaddr *)&src_addr, src_addr_len, host,
                      NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV);
      printf("Received %ld bytes from %s:%s\n", (long)count, host, service);
      for (int i = 0; i < count; i++) {
        printf("%X", (int)(*(unsigned char *)(&buffer[i])));
      }
      printf("\n");
    }
    if (isValidMagicPacket(buffer)) {
      printf("Magic packet verified\n");
      printf("The mac address is following:\n");
      printMac(buffer);
      printf("Resending the packet to network broadcast address\n");
      inet_aton("255.255.255.255",
                &((struct sockaddr_in *)&src_addr)->sin_addr);
      int bytessent = sendto(fd, buffer, count, 0, (struct sockaddr *)&src_addr,
                             src_addr_len);
      if (bytessent > 0) {
        printf("Sent %d bytes to network broadcast address\n", bytessent);
      } else if (bytessent == -1) {
        perror("Failed to resend WOL packet to network broadcast\n");
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2d0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xde8(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    0x2d44(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10e0 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1294 <main+0xa4>
mov    -0x44(%rbp),%esi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    12c7 <main+0xd7>
lea    0xd79(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4c(%rbp)
mov    -0x48(%rbp),%edi
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
mov    0x2cdf(%rip),%rax
mov    (%rax),%edx
lea    -0x4c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1311 <main+0x121>
lea    0xd53(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x48(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    133c <main+0x14c>
lea    0xd42(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x40(%rbp),%rdi
call   10f0 <freeaddrinfo@plt>
movl   $0x4,-0x28c(%rbp)
mov    -0x48(%rbp),%edi
lea    -0x280(%rbp),%rsi
lea    -0x288(%rbp),%r8
mov    $0x225,%edx
xor    %ecx,%ecx
lea    -0x28c(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x294(%rbp)
cmpl   $0xffffffff,-0x294(%rbp)
jne    1397 <main+0x1a7>
lea    0xd0a(%rip),%rdi
call   10c0 <perror@plt>
jmp    14ea <main+0x2fa>
cmpl   $0x66,-0x294(%rbp)
je     13be <main+0x1ce>
movslq -0x294(%rbp),%rsi
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    14e5 <main+0x2f5>
mov    0x2beb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x2a0(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x2c8(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x2a8(%rbp)
mov    0x2bce(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%r8
sub    %rcx,%r8
mov    %r8,-0x2c0(%rbp)
mov    %r8,%rsp
mov    %rax,-0x2b0(%rbp)
lea    -0x288(%rbp),%rdi
mov    -0x28c(%rbp),%esi
mov    0x2b7e(%rip),%rax
mov    (%rax),%ecx
mov    0x2b8d(%rip),%rax
mov    (%rax),%r9d
mov    0x2ba3(%rip),%rax
mov    (%rax),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1070 <getnameinfo@plt>
mov    -0x2c8(%rbp),%rdx
mov    -0x2c0(%rbp),%rcx
add    $0x10,%rsp
mov    %eax,-0x290(%rbp)
movslq -0x294(%rbp),%rsi
lea    0xc7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2b4(%rbp)
mov    -0x2b4(%rbp),%eax
cmp    -0x294(%rbp),%eax
jge    14cd <main+0x2dd>
movslq -0x2b4(%rbp),%rax
movzbl -0x280(%rbp,%rax,1),%esi
ff
lea    0xc65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2b4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2b4(%rbp)
jmp    148a <main+0x29a>
lea    0xc7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2a0(%rbp),%rax
mov    %rax,%rsp
jmp    14ea <main+0x2fa>
lea    -0x280(%rbp),%rdi
call   1110 <isValidMagicPacket@plt>
cmp    $0x0,%rax
je     15bd <main+0x3cd>
lea    0xc13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xc1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x280(%rbp),%rdi
call   10a0 <printMac@plt>
lea    0xc21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x288(%rbp),%rsi
lea    0xc3f(%rip),%rdi
call   1080 <inet_aton@plt>
mov    -0x48(%rbp),%edi
lea    -0x280(%rbp),%rsi
mov    -0x294(%rbp),%edx
lea    -0x288(%rbp),%r8
mov    -0x28c(%rbp),%r9d
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,-0x2b8(%rbp)
cmpl   $0x0,-0x2b8(%rbp)
jle    159a <main+0x3aa>
mov    -0x2b8(%rbp),%esi
lea    0xc05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    15b8 <main+0x3c8>
cmpl   $0xffffffff,-0x2b8(%rbp)
jne    15b3 <main+0x3c3>
lea    0xc11(%rip),%rdi
call   10c0 <perror@plt>
jmp    15b8 <main+0x3c8>
jmp    15bd <main+0x3cd>
jmp    134f <main+0x15f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x268,%rsp
lea    -0x68(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2d82(%rip),%rax
mov    0x2d3b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,-0x68(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xd8d(%rip),%rsi
lea    -0x38(%rbp),%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   1100 <getaddrinfo@plt>
test   %eax,%eax
je     1295 <main+0x85>
lea    0xd79(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%edi
mov    0xc(%rax),%edx
mov    0x10(%rax),%esi
call   1120 <socket@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     14d6 <main+0x2c6>
movl   $0x1,-0x48(%rbp)
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
mov    0x2cf6(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     14f8 <main+0x2e8>
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edx
mov    0x8(%rax),%esi
mov    %r14d,%edi
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
je     14e7 <main+0x2d7>
mov    -0x38(%rbp),%rdi
call   1110 <freeaddrinfo@plt>
movl   $0x4,-0x2c(%rbp)
lea    -0x290(%rbp),%rbx
lea    -0x40(%rbp),%r15
lea    0xdf3(%rip),%r13
mov    %r14d,-0x44(%rbp)
jmp    1340 <main+0x130>
nopw   0x0(%rax,%rax,1)
lea    0xdf3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x225,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    -0x2c(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,%r12d
cmp    $0x66,%eax
je     1380 <main+0x170>
cmp    $0xffffffff,%r12d
jne    1440 <main+0x230>
lea    0xd25(%rip),%rdi
call   10e0 <perror@plt>
jmp    1451 <main+0x241>
nopl   0x0(%rax)
mov    %rsp,-0x50(%rbp)
mov    0x2c25(%rip),%rcx
mov    (%rcx),%eax
mov    %r15,%rdi
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2c20(%rip),%rax
mov    (%rax),%r9d
mov    %rsp,%r14
mov    %r9,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    -0x2c(%rbp),%esi
mov    (%rcx),%ecx
mov    0x2c1d(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    %r15,%rdx
mov    %r14,%r8
push   %rax
call   1090 <getnameinfo@plt>
add    $0x10,%rsp
mov    $0x66,%esi
lea    0xd0b(%rip),%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %ebx,%ebx
nopl   0x0(%rax)
movzbl -0x290(%rbp,%rbx,1),%esi
ff
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    $0x66,%rbx
jne    1400 <main+0x1f0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x50(%rbp),%rsp
mov    -0x44(%rbp),%r14d
lea    -0x290(%rbp),%rbx
lea    -0x40(%rbp),%r15
jmp    1451 <main+0x241>
nopl   0x0(%rax,%rax,1)
movslq %r12d,%rsi
lea    0xc67(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1130 <isValidMagicPacket@plt>
test   %rax,%rax
je     1340 <main+0x130>
lea    0xd1f(%rip),%rdi
call   1040 <puts@plt>
lea    0xd29(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   10c0 <printMac@plt>
lea    0xd33(%rip),%rdi
call   1040 <puts@plt>
lea    0xc85(%rip),%rdi
mov    %r15,%rsi
call   10a0 <inet_aton@plt>
mov    -0x2c(%rbp),%r9d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1080 <sendto@plt>
test   %eax,%eax
jg     1330 <main+0x120>
cmp    $0xffffffff,%eax
jne    1340 <main+0x130>
lea    0xc8a(%rip),%rdi
call   10e0 <perror@plt>
jmp    1340 <main+0x130>
lea    0xb5e(%rip),%rdi
call   10e0 <perror@plt>
jmp    12b3 <main+0xa3>
lea    0xb8b(%rip),%rdi
call   10e0 <perror@plt>
jmp    1302 <main+0xf2>
lea    0xb56(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
@.str = [5 x i8] c"1337\00"
@AF_INET = global i32 0
@IPPROTO_UDP = global i32 0
@AI_PASSIVE = global i32 0
@AI_ADDRCONFIG = global i32 0
@.str.1 = [54 x i8] c"Failed to find local socket addresses. Error code: %d\00"
@.str.2 = [26 x i8] c"Failed to create a socket\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.3 = [36 x i8] c"Failed to allow broadcast on socket\00"
@.str.4 = [30 x i8] c"Failed to bind a local socket\00"
@.str.5 = [26 x i8] c"Error reading from socket\00"
@.str.6 = [71 x i8] c"The packet received is not 102 byte long and is %ld bytes long instead\00"
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.7 = [31 x i8] c"Received %ld bytes from %s:%s\0A\00"
@.str.8 = [3 x i8] c"%X\00"
@.str.9 = [2 x i8] c"\0A\00"
@.str.10 = [23 x i8] c"Magic packet verified\0A\00"
@.str.11 = [31 x i8] c"The mac address is following:\0A\00"
@.str.12 = [51 x i8] c"Resending the packet to network broadcast address\0A\00"
@.str.13 = [16 x i8] c"255.255.255.255\00"
@.str.14 = [44 x i8] c"Sent %d bytes to network broadcast address\0A\00"
@.str.15 = [50 x i8] c"Failed to resend WOL packet to network broadcast\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [549 x i8]
%14 = alloca %struct.sockaddr_storage
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i64
%20 = alloca i64
%21 = alloca i32
%22 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %7
%23 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %24, i32* %25
%26 = load i32, i32* @IPPROTO_UDP
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %26, i32* %27
%28 = load i32, i32* @AI_PASSIVE
%29 = load i32, i32* @AI_ADDRCONFIG
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %30, i32* %31
store %struct.addrinfo* null, %struct.addrinfo** %9
%32 = load i8*, i8** %6
%33 = load i8*, i8** %7
%34 = call i32 @getaddrinfo(i8* %32, i8* %33, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %2
%38 = load i32, i32* %10
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %37, %2
%41 = load %struct.addrinfo*, %struct.addrinfo** %9
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 5
%43 = load i32, i32* %42
%44 = load %struct.addrinfo*, %struct.addrinfo** %9
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 4
%46 = load i32, i32* %45
%47 = load %struct.addrinfo*, %struct.addrinfo** %9
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 3
%49 = load i32, i32* %48
%50 = call i32 @socket(i32 %43, i32 %46, i32 %49)
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %40
%54 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %55
55: ; preds = %53, %40
store i32 1, i32* %12
%56 = load i32, i32* %11
%57 = load i32, i32* @SOL_SOCKET
%58 = load i32, i32* @SO_BROADCAST
%59 = call i32 @setsockopt(i32 %56, i32 %57, i32 %58, i32* %12, i32 4)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %55
%65 = load i32, i32* %11
%66 = load %struct.addrinfo*, %struct.addrinfo** %9
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 2
%68 = load i32, i32* %67
%69 = load %struct.addrinfo*, %struct.addrinfo** %9
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 1
%71 = load i32, i32* %70
%72 = call i32 @bind(i32 %65, i32 %68, i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %64
%75 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %76
76: ; preds = %74, %64
%77 = load %struct.addrinfo*, %struct.addrinfo** %9
%78 = call i32 @freeaddrinfo(%struct.addrinfo* %77)
store i32 4, i32* %15
br label %79
79: ; preds = %76, %161
%80 = load i32, i32* %11
%81 = getelementptr inbounds [549 x i8], [549 x i8]* %13, i64 0, i64 0
%82 = bitcast %struct.sockaddr_storage* %14 to %struct.sockaddr*
%83 = call i32 @recvfrom(i32 %80, i8* %81, i32 549, i32 0, %struct.sockaddr* %82, i32* %15)
store i32 %83, i32* %17
%84 = load i32, i32* %17
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %88
86: ; preds = %79
%87 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %130
88: ; preds = %79
%89 = load i32, i32* %17
%90 = icmp ne i32 %89, 102
br i1 %90, label %91, label %95
91: ; preds = %88
%92 = load i32, i32* %17
%93 = sext i32 %92 to i64
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.6, i64 0, i64 0), i64 %93)
br label %129
95: ; preds = %88
%96 = load i32, i32* @NI_MAXHOST
%97 = zext i32 %96 to i64
%98 = call i8* @llvm.stacksave()
store i8* %98, i8** %18
%99 = alloca i8, i64 %97
store i64 %97, i64* %19
%100 = load i32, i32* @NI_MAXSERV
%101 = zext i32 %100 to i64
%102 = alloca i8, i64 %101
store i64 %101, i64* %20
%103 = bitcast %struct.sockaddr_storage* %14 to %struct.sockaddr*
%104 = load i32, i32* %15
%105 = load i32, i32* @NI_MAXHOST
%106 = load i32, i32* @NI_MAXSERV
%107 = load i32, i32* @NI_NUMERICSERV
%108 = call i32 @getnameinfo(%struct.sockaddr* %103, i32 %104, i8* %99, i32 %105, i8* %102, i32 %106, i32 %107)
store i32 %108, i32* %16
%109 = load i32, i32* %17
%110 = sext i32 %109 to i64
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i64 %110, i8* %99, i8* %102)
store i32 0, i32* %21
br label %112
112: ; preds = %123, %95
%113 = load i32, i32* %21
%114 = load i32, i32* %17
%115 = icmp slt i32 %113, %114
br i1 %115, label %116, label %126
116: ; preds = %112
%117 = load i32, i32* %21
%118 = sext i32 %117 to i64
%119 = getelementptr inbounds [549 x i8], [549 x i8]* %13, i64 0, i64 %118
%120 = load i8, i8* %119
%121 = zext i8 %120 to i32
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %121)
br label %123
123: ; preds = %116
%124 = load i32, i32* %21
%125 = add nsw i32 %124, 1
store i32 %125, i32* %21
br label %112
126: ; preds = %112
%127 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%128 = load i8*, i8** %18
br label %129
129: ; preds = %126, %91
br label %130
130: ; preds = %129, %86
%131 = getelementptr inbounds [549 x i8], [549 x i8]* %13, i64 0, i64 0
%132 = call i64 @isValidMagicPacket(i8* %131)
%133 = icmp ne i64 %132, 0
br i1 %133, label %134, label %161
134: ; preds = %130
%135 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
%136 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.11, i64 0, i64 0))
%137 = getelementptr inbounds [549 x i8], [549 x i8]* %13, i64 0, i64 0
%138 = call i32 @printMac(i8* %137)
%139 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.12, i64 0, i64 0))
%140 = bitcast %struct.sockaddr_storage* %14 to %struct.sockaddr_in*
%141 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %140, i32 0, i32 0
%142 = call i32 @inet_aton(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.13, i64 0, i64 0), i32* %141)
%143 = load i32, i32* %11
%144 = getelementptr inbounds [549 x i8], [549 x i8]* %13, i64 0, i64 0
%145 = load i32, i32* %17
%146 = bitcast %struct.sockaddr_storage* %14 to %struct.sockaddr*
%147 = load i32, i32* %15
%148 = call i32 @sendto(i32 %143, i8* %144, i32 %145, i32 0, %struct.sockaddr* %146, i32 %147)
store i32 %148, i32* %22
%149 = load i32, i32* %22
%150 = icmp sgt i32 %149, 0
br i1 %150, label %151, label %154
151: ; preds = %134
%152 = load i32, i32* %22
%153 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.14, i64 0, i64 0), i32 %152)
br label %160
154: ; preds = %134
%155 = load i32, i32* %22
%156 = icmp eq i32 %155, -1
br i1 %156, label %157, label %159
157: ; preds = %154
%158 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.15, i64 0, i64 0))
br label %159
159: ; preds = %157, %154
br label %160
160: ; preds = %159, %151
br label %161
161: ; preds = %160, %130
br label %79
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"1337\00"
@AF_INET = global i32 0
@IPPROTO_UDP = global i32 0
@AI_PASSIVE = global i32 0
@AI_ADDRCONFIG = global i32 0
@.str.1 = [54 x i8] c"Failed to find local socket addresses. Error code: %d\00"
@.str.2 = [26 x i8] c"Failed to create a socket\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.3 = [36 x i8] c"Failed to allow broadcast on socket\00"
@.str.4 = [30 x i8] c"Failed to bind a local socket\00"
@.str.5 = [26 x i8] c"Error reading from socket\00"
@.str.6 = [71 x i8] c"The packet received is not 102 byte long and is %ld bytes long instead\00"
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.7 = [31 x i8] c"Received %ld bytes from %s:%s\0A\00"
@.str.8 = [3 x i8] c"%X\00"
@.str.13 = [16 x i8] c"255.255.255.255\00"
@.str.14 = [44 x i8] c"Sent %d bytes to network broadcast address\0A\00"
@.str.15 = [50 x i8] c"Failed to resend WOL packet to network broadcast\0A\00"
@str = [22 x i8] c"Magic packet verified\00"
@str.16 = [30 x i8] c"The mac address is following:\00"
@str.17 = [50 x i8] c"Resending the packet to network broadcast address\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = alloca [549 x i8]
%7 = alloca %struct.sockaddr_storage
%8 = alloca i32
%9 = bitcast %struct.addrinfo* %3 to i8*
%10 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %11, i32* %12
%13 = load i32, i32* @IPPROTO_UDP
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = load i32, i32* @AI_ADDRCONFIG
%17 = or i32 %16, %15
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.addrinfo** %4 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %4
%20 = call i32 @getaddrinfo(i8* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %3, %struct.addrinfo** %4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %24
24: ; preds = %22, %2
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %24
%37 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_BROADCAST
%40 = call i32 @setsockopt(i32 %32, i32 %38, i32 %39, i32* %5, i32 4)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
%46 = load %struct.addrinfo*, %struct.addrinfo** %4
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i64 0, i32 2
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i64 0, i32 1
%50 = load i32, i32* %49
%51 = call i32 @bind(i32 %32, i32 %48, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %53, %45
%56 = load %struct.addrinfo*, %struct.addrinfo** %4
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
%58 = getelementptr inbounds [549 x i8], [549 x i8]* %6, i64 0, i64 0
%59 = bitcast %struct.sockaddr_storage* %7 to i8*
%60 = bitcast i32* %8 to i8*
store i32 4, i32* %8
%61 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%62 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %7, i64 0, i32 0
br label %63
63: ; preds = %107, %55
%64 = call i32 @recvfrom(i32 %32, i8* %58, i32 549, i32 0, %struct.sockaddr* %61, i32* %8)
switch i32 %64, label %67 [
i32 -1, label %65
i32 102, label %70
]
65: ; preds = %63
%66 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %93
67: ; preds = %63
%68 = sext i32 %64 to i64
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([71 x i8], [71 x i8]* @.str.6, i64 0, i64 0), i64 %68)
br label %93
70: ; preds = %63
%71 = load i32, i32* @NI_MAXHOST
%72 = zext i32 %71 to i64
%73 = call i8* @llvm.stacksave()
%74 = alloca i8, i64 %72
%75 = load i32, i32* @NI_MAXSERV
%76 = zext i32 %75 to i64
%77 = alloca i8, i64 %76
%78 = load i32, i32* %8
%79 = load i32, i32* @NI_MAXHOST
%80 = load i32, i32* @NI_NUMERICSERV
%81 = call i32 @getnameinfo(%struct.sockaddr* %61, i32 %78, i8* %74, i32 %79, i8* %77, i32 %75, i32 %80)
%82 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i64 102, i8* %74, i8* %77)
br label %85
83: ; preds = %85
%84 = call i32 @putchar(i32 10)
br label %93
85: ; preds = %70, %85
%86 = phi i64 [ 0, %70 ], [ %91, %85 ]
%87 = getelementptr inbounds [549 x i8], [549 x i8]* %6, i64 0, i64 %86
%88 = load i8, i8* %87
%89 = zext i8 %88 to i32
%90 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %89)
%91 = add nuw nsw i64 %86, 1
%92 = icmp eq i64 %91, 102
br i1 %92, label %83, label %85
93: ; preds = %67, %83, %65
%94 = call i64 @isValidMagicPacket(i8* %58)
%95 = icmp eq i64 %94, 0
br i1 %95, label %107, label %96
96: ; preds = %93
%97 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%98 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.16, i64 0, i64 0))
%99 = call i32 @printMac(i8* %58)
%100 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.17, i64 0, i64 0))
%101 = call i32 @inet_aton(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.13, i64 0, i64 0), i32* %62)
%102 = load i32, i32* %8
%103 = call i32 @sendto(i32 %32, i8* %58, i32 %64, i32 0, %struct.sockaddr* %61, i32 %102)
%104 = icmp sgt i32 %103, 0
br i1 %104, label %105, label %108
105: ; preds = %96
%106 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.14, i64 0, i64 0), i32 %103)
br label %107
107: ; preds = %105, %110, %108, %93
br label %63
108: ; preds = %96
%109 = icmp eq i32 %103, -1
br i1 %109, label %110, label %107
110: ; preds = %108
%111 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.15, i64 0, i64 0))
br label %107
}