#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 /*<<< orphan*/ ifr_broadaddr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFBRDADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int get_bcast_addr(char *ifname, char *bcast_addr) {
  struct ifreq ifr;
  int fd;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0) {
    printf("Unable to open the socket\n");
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  memset(bcast_addr, 0, 16);
  if (ioctl(fd, SIOCGIFBRDADDR, &ifr) >= 0)
    sprintf(bcast_addr, "%s",
            inet_ntoa(((struct sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<get_bcast_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ce <get_bcast_addr+0x4e>
lea    0xe43(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <get_bcast_addr+0xc0>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    -0x24(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jl     1231 <get_bcast_addr+0xb1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
lea    0xdef(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_bcast_addr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1202 <get_bcast_addr+0x82>
mov    %eax,%ebp
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1080 <ioctl@plt>
test   %rax,%rax
js     11f9 <get_bcast_addr+0x79>
mov    0x8(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    1213 <get_bcast_addr+0x93>
lea    0xdf7(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [27 x i8] c"Unable to open the socket\0A\00"
@SIOCGIFBRDADDR = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @get_bcast_addr(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%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* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %37
15: ; preds = %2
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %4
%19 = call i32 @strncpy(i32 %17, i8* %18, i32 4)
%20 = load i8*, i8** %5
%21 = call i32 @memset(i8* %20, i32 0, i32 16)
%22 = load i32, i32* %7
%23 = load i32, i32* @SIOCGIFBRDADDR
%24 = call i64 @ioctl(i32 %22, i32 %23, %struct.ifreq* %6)
%25 = icmp sge i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %15
%27 = load i8*, i8** %5
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%29 = bitcast i32* %28 to %struct.sockaddr_in*
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i8* @inet_ntoa(i32 %31)
%33 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %26, %15
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
store i32 0, i32* %3
br label %37
37: ; preds = %34, %13
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFBRDADDR = global i32 0
@str = [26 x i8] c"Unable to open the socket\00"
define i32 @get_bcast_addr(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%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 slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %24
11: ; preds = %2
%12 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%13 = tail call i32 @memset(i8* %1, i32 0, i32 16)
%14 = load i32, i32* @SIOCGIFBRDADDR
%15 = call i64 @ioctl(i32 %7, i32 %14, %struct.ifreq* %3)
%16 = icmp sgt i64 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i8* @inet_ntoa(i32 %19)
%21 = call i8* @strcpy(i8* dereferenceable(1) %1, i8* dereferenceable(1) %20)
br label %22
22: ; preds = %17, %11
%23 = call i32 @close(i32 %7)
br label %24
24: ; preds = %22, %9
%25 = phi i32 [ -1, %9 ], [ 0, %22 ]
ret i32 %25
}