#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_ll {
  int sll_hatype;
  int sll_halen;
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  int /*<<< orphan*/ ifr_name;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int sll_hatype;
  int sll_halen;
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  int /*<<< orphan*/ ifr_name;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ me;
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_LOOP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct sockaddr_ll *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int get_address(const char *dev, int *htype) {
  struct ifreq ifr;
  struct sockaddr_ll me;
  socklen_t alen;
  int s;
  s = socket(PF_PACKET, SOCK_DGRAM, 0);
  if (s < 0) {
    perror("socket(PF_PACKET)");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
    perror("SIOCGIFINDEX");
    close(s);
    return -1;
  }
  memset(&me, 0, sizeof(me));
  me.sll_family = AF_PACKET;
  me.sll_ifindex = ifr.ifr_ifindex;
  me.sll_protocol = htons(ETH_P_LOOP);
  if (bind(s, (struct sockaddr *)&me, sizeof(me)) == -1) {
    perror("bind");
    close(s);
    return -1;
  }
  alen = sizeof(me);
  if (getsockname(s, (struct sockaddr *)&me, &alen) == -1) {
    perror("getsockname");
    close(s);
    return -1;
  }
  close(s);
  *htype = me.sll_hatype;
  return me.sll_halen;
}


x86 assembly (clang, -O0):
<get_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
jge    11de <get_address+0x4e>
lea    0xe33(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <get_address+0x176>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x60(%rbp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1242 <get_address+0xb2>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
mov    -0x60(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <get_address+0x176>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x50(%rbp)
mov    -0x60(%rbp),%edi
lea    -0x58(%rbp),%rsi
mov    $0x1c,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12af <get_address+0x11f>
lea    0xd89(%rip),%rdi
call   1090 <perror@plt>
mov    -0x60(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <get_address+0x176>
movl   $0x1c,-0x5c(%rbp)
mov    -0x60(%rbp),%edi
lea    -0x58(%rbp),%rsi
lea    -0x5c(%rbp),%rdx
call   10a0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    12ef <get_address+0x15f>
lea    0xd4e(%rip),%rdi
call   1090 <perror@plt>
mov    -0x60(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <get_address+0x176>
mov    -0x60(%rbp),%edi
call   1070 <close@plt>
mov    -0x58(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_address>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1294 <get_address+0x104>
mov    %eax,%ebx
lea    0x28(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    0x40(%rsp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     12a7 <get_address+0x117>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x34(%rsp),%eax
mov    %eax,0x18(%rsp)
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1c,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     12b0 <get_address+0x120>
movl   $0x1c,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <getsockname@plt>
cmp    $0xffffffff,%eax
je     12b9 <get_address+0x129>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    0xc(%rsp),%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd65(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1288 <get_address+0xf8>
lea    0xd64(%rip),%rdi
jmp    12c0 <get_address+0x130>
lea    0xd68(%rip),%rdi
jmp    12c0 <get_address+0x130>
lea    0xd64(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
jmp    1288 <get_address+0xf8>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.sockaddr_ll = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"socket(PF_PACKET)\00"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [13 x i8] c"SIOCGIFINDEX\00"
@AF_PACKET = global i32 0
@ETH_P_LOOP = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [12 x i8] c"getsockname\00"
define i32 @get_address(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.ifreq
%7 = alloca %struct.sockaddr_ll
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i32, i32* @PF_PACKET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %69
17: ; preds = %2
%18 = bitcast %struct.ifreq* %6 to %struct.sockaddr_ll*
%19 = call i32 @memset(%struct.sockaddr_ll* %18, i32 0, i32 28)
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 6
%21 = load i32, i32* %20
%22 = load i8*, i8** %4
%23 = load i32, i32* @IFNAMSIZ
%24 = call i32 @strncpy(i32 %21, i8* %22, i32 %23)
%25 = load i32, i32* %9
%26 = load i32, i32* @SIOCGIFINDEX
%27 = bitcast %struct.ifreq* %6 to %struct.sockaddr_ll*
%28 = call i64 @ioctl(i32 %25, i32 %26, %struct.sockaddr_ll* %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %9
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %3
br label %69
34: ; preds = %17
%35 = call i32 @memset(%struct.sockaddr_ll* %7, i32 0, i32 28)
%36 = load i32, i32* @AF_PACKET
%37 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 5
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 3
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 4
store i32 %39, i32* %40
%41 = load i32, i32* @ETH_P_LOOP
%42 = call i32 @htons(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 2
store i32 %42, i32* %43
%44 = load i32, i32* %9
%45 = bitcast %struct.sockaddr_ll* %7 to %struct.sockaddr*
%46 = call i32 @bind(i32 %44, %struct.sockaddr* %45, i32 28)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %52
48: ; preds = %34
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %3
br label %69
52: ; preds = %34
store i32 28, i32* %8
%53 = load i32, i32* %9
%54 = bitcast %struct.sockaddr_ll* %7 to %struct.sockaddr*
%55 = call i32 @getsockname(i32 %53, %struct.sockaddr* %54, i32* %8)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %61
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%59 = load i32, i32* %9
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %3
br label %69
61: ; preds = %52
%62 = load i32, i32* %9
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 0
%65 = load i32, i32* %64
%66 = load i32*, i32** %5
store i32 %65, i32* %66
%67 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 1
%68 = load i32, i32* %67
store i32 %68, i32* %3
br label %69
69: ; preds = %61, %57, %48, %30, %15
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.sockaddr_ll = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"socket(PF_PACKET)\00"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [13 x i8] c"SIOCGIFINDEX\00"
@AF_PACKET = global i32 0
@ETH_P_LOOP = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [12 x i8] c"getsockname\00"
define i32 @get_address(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.ifreq
%4 = alloca %struct.sockaddr_ll
%5 = alloca i32
%6 = bitcast %struct.ifreq* %3 to i8*
%7 = bitcast %struct.sockaddr_ll* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i32, i32* @PF_PACKET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %56
15: ; preds = %2
%16 = bitcast %struct.ifreq* %3 to %struct.sockaddr_ll*
%17 = call i32 @memset(%struct.sockaddr_ll* %16, i32 0, i32 28)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 6
%19 = load i32, i32* %18
%20 = load i32, i32* @IFNAMSIZ
%21 = call i32 @strncpy(i32 %19, i8* %0, i32 %20)
%22 = load i32, i32* @SIOCGIFINDEX
%23 = call i64 @ioctl(i32 %11, i32 %22, %struct.sockaddr_ll* %16)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @close(i32 %11)
br label %56
28: ; preds = %15
%29 = call i32 @memset(%struct.sockaddr_ll* %4, i32 0, i32 28)
%30 = load i32, i32* @AF_PACKET
%31 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 5
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 3
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 4
store i32 %33, i32* %34
%35 = load i32, i32* @ETH_P_LOOP
%36 = call i32 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 2
store i32 %36, i32* %37
%38 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%39 = call i32 @bind(i32 %11, %struct.sockaddr* %38, i32 28)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %28
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @close(i32 %11)
br label %56
44: ; preds = %28
store i32 28, i32* %5
%45 = call i32 @getsockname(i32 %11, %struct.sockaddr* %38, i32* %5)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @close(i32 %11)
br label %56
50: ; preds = %44
%51 = call i32 @close(i32 %11)
%52 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 0
%53 = load i32, i32* %52
store i32 %53, i32* %1
%54 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 1
%55 = load i32, i32* %54
br label %56
56: ; preds = %50, %47, %41, %25, %13
%57 = phi i32 [ -1, %13 ], [ -1, %25 ], [ -1, %41 ], [ -1, %47 ], [ %55, %50 ]
ret i32 %57
}