#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 /*<<< orphan*/ me;
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_LOOP;
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*/ strcpy(int /*<<< orphan*/, char *);
int get_address(char *dev, int *htype) {
  struct ifreq ifr;
  struct sockaddr_ll me;
  int alen;
  int s;
  s = socket(PF_PACKET, SOCK_DGRAM, 0);
  if (s < 0) {
    perror("socket(PF_PACKET)");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, dev);
  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    12fd <get_address+0x16d>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x60(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1239 <get_address+0xa9>
lea    0xdf2(%rip),%rdi
call   1090 <perror@plt>
mov    -0x60(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fd <get_address+0x16d>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d66(%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    12a6 <get_address+0x116>
lea    0xd92(%rip),%rdi
call   1090 <perror@plt>
mov    -0x60(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fd <get_address+0x16d>
movl   $0x1c,-0x5c(%rbp)
mov    -0x60(%rbp),%edi
lea    -0x58(%rbp),%rsi
lea    -0x5c(%rbp),%rdx
call   10a0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    12e6 <get_address+0x156>
lea    0xd57(%rip),%rdi
call   1090 <perror@plt>
mov    -0x60(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fd <get_address+0x16d>
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     128b <get_address+0xfb>
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    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     129e <get_address+0x10e>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1050 <memset@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x34(%rsp),%eax
mov    %eax,0x18(%rsp)
mov    0x2d98(%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     12a7 <get_address+0x117>
movl   $0x1c,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <getsockname@plt>
cmp    $0xffffffff,%eax
je     12b0 <get_address+0x120>
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    0xd6e(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    127f <get_address+0xef>
lea    0xd6d(%rip),%rdi
jmp    12b7 <get_address+0x127>
lea    0xd71(%rip),%rdi
jmp    12b7 <get_address+0x127>
lea    0xd6d(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
jmp    127f <get_address+0xef>

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"
@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 %68
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 = call i32 @strcpy(i32 %21, i8* %22)
%24 = load i32, i32* %9
%25 = load i32, i32* @SIOCGIFINDEX
%26 = bitcast %struct.ifreq* %6 to %struct.sockaddr_ll*
%27 = call i64 @ioctl(i32 %24, i32 %25, %struct.sockaddr_ll* %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %17
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %9
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %68
33: ; preds = %17
%34 = call i32 @memset(%struct.sockaddr_ll* %7, i32 0, i32 28)
%35 = load i32, i32* @AF_PACKET
%36 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 5
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 3
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 4
store i32 %38, i32* %39
%40 = load i32, i32* @ETH_P_LOOP
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 2
store i32 %41, i32* %42
%43 = load i32, i32* %9
%44 = bitcast %struct.sockaddr_ll* %7 to %struct.sockaddr*
%45 = call i32 @bind(i32 %43, %struct.sockaddr* %44, i32 28)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %33
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* %9
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %3
br label %68
51: ; preds = %33
store i32 28, i32* %8
%52 = load i32, i32* %9
%53 = bitcast %struct.sockaddr_ll* %7 to %struct.sockaddr*
%54 = call i32 @getsockname(i32 %52, %struct.sockaddr* %53, i32* %8)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* %9
%59 = call i32 @close(i32 %58)
store i32 -1, i32* %3
br label %68
60: ; preds = %51
%61 = load i32, i32* %9
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 0
%64 = load i32, i32* %63
%65 = load i32*, i32** %5
store i32 %64, i32* %65
%66 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 1
%67 = load i32, i32* %66
store i32 %67, i32* %3
br label %68
68: ; preds = %60, %56, %47, %29, %15
%69 = load i32, i32* %3
ret i32 %69
}

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"
@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 %55
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 = call i32 @strcpy(i32 %19, i8* %0)
%21 = load i32, i32* @SIOCGIFINDEX
%22 = call i64 @ioctl(i32 %11, i32 %21, %struct.sockaddr_ll* %16)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @close(i32 %11)
br label %55
27: ; preds = %15
%28 = call i32 @memset(%struct.sockaddr_ll* %4, i32 0, i32 28)
%29 = load i32, i32* @AF_PACKET
%30 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 5
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 4
store i32 %32, i32* %33
%34 = load i32, i32* @ETH_P_LOOP
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 2
store i32 %35, i32* %36
%37 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%38 = call i32 @bind(i32 %11, %struct.sockaddr* %37, i32 28)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %27
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @close(i32 %11)
br label %55
43: ; preds = %27
store i32 28, i32* %5
%44 = call i32 @getsockname(i32 %11, %struct.sockaddr* %37, i32* %5)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @close(i32 %11)
br label %55
49: ; preds = %43
%50 = call i32 @close(i32 %11)
%51 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 0
%52 = load i32, i32* %51
store i32 %52, i32* %1
%53 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 1
%54 = load i32, i32* %53
br label %55
55: ; preds = %49, %46, %40, %24, %13
%56 = phi i32 [ -1, %13 ], [ -1, %24 ], [ -1, %40 ], [ -1, %46 ], [ %54, %49 ]
ret i32 %56
}