#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, ...);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
int GetIpFromDevice(uint32_t ip, const char DeviceName[]) {
  int fd;
  struct ifreq ifr;
  if (strlen(DeviceName) > IFNAMSIZ) {
    fprintf(stderr, "[%s][%d] error: strlen(DeviceName) > IFNAMSIZ\n",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/weiqiangdragonite/blog_tmp/refs/heads/"
            "master/unix_linux/802.1x/mytest/raw_main.c",
            431);
    return -1;
  }
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0) {
    fprintf(stderr, "[%s][%d] socker() error: %s\n",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/weiqiangdragonite/blog_tmp/refs/heads/"
            "master/unix_linux/802.1x/mytest/raw_main.c",
            439, strerror(errno));
    return -1;
  }
  strncpy(ifr.ifr_name, DeviceName, IFNAMSIZ);
  ifr.ifr_addr.sa_family = AF_INET;
  if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
    struct sockaddr_in *p = (void *)&(ifr.ifr_addr);
    memcpy(&ip, &(p->sin_addr), 4);
  } else {
    char *ip_str = "128.128.166.7";
    struct sockaddr_in addr4;
    if (inet_pton(AF_INET, ip_str, &addr4.sin_addr) != 1) {
      fprintf(stderr, "[%s][%d] inet_pton() error: \n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/weiqiangdragonite/blog_tmp/refs/heads/"
              "master/unix_linux/802.1x/mytest/raw_main.c",
              462, strerror(errno));
      return -1;
    }
    memcpy(&ip, &(addr4.sin_addr), 4);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<GetIpFromDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    0x2e11(%rip),%rcx
cmp    (%rcx),%rax
jle    11e7 <GetIpFromDevice+0x57>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
lea    0xe60(%rip),%rdx
mov    $0x1af,%ecx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <GetIpFromDevice+0x1b4>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1253 <GetIpFromDevice+0xc3>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%r8
lea    0xea6(%rip),%rsi
lea    0xdf4(%rip),%rdx
mov    $0x1b7,%ecx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <GetIpFromDevice+0x1b4>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d5f(%rip),%rax
mov    (%rax),%rdx
call   1030 <strncpy@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    12b3 <GetIpFromDevice+0x123>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    -0x8(%rbp),%rdi
mov    $0x4,%edx
call   1080 <memcpy@plt>
jmp    1335 <GetIpFromDevice+0x1a5>
lea    0xe3d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1090 <inet_pton@plt>
cmp    $0x1,%eax
je     1323 <GetIpFromDevice+0x193>
mov    0x2ce4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%r8
lea    0xe01(%rip),%rsi
lea    0xd24(%rip),%rdx
mov    $0x1ce,%ecx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <GetIpFromDevice+0x1b4>
lea    -0x38(%rbp),%rsi
lea    -0x8(%rbp),%rdi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetIpFromDevice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,0x4(%rsp)
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    0x2e10(%rip),%rbp
cmp    0x0(%rbp),%rax
jle    11de <GetIpFromDevice+0x4e>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
lea    0xe62(%rip),%rdx
mov    $0x1af,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12d6 <GetIpFromDevice+0x146>
mov    0x2dfb(%rip),%r14
mov    (%r14),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1252 <GetIpFromDevice+0xc2>
mov    %eax,%r15d
mov    0x0(%rbp),%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r15d,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
je     1280 <GetIpFromDevice+0xf0>
mov    (%r14),%edi
lea    0xec0(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1090 <inet_pton@plt>
cmp    $0x1,%eax
jne    12a0 <GetIpFromDevice+0x110>
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rsi
jmp    128a <GetIpFromDevice+0xfa>
mov    0x2d6f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe6a(%rip),%rsi
lea    0xdb8(%rip),%rdx
mov    %ebx,%edi
mov    $0x1b7,%ecx
jmp    12cc <GetIpFromDevice+0x13c>
lea    0x4(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    12db <GetIpFromDevice+0x14b>
mov    0x2d21(%rip),%rax
mov    (%rax),%ebx
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe47(%rip),%rsi
lea    0xd6a(%rip),%rdx
mov    %ebx,%edi
mov    $0x1ce,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
@IFNAMSIZ = global i64 0
@stderr = global i32 0
@.str = [47 x i8] c"[%s][%d] error: strlen(DeviceName) > IFNAMSIZ\0A\00"
@.str.1 = [171 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/weiqiangdragonite/blog_tmp/refs/heads/master/unix_linux/802.1x/mytest/raw_main.c\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [29 x i8] c"[%s][%d] socker() error: %s\0A\00"
@errno = global i32 0
@SIOCGIFADDR = global i32 0
@.str.3 = [14 x i8] c"128.128.166.7\00"
@.str.4 = [30 x i8] c"[%s][%d] inet_pton() error: \0A\00"
define i32 @GetIpFromDevice(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca %struct.sockaddr_in*
%9 = alloca i8*
%10 = alloca %struct.sockaddr_in
store i32 %0, i32* %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
%13 = load i64, i64* @IFNAMSIZ
%14 = icmp sgt i64 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 431)
store i32 -1, i32* %3
br label %66
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 439, i8* %27)
store i32 -1, i32* %3
br label %66
29: ; preds = %18
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load i8*, i8** %5
%33 = load i64, i64* @IFNAMSIZ
%34 = call i32 @strncpy(i32 %31, i8* %32, i64 %33)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load i32, i32* %6
%39 = load i32, i32* @SIOCGIFADDR
%40 = call i64 @ioctl(i32 %38, i32 %39, %struct.ifreq* %7)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %29
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%44 = bitcast %struct.TYPE_2__* %43 to i8*
%45 = bitcast i8* %44 to %struct.sockaddr_in*
store %struct.sockaddr_in* %45, %struct.sockaddr_in** %8
%46 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %46, i32 0, i32 0
%48 = call i32 @memcpy(i32* %4, i32* %47, i32 4)
br label %63
49: ; preds = %29
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8** %9
%50 = load i32, i32* @AF_INET
%51 = load i8*, i8** %9
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%53 = call i32 @inet_pton(i32 %50, i8* %51, i32* %52)
%54 = icmp ne i32 %53, 1
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 462, i8* %58)
store i32 -1, i32* %3
br label %66
60: ; preds = %49
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%62 = call i32 @memcpy(i32* %4, i32* %61, i32 4)
br label %63
63: ; preds = %60, %42
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
store i32 0, i32* %3
br label %66
66: ; preds = %63, %55, %24, %15
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
@IFNAMSIZ = global i64 0
@stderr = global i32 0
@.str = [47 x i8] c"[%s][%d] error: strlen(DeviceName) > IFNAMSIZ\0A\00"
@.str.1 = [171 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/weiqiangdragonite/blog_tmp/refs/heads/master/unix_linux/802.1x/mytest/raw_main.c\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [29 x i8] c"[%s][%d] socker() error: %s\0A\00"
@errno = global i32 0
@SIOCGIFADDR = global i32 0
@.str.3 = [14 x i8] c"128.128.166.7\00"
@.str.4 = [30 x i8] c"[%s][%d] inet_pton() error: \0A\00"
define i32 @GetIpFromDevice(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.ifreq
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%6 = bitcast %struct.ifreq* %4 to i8*
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = load i64, i64* @IFNAMSIZ
%9 = icmp sgt i64 %7, %8
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 431)
br label %48
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 439, i8* %21)
br label %48
23: ; preds = %13
%24 = load i64, i64* @IFNAMSIZ
%25 = tail call i32 @strncpy(i32 undef, i8* %1, i64 %24)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @SIOCGIFADDR
%29 = call i64 @ioctl(i32 %16, i32 %28, %struct.ifreq* %4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i32 @memcpy(i32* %3, i32* %27, i32 4)
br label %46
33: ; preds = %23
%34 = bitcast %struct.sockaddr_in* %5 to i8*
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%37 = call i32 @inet_pton(i32 %35, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32* %36)
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @memcpy(i32* %3, i32* %36, i32 4)
br label %46
41: ; preds = %33
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 462, i8* %44)
br label %48
46: ; preds = %39, %31
%47 = call i32 @close(i32 %16)
br label %48
48: ; preds = %41, %46, %18, %10
%49 = phi i32 [ -1, %10 ], [ -1, %18 ], [ 0, %46 ], [ -1, %41 ]
ret i32 %49
}