#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;
};
struct ifreq {
  int ifc_len;
  char *ifc_buf;
  int /*<<< orphan*/ ifr_broadaddr;
  int /*<<< orphan*/ ifr_name;
  struct ifreq *ifc_req;
};
struct ifconf {
  int ifc_len;
  char *ifc_buf;
  int /*<<< orphan*/ ifr_broadaddr;
  int /*<<< orphan*/ ifr_name;
  struct ifconf *ifc_req;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFBRDADDR;
int /*<<< orphan*/ SIOCGIFCONF;
int /*<<< orphan*/ SOCK_DGRAM;
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
char *getBroadCast(char *host) {
  int sck;
  char buf[1024];
  struct ifconf ifc;
  struct ifreq *ifr;
  int nInterfaces;
  int i;
  if ((sck = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket getBroadCast: ");
    return NULL;
  }
  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if (ioctl(sck, SIOCGIFCONF, &ifc) < 0) {
    perror("ioctl(SIOCGIFCONF) getBroadCast :");
    return NULL;
  }
  ifr = ifc.ifc_req;
  nInterfaces = ifc.ifc_len / sizeof(struct ifreq);
  for (i = 0; i < nInterfaces; i++) {
    struct ifreq *item = &ifr[i];
    if (!strcmp(item->ifr_name, host)) {
      if (ioctl(sck, SIOCGIFBRDADDR, item) >= 0)
        return inet_ntoa(
            ((struct sockaddr_in *)&item->ifr_broadaddr)->sin_addr);
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<getBroadCast>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    119d <getBroadCast+0x4d>
lea    0xe75(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c3 <getBroadCast+0x173>
movl   $0x400,-0x440(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    -0x440(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11f0 <getBroadCast+0xa0>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c3 <getBroadCast+0x173>
mov    -0x428(%rbp),%rax
mov    %rax,-0x448(%rbp)
movslq -0x440(%rbp),%rax
shr    $0x5,%rax
mov    %eax,-0x44c(%rbp)
movl   $0x0,-0x450(%rbp)
mov    -0x450(%rbp),%eax
cmp    -0x44c(%rbp),%eax
jge    12bb <getBroadCast+0x16b>
mov    -0x448(%rbp),%rax
movslq -0x450(%rbp),%rcx
shl    $0x5,%rcx
add    %rcx,%rax
mov    %rax,-0x458(%rbp)
mov    -0x458(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    12a2 <getBroadCast+0x152>
mov    -0x14(%rbp),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
mov    -0x458(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     129d <getBroadCast+0x14d>
mov    -0x458(%rbp),%rax
mov    0x10(%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x8(%rbp)
jmp    12c3 <getBroadCast+0x173>
jmp    12a2 <getBroadCast+0x152>
jmp    12a7 <getBroadCast+0x157>
mov    -0x450(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x450(%rbp)
jmp    1219 <getBroadCast+0xc9>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getBroadCast>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rdi,%r12
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     120a <getBroadCast+0xba>
mov    %eax,%r15d
movl   $0x400,(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1213 <getBroadCast+0xc3>
mov    (%rsp),%ebx
cmp    $0x20,%ebx
jl     121f <getBroadCast+0xcf>
mov    0x18(%rsp),%rbp
shr    $0x5,%rbx
mov    0x2e06(%rip),%r13
xor    %r14d,%r14d
jmp    11da <getBroadCast+0x8a>
nop
add    $0x20,%rbp
add    $0xffffffffffffffff,%rbx
je     1222 <getBroadCast+0xd2>
mov    0x14(%rbp),%edi
mov    %r12,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
jne    11d0 <getBroadCast+0x80>
mov    0x0(%r13),%esi
mov    %r15d,%edi
mov    %rbp,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     11d0 <getBroadCast+0x80>
mov    0x10(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r14
jmp    1222 <getBroadCast+0xd2>
lea    0xdef(%rip),%rdi
jmp    121a <getBroadCast+0xca>
lea    0xdfc(%rip),%rdi
call   1060 <perror@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifconf = type { i32, i8*, i32, i32, %struct.ifconf* }
%struct.ifreq = type { i32, i8*, i32, i32, %struct.ifreq* }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"socket getBroadCast: \00"
@SIOCGIFCONF = global i32 0
@.str.1 = [34 x i8] c"ioctl(SIOCGIFCONF) getBroadCast :\00"
@SIOCGIFBRDADDR = global i32 0
define i8* @getBroadCast(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca %struct.ifconf
%7 = alloca %struct.ifreq*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.ifreq*
store i8* %0, i8** %3
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %71
17: ; preds = %1
%18 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
store i32 1024, i32* %18
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 1
store i8* %19, i8** %20
%21 = load i32, i32* %4
%22 = load i32, i32* @SIOCGIFCONF
%23 = bitcast %struct.ifconf* %6 to %struct.ifreq*
%24 = call i64 @ioctl(i32 %21, i32 %22, %struct.ifreq* %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %71
28: ; preds = %17
%29 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 4
%30 = load %struct.ifconf*, %struct.ifconf** %29
%31 = bitcast %struct.ifconf* %30 to %struct.ifreq*
store %struct.ifreq* %31, %struct.ifreq** %7
%32 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
%33 = load i32, i32* %32
%34 = sext i32 %33 to i64
%35 = udiv i64 %34, 32
%36 = trunc i64 %35 to i32
store i32 %36, i32* %8
store i32 0, i32* %9
br label %37
37: ; preds = %67, %28
%38 = load i32, i32* %9
%39 = load i32, i32* %8
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %70
41: ; preds = %37
%42 = load %struct.ifreq*, %struct.ifreq** %7
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %42, i64 %44
store %struct.ifreq* %45, %struct.ifreq** %10
%46 = load %struct.ifreq*, %struct.ifreq** %10
%47 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %46, i32 0, i32 3
%48 = load i32, i32* %47
%49 = load i8*, i8** %3
%50 = call i32 @strcmp(i32 %48, i8* %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %66, label %52
52: ; preds = %41
%53 = load i32, i32* %4
%54 = load i32, i32* @SIOCGIFBRDADDR
%55 = load %struct.ifreq*, %struct.ifreq** %10
%56 = call i64 @ioctl(i32 %53, i32 %54, %struct.ifreq* %55)
%57 = icmp sge i64 %56, 0
br i1 %57, label %58, label %65
58: ; preds = %52
%59 = load %struct.ifreq*, %struct.ifreq** %10
%60 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %59, i32 0, i32 2
%61 = bitcast i32* %60 to %struct.sockaddr_in*
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i8* @inet_ntoa(i32 %63)
store i8* %64, i8** %2
br label %71
65: ; preds = %52
br label %66
66: ; preds = %65, %41
br label %67
67: ; preds = %66
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
store i32 %69, i32* %9
br label %37
70: ; preds = %37
store i8* null, i8** %2
br label %71
71: ; preds = %70, %58, %26, %15
%72 = load i8*, i8** %2
ret i8* %72
}

LLVM IR (-O2):
%struct.ifconf = type { i32, i8*, i32, i32, %struct.ifconf* }
%struct.ifreq = type { i32, i8*, i32, i32, %struct.ifreq* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"socket getBroadCast: \00"
@SIOCGIFCONF = global i32 0
@.str.1 = [34 x i8] c"ioctl(SIOCGIFCONF) getBroadCast :\00"
@SIOCGIFBRDADDR = global i32 0
define i8* @getBroadCast(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.ifconf
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.ifconf* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %49
12: ; preds = %1
%13 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 0
store i32 1024, i32* %13
%14 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 1
store i8* %4, i8** %14
%15 = load i32, i32* @SIOCGIFCONF
%16 = bitcast %struct.ifconf* %3 to %struct.ifreq*
%17 = call i64 @ioctl(i32 %8, i32 %15, %struct.ifreq* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %49
21: ; preds = %12
%22 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 4
%23 = bitcast %struct.ifconf** %22 to %struct.ifreq**
%24 = load %struct.ifreq*, %struct.ifreq** %23
%25 = load i32, i32* %13
%26 = icmp sgt i32 %25, 31
br i1 %26, label %27, label %49
27: ; preds = %21
%28 = lshr i32 %25, 5
%29 = zext i32 %28 to i64
br label %30
30: ; preds = %27, %46
%31 = phi i64 [ 0, %27 ], [ %47, %46 ]
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %24, i64 %31, i32 3
%33 = load i32, i32* %32
%34 = call i32 @strcmp(i32 %33, i8* %0)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %30
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %24, i64 %31
%38 = load i32, i32* @SIOCGIFBRDADDR
%39 = call i64 @ioctl(i32 %8, i32 %38, %struct.ifreq* %37)
%40 = icmp sgt i64 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = and i64 %31, 4294967295
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %24, i64 %42, i32 2
%44 = load i32, i32* %43
%45 = call i8* @inet_ntoa(i32 %44)
br label %49
46: ; preds = %36, %30
%47 = add nuw nsw i64 %31, 1
%48 = icmp eq i64 %47, %29
br i1 %48, label %49, label %30
49: ; preds = %46, %21, %41, %19, %10
%50 = phi i8* [ null, %10 ], [ null, %19 ], [ %45, %41 ], [ null, %21 ], [ null, %46 ]
ret i8* %50
}