#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 mii_ioctl_data {
  unsigned short phy_id;
  unsigned short reg_num;
  unsigned short val_out;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifru;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGMIIREG;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
unsigned short smi_read(unsigned short phyaddr, unsigned short regnum) {
  int fd;
  struct ifreq ifr;
  struct mii_ioctl_data *smi_data = NULL;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0) {
    perror("  smi_read socket error !\n");
    return 0;
  }
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, "eth0");
  smi_data = (struct mii_ioctl_data *)&(ifr.ifr_ifru);
  smi_data->phy_id = phyaddr;
  smi_data->reg_num = regnum;
  if (ioctl(fd, SIOCGMIIREG, &ifr) < 0) {
    perror("  smi_read ioctl error !\n");
    close(fd);
    return 0;
  } else {
    return smi_data->val_out;
  }
}


x86 assembly (clang, -O0):
<smi_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %di,%cx
mov    %cx,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11bb <smi_read+0x5b>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
movw   $0x0,-0x2(%rbp)
jmp    1243 <smi_read+0xe3>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
lea    0xe46(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x6(%rbp),%cx
mov    -0x20(%rbp),%rax
mov    %cx,0x2(%rax)
mov    -0xc(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1237 <smi_read+0xd7>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movw   $0x0,-0x2(%rbp)
jmp    1243 <smi_read+0xe3>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ax
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smi_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11df <smi_read+0x7f>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
lea    0xe73(%rip),%rsi
call   1030 <strcpy@plt>
mov    %bp,(%rsp)
mov    %r14w,0x2(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11ef <smi_read+0x8f>
movzwl 0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
jmp    11d4 <smi_read+0x74>
lea    0xe2a(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11d4 <smi_read+0x74>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.mii_ioctl_data = type { i16, i16, i16 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [27 x i8] c" smi_read socket error !\0A\00"
@.str.1 = [5 x i8] c"eth0\00"
@SIOCGMIIREG = global i32 0
@.str.2 = [26 x i8] c" smi_read ioctl error !\0A\00"
define zeroext i16 @smi_read(i16 zeroext %0, i16 zeroext %1) {
%3 = alloca i16
%4 = alloca i16
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca %struct.mii_ioctl_data*
store i16 %0, i16* %4
store i16 %1, i16* %5
store %struct.mii_ioctl_data* null, %struct.mii_ioctl_data** %8
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i16 0, i16* %3
br label %41
16: ; preds = %2
%17 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 8)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i32 @strcpy(i32 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%22 = bitcast i32* %21 to %struct.mii_ioctl_data*
store %struct.mii_ioctl_data* %22, %struct.mii_ioctl_data** %8
%23 = load i16, i16* %4
%24 = load %struct.mii_ioctl_data*, %struct.mii_ioctl_data** %8
%25 = getelementptr inbounds %struct.mii_ioctl_data, %struct.mii_ioctl_data* %24, i32 0, i32 0
store i16 %23, i16* %25
%26 = load i16, i16* %5
%27 = load %struct.mii_ioctl_data*, %struct.mii_ioctl_data** %8
%28 = getelementptr inbounds %struct.mii_ioctl_data, %struct.mii_ioctl_data* %27, i32 0, i32 1
store i16 %26, i16* %28
%29 = load i32, i32* %6
%30 = load i32, i32* @SIOCGMIIREG
%31 = call i64 @ioctl(i32 %29, i32 %30, %struct.ifreq* %7)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %16
%34 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i16 0, i16* %3
br label %41
37: ; preds = %16
%38 = load %struct.mii_ioctl_data*, %struct.mii_ioctl_data** %8
%39 = getelementptr inbounds %struct.mii_ioctl_data, %struct.mii_ioctl_data* %38, i32 0, i32 2
%40 = load i16, i16* %39
store i16 %40, i16* %3
br label %41
41: ; preds = %37, %33, %14
%42 = load i16, i16* %3
ret i16 %42
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
%struct.mii_ioctl_data = type { i16, i16, i16 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [27 x i8] c" smi_read socket error !\0A\00"
@.str.1 = [5 x i8] c"eth0\00"
@SIOCGMIIREG = global i32 0
@.str.2 = [26 x i8] c" smi_read ioctl error !\0A\00"
define zeroext i16 @smi_read(i16 zeroext %0, i16 zeroext %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %28
11: ; preds = %2
%12 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @strcpy(i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = bitcast %struct.ifreq* %3 to %struct.mii_ioctl_data*
%17 = bitcast %struct.ifreq* %3 to i16*
store i16 %0, i16* %17
%18 = getelementptr inbounds %struct.mii_ioctl_data, %struct.mii_ioctl_data* %16, i64 0, i32 1
store i16 %1, i16* %18
%19 = load i32, i32* @SIOCGMIIREG
%20 = call i64 @ioctl(i32 %7, i32 %19, %struct.ifreq* %3)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %11
%23 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @close(i32 %7)
br label %28
25: ; preds = %11
%26 = bitcast i32* %13 to i16*
%27 = load i16, i16* %26
br label %28
28: ; preds = %25, %22, %9
%29 = phi i16 [ 0, %9 ], [ 0, %22 ], [ %27, %25 ]
ret i16 %29
}