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

/* Variables and functions */
int /*<<< orphan*/ SIOCGIFBRDADDR;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int GetBroadcastAddr(int sock, struct sockaddr_in *broadcast_addr) {
  struct ifreq ifr;
  strncpy(ifr.ifr_name, "eth0", strlen("eth0") + 1);
  if (0 > ioctl(sock, SIOCGIFBRDADDR, &ifr)) {
    perror("ioctl");
    return -1;
  }
  memcpy(broadcast_addr, &ifr.ifr_broadaddr, sizeof(struct sockaddr_in));
  return 0;
}


x86 assembly (clang, -O0):
<GetBroadcastAddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe94(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
add    $0x1,%rdx
lea    0xe7e(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11c2 <GetBroadcastAddr+0x72>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <GetBroadcastAddr+0x8b>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetBroadcastAddr>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xeb1(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     118a <GetBroadcastAddr+0x4a>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    1183 <GetBroadcastAddr+0x43>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { i32, i32 }
@.str = [5 x i8] c"eth0\00"
@SIOCGIFBRDADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i32 @GetBroadcastAddr(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.ifreq
store i32 %0, i32* %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%8 = load i32, i32* %7
%9 = call i64 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = add nsw i64 %9, 1
%11 = call i32 @strncpy(i32 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %10)
%12 = load i32, i32* %4
%13 = load i32, i32* @SIOCGIFBRDADDR
%14 = call i64 @ioctl(i32 %12, i32 %13, %struct.ifreq* %6)
%15 = icmp sgt i64 0, %14
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %22
18: ; preds = %2
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%21 = call i32 @memcpy(%struct.sockaddr_in* %19, i32* %20, i32 4)
store i32 0, i32* %3
br label %22
22: ; preds = %18, %16
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { i32, i32 }
@.str = [5 x i8] c"eth0\00"
@SIOCGIFBRDADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i32 @GetBroadcastAddr(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 5)
%6 = load i32, i32* @SIOCGIFBRDADDR
%7 = call i64 @ioctl(i32 %0, i32 %6, %struct.ifreq* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %14
11: ; preds = %2
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%13 = call i32 @memcpy(%struct.sockaddr_in* %1, i32* %12, i32 4)
br label %14
14: ; preds = %11, %9
%15 = phi i32 [ -1, %9 ], [ 0, %11 ]
ret i32 %15
}