#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 ifreq {
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ IPPROTO_RAW;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bzero(struct ifreq *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ioctl(int, unsigned int, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
struct ifreq *get_ifreq(char *ifname, unsigned int cmd) {
  static struct ifreq ifr;
  bzero(&ifr, sizeof(ifr));
  int fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  if (fd < 0) {
    fprintf(stderr, "socket error!\n");
  } else {
    strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
    ioctl(fd, cmd, &ifr);
    close(fd);
  }
  return &ifr;
}


x86 assembly (clang, -O0):
<get_ifreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0x2ede(%rip),%rdi
mov    $0x4,%esi
call   1070 <bzero@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c7 <get_ifreq+0x67>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11f9 <get_ifreq+0x99>
mov    0x2e87(%rip),%edi
mov    -0x8(%rbp),%rsi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    0x2e68(%rip),%rdx
call   1040 <ioctl@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
lea    0x2e54(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ifreq>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x2ee0(%rip),%rdi
mov    $0x4,%esi
call   1070 <bzero@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11d8 <get_ifreq+0x78>
mov    %eax,%ebp
lea    0x2ea9(%rip),%r15
mov    0x2ea3(%rip),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %r15,%rdx
call   1040 <ioctl@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11ed <get_ifreq+0x8d>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1060 <fprintf@plt>
lea    0x2e60(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32 }
@get_ifreq.ifr = internal global %struct.ifreq zeroinitializer
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"socket error!\0A\00"
@IFNAMSIZ = global i32 0
define %struct.ifreq* @get_ifreq(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = call i32 @bzero(%struct.ifreq* @get_ifreq.ifr, i32 4)
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @IPPROTO_RAW
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
16: ; preds = %2
%17 = load i32, i32* getelementptr inbounds (%struct.ifreq, %struct.ifreq* @get_ifreq.ifr, i32 0, i32 0)
%18 = load i8*, i8** %3
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strncpy(i32 %17, i8* %18, i32 %19)
%21 = load i32, i32* %5
%22 = load i32, i32* %4
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* @get_ifreq.ifr)
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %16, %13
ret %struct.ifreq* @get_ifreq.ifr
}

LLVM IR (-O2):
%struct.ifreq = type { i32 }
@get_ifreq.ifr = internal global %struct.ifreq zeroinitializer
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_RAW = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"socket error!\0A\00"
@IFNAMSIZ = global i32 0
define %struct.ifreq* @get_ifreq(i8* %0, i32 %1) {
%3 = tail call i32 @bzero(%struct.ifreq* @get_ifreq.ifr, i32 4)
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @IPPROTO_RAW
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %18
12: ; preds = %2
%13 = load i32, i32* getelementptr inbounds (%struct.ifreq, %struct.ifreq* @get_ifreq.ifr, i64 0, i32 0)
%14 = load i32, i32* @IFNAMSIZ
%15 = tail call i32 @strncpy(i32 %13, i8* %0, i32 %14)
%16 = tail call i32 @ioctl(i32 %7, i32 %1, %struct.ifreq* @get_ifreq.ifr)
%17 = tail call i32 @close(i32 %7)
br label %18
18: ; preds = %12, %9
ret %struct.ifreq* @get_ifreq.ifr
}