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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCSMIIREG;
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 *);
int smi_write(unsigned short phyaddr, unsigned short regnum,
              unsigned short regvalue) {
  int fd;
  struct ifreq ifr;
  struct mii_ioctl_data *smi_data = NULL;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0) {
    perror("  smi_write socket error !\n");
    return -1;
  }
  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;
  smi_data->val_in = regvalue;
  if (ioctl(fd, SIOCSMIIREG, &ifr) < 0) {
    perror("  smi_write ioctl error !\n");
    close(fd);
    return -1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<smi_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %si,%cx
mov    %di,%dx
mov    %dx,-0x6(%rbp)
mov    %cx,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c3 <smi_write+0x63>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <smi_write+0xf3>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
lea    0xe3f(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x6(%rbp),%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x8(%rbp),%cx
mov    -0x20(%rbp),%rax
mov    %cx,0x2(%rax)
mov    -0xa(%rbp),%cx
mov    -0x20(%rbp),%rax
mov    %cx,0x4(%rax)
mov    -0x10(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    124c <smi_write+0xec>
lea    0xdee(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <smi_write+0xf3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smi_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r12d
mov    %edi,%ebx
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f1 <smi_write+0x91>
mov    %eax,%ebp
mov    %rsp,%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
lea    0xe6a(%rip),%rsi
call   1030 <strcpy@plt>
mov    %bx,(%rsp)
mov    %r12w,0x2(%rsp)
mov    %r15w,0x4(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11ff <smi_write+0x9f>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe08(%rip),%rdi
call   1070 <perror@plt>
jmp    1212 <smi_write+0xb2>
lea    0xe1b(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%r14d
jmp    11df <smi_write+0x7f>

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

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