#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_flags;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFF_UP;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int enable_eth(const char *iface, int up) {
  int sockfd;
  struct ifreq ifr;
  int ret = -1;
  if (iface == NULL)
    return -1;
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    return sockfd;
  strncpy(ifr.ifr_name, iface, 16);
  if (ret = ioctl(sockfd, SIOCGIFFLAGS, (caddr_t)&ifr))
    goto _end;
  if (up) {
    ifr.ifr_flags |= IFF_UP;
  } else {
    ifr.ifr_flags &= (~IFF_UP);
  }
  ret = ioctl(sockfd, SIOCSIFFLAGS, (caddr_t)&ifr);
_end:
  close(sockfd);
  return ret;
}


x86 assembly (clang, -O0):
<enable_eth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <enable_eth+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <enable_eth+0xee>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    119d <enable_eth+0x5d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122e <enable_eth+0xee>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rax
mov    %eax,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11d6 <enable_eth+0x96>
jmp    1220 <enable_eth+0xe0>
cmpl   $0x0,-0x14(%rbp)
je     11f4 <enable_eth+0xb4>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1206 <enable_eth+0xc6>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rax
mov    %eax,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enable_eth>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11ad <enable_eth+0x6d>
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d7 <enable_eth+0x97>
mov    %rbp,%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14d,%edx
call   1040 <ioctl@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11ce <enable_eth+0x8e>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
test   %r15d,%r15d
je     11b4 <enable_eth+0x74>
or     %eax,(%rsp)
jmp    11b9 <enable_eth+0x79>
mov    $0xffffffff,%ebx
jmp    11d7 <enable_eth+0x97>
not    %eax
and    %eax,(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1040 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @enable_eth(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %8
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %53
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %53
19: ; preds = %12
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load i8*, i8** %4
%23 = call i32 @strncpy(i32 %21, i8* %22, i32 16)
%24 = load i32, i32* %6
%25 = load i32, i32* @SIOCGIFFLAGS
%26 = ptrtoint %struct.ifreq* %7 to i32
%27 = call i32 @ioctl(i32 %24, i32 %25, i32 %26)
store i32 %27, i32* %8
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %19
br label %49
30: ; preds = %19
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* @IFF_UP
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%36 = load i32, i32* %35
%37 = or i32 %36, %34
store i32 %37, i32* %35
br label %44
38: ; preds = %30
%39 = load i32, i32* @IFF_UP
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = and i32 %42, %40
store i32 %43, i32* %41
br label %44
44: ; preds = %38, %33
%45 = load i32, i32* %6
%46 = load i32, i32* @SIOCSIFFLAGS
%47 = ptrtoint %struct.ifreq* %7 to i32
%48 = call i32 @ioctl(i32 %45, i32 %46, i32 %47)
store i32 %48, i32* %8
br label %49
49: ; preds = %44, %29
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %8
store i32 %52, i32* %3
br label %53
53: ; preds = %49, %17, %11
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @enable_eth(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %36, label %6
6: ; preds = %2
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %36, label %11
11: ; preds = %6
%12 = tail call i32 @strncpy(i32 undef, i8* %0, i32 16)
%13 = load i32, i32* @SIOCGIFFLAGS
%14 = ptrtoint %struct.ifreq* %3 to i64
%15 = trunc i64 %14 to i32
%16 = call i32 @ioctl(i32 %9, i32 %13, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %11
%19 = icmp eq i32 %1, 0
%20 = load i32, i32* @IFF_UP
br i1 %19, label %25, label %21
21: ; preds = %18
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = or i32 %23, %20
store i32 %24, i32* %22
br label %30
25: ; preds = %18
%26 = xor i32 %20, -1
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, %26
store i32 %29, i32* %27
br label %30
30: ; preds = %25, %21
%31 = load i32, i32* @SIOCSIFFLAGS
%32 = call i32 @ioctl(i32 %9, i32 %31, i32 %15)
br label %33
33: ; preds = %11, %30
%34 = phi i32 [ %16, %11 ], [ %32, %30 ]
%35 = call i32 @close(i32 %9)
br label %36
36: ; preds = %6, %2, %33
%37 = phi i32 [ %34, %33 ], [ -1, %2 ], [ %9, %6 ]
ret i32 %37
}