#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;
struct TYPE_2__ {
  int /*<<< orphan*/ sa_data;
};
struct ifreq {
  int ifr_ifindex;
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
scalar_t__ get_if_ip4(int, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int get_if_info(const char *ifname, uint32_t *ip, char *mac, int *ifindex) {
  printf("get_if_info for %s", ifname);
  printf("\n");
  ;
  int err = -1;
  struct ifreq ifr;
  int sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sd <= 0) {
    perror("socket()");
    goto out;
  }
  if (strlen(ifname) > (IFNAMSIZ - 1)) {
    printf("Too long interface name, MAX=%i\n", IFNAMSIZ - 1);
    goto out;
  }
  strcpy(ifr.ifr_name, ifname);
  if (ioctl(sd, SIOCGIFINDEX, &ifr) == -1) {
    perror("SIOCGIFINDEX");
    goto out;
  }
  *ifindex = ifr.ifr_ifindex;
  printf("interface index is %d\n", *ifindex);
  if (ioctl(sd, SIOCGIFHWADDR, &ifr) == -1) {
    perror("SIOCGIFINDEX");
    goto out;
  }
  memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
  if (get_if_ip4(sd, ifname, ip)) {
    goto out;
  }
  printf("get_if_info OK");
  printf("\n");
  ;
  err = 0;
out:
  if (sd > 0) {
    printf("Clean up temporary socket");
    printf("\n");
    ;
    close(sd);
  }
  return err;
}


x86 assembly (clang, -O0):
<get_if_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x24(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1060 <htons@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%esi
mov    %eax,%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     1230 <get_if_info+0x90>
lea    0xded(%rip),%rdi
call   10b0 <perror@plt>
jmp    1346 <get_if_info+0x1a6>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    0x2d80(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    126c <get_if_info+0xcc>
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
sub    $0x1,%esi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1346 <get_if_info+0x1a6>
mov    -0x28(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x34(%rbp),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12a7 <get_if_info+0x107>
lea    0xda0(%rip),%rdi
call   10b0 <perror@plt>
jmp    1346 <get_if_info+0x1a6>
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x34(%rbp),%edi
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12f3 <get_if_info+0x153>
lea    0xd54(%rip),%rdi
call   10b0 <perror@plt>
jmp    1346 <get_if_info+0x1a6>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    $0x6,%edx
call   10a0 <memcpy@plt>
mov    -0x34(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <get_if_ip4@plt>
cmp    $0x0,%rax
je     1323 <get_if_info+0x183>
jmp    1346 <get_if_info+0x1a6>
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    1374 <get_if_info+0x1d4>
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xcfa(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x34(%rbp),%edi
call   1090 <close@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_if_info>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xe2f(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%ebp
mov    0x2df3(%rip),%rax
mov    (%rax),%ebx
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1070 <htons@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   10d0 <socket@plt>
test   %eax,%eax
jle    1317 <get_if_info+0x167>
mov    %eax,%ebp
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    0x2d97(%rip),%rcx
mov    (%rcx),%esi
cmp    %esi,%eax
jge    12d1 <get_if_info+0x121>
mov    %r13,%rsi
call   1040 <strcpy@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1090 <ioctl@plt>
cmp    $0xffffffff,%eax
je     132a <get_if_info+0x17a>
mov    0x8(%rsp),%esi
mov    %esi,(%r12)
lea    0xde2(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1090 <ioctl@plt>
cmp    $0xffffffff,%eax
je     132a <get_if_info+0x17a>
mov    0xc(%rsp),%esi
mov    %r15,%rdi
mov    $0x6,%edx
call   10b0 <memcpy@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1050 <get_if_ip4@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    12e7 <get_if_info+0x137>
lea    0xda5(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    12e7 <get_if_info+0x137>
add    $0xffffffff,%esi
lea    0xd41(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xffffffff,%ebx
lea    0xd82(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf5(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1306 <get_if_info+0x156>
lea    0xd0c(%rip),%rdi
call   10c0 <perror@plt>
jmp    12e2 <get_if_info+0x132>

LLVM IR (-O0):
%struct.ifreq = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@.str = [19 x i8] c"get_if_info for %s\00"
@.str.1 = [2 x i8] c"\0A\00"
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@IFNAMSIZ = global i32 0
@.str.3 = [33 x i8] c"Too long interface name, MAX=%i\0A\00"
@SIOCGIFINDEX = global i32 0
@.str.4 = [13 x i8] c"SIOCGIFINDEX\00"
@.str.5 = [23 x i8] c"interface index is %d\0A\00"
@SIOCGIFHWADDR = global i32 0
@.str.6 = [15 x i8] c"get_if_info OK\00"
@.str.7 = [26 x i8] c"Clean up temporary socket\00"
define i32 @get_if_info(i8* %0, i32* %1, i8* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.ifreq
%11 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i32* %3, i32** %8
%12 = load i8*, i8** %5
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %9
%15 = load i32, i32* @AF_PACKET
%16 = load i32, i32* @SOCK_RAW
%17 = load i32, i32* @ETH_P_ALL
%18 = call i32 @htons(i32 %17)
%19 = call i32 @socket(i32 %15, i32 %16, i32 %18)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %4
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %73
24: ; preds = %4
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
%27 = load i32, i32* @IFNAMSIZ
%28 = sub nsw i32 %27, 1
%29 = icmp sgt i32 %26, %28
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32, i32* @IFNAMSIZ
%32 = sub nsw i32 %31, 1
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %73
34: ; preds = %24
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 2
%36 = load i32, i32* %35
%37 = load i8*, i8** %5
%38 = call i32 @strcpy(i32 %36, i8* %37)
%39 = load i32, i32* %11
%40 = load i32, i32* @SIOCGIFINDEX
%41 = call i32 @ioctl(i32 %39, i32 %40, %struct.ifreq* %10)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %73
45: ; preds = %34
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i32*, i32** %8
store i32 %47, i32* %48
%49 = load i32*, i32** %8
%50 = load i32, i32* %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %11
%53 = load i32, i32* @SIOCGIFHWADDR
%54 = call i32 @ioctl(i32 %52, i32 %53, %struct.ifreq* %10)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %45
%57 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %73
58: ; preds = %45
%59 = load i8*, i8** %7
%60 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 1
%61 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @memcpy(i8* %59, i32 %62, i32 6)
%64 = load i32, i32* %11
%65 = load i8*, i8** %5
%66 = load i32*, i32** %6
%67 = call i64 @get_if_ip4(i32 %64, i8* %65, i32* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %58
br label %73
70: ; preds = %58
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %9
br label %73
73: ; preds = %70, %69, %56, %43, %30, %22
%74 = load i32, i32* %11
%75 = icmp sgt i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %73
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%79 = load i32, i32* %11
%80 = call i32 @close(i32 %79)
br label %81
81: ; preds = %76, %73
%82 = load i32, i32* %9
ret i32 %82
}

LLVM IR (-O2):
%struct.ifreq = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@.str = [19 x i8] c"get_if_info for %s\00"
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@IFNAMSIZ = global i32 0
@.str.3 = [33 x i8] c"Too long interface name, MAX=%i\0A\00"
@SIOCGIFINDEX = global i32 0
@.str.4 = [13 x i8] c"SIOCGIFINDEX\00"
@.str.5 = [23 x i8] c"interface index is %d\0A\00"
@SIOCGIFHWADDR = global i32 0
@.str.6 = [15 x i8] c"get_if_info OK\00"
@.str.7 = [26 x i8] c"Clean up temporary socket\00"
define i32 @get_if_info(i8* %0, i32* %1, i8* %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.ifreq
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = tail call i32 @putchar(i32 10)
%8 = bitcast %struct.ifreq* %5 to i8*
%9 = load i32, i32* @AF_PACKET
%10 = load i32, i32* @SOCK_RAW
%11 = load i32, i32* @ETH_P_ALL
%12 = tail call i32 @htons(i32 %11)
%13 = tail call i32 @socket(i32 %9, i32 %10, i32 %12)
%14 = icmp slt i32 %13, 1
br i1 %14, label %47, label %15
15: ; preds = %4
%16 = tail call i32 @strlen(i8* %0)
%17 = load i32, i32* @IFNAMSIZ
%18 = icmp slt i32 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = add nsw i32 %17, -1
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %49
22: ; preds = %15
%23 = tail call i32 @strcpy(i32 undef, i8* %0)
%24 = load i32, i32* @SIOCGIFINDEX
%25 = call i32 @ioctl(i32 %13, i32 %24, %struct.ifreq* %5)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %49
29: ; preds = %22
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%31 = load i32, i32* %30
store i32 %31, i32* %3
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @SIOCGIFHWADDR
%34 = call i32 @ioctl(i32 %13, i32 %33, %struct.ifreq* %5)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %49
38: ; preds = %29
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 1, i32 0
%40 = load i32, i32* %39
%41 = call i32 @memcpy(i8* %2, i32 %40, i32 6)
%42 = call i64 @get_if_ip4(i32 %13, i8* %0, i32* %1)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%46 = call i32 @putchar(i32 10)
br label %49
47: ; preds = %4
%48 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %54
49: ; preds = %38, %44, %36, %27, %19
%50 = phi i32 [ 0, %44 ], [ -1, %38 ], [ -1, %36 ], [ -1, %27 ], [ -1, %19 ]
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%52 = call i32 @putchar(i32 10)
%53 = call i32 @close(i32 %13)
br label %54
54: ; preds = %47, %49
%55 = phi i32 [ %50, %49 ], [ -1, %47 ]
ret i32 %55
}