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

/* Variables and functions */
int IFF_UP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int bring_up_interface(char *dev) {
  uint32_t mask = IFF_UP;
  uint32_t flags = IFF_UP;
  struct ifreq ifr;
  int fd;
  int err;
  fd = socket(PF_INET, SOCK_DGRAM, 0);
  if (fd < 0) {
    perror("Unable to create control socket");
    return -1;
  }
  strcpy(ifr.ifr_name, dev);
  err = ioctl(fd, SIOCGIFFLAGS, &ifr);
  if (err) {
    perror("SIOCGIFFLAGS");
    close(fd);
    return -1;
  }
  if ((ifr.ifr_flags ^ flags) & mask) {
    ifr.ifr_flags &= ~mask;
    ifr.ifr_flags |= mask & flags;
    err = ioctl(fd, SIOCSIFFLAGS, &ifr);
    if (err)
      perror("SIOCSIFFLAGS");
  }
  close(fd);
  return err;
}


x86 assembly (clang, -O0):
<bring_up_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b2 <bring_up_interface+0x62>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <bring_up_interface+0x11b>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1200 <bring_up_interface+0xb0>
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <bring_up_interface+0x11b>
mov    -0x20(%rbp),%eax
xor    -0x18(%rbp),%eax
and    -0x14(%rbp),%eax
cmp    $0x0,%eax
je     125d <bring_up_interface+0x10d>
mov    -0x14(%rbp),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
and    -0x18(%rbp),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1258 <bring_up_interface+0x108>
lea    0xdda(%rip),%rdi
call   1060 <perror@plt>
jmp    125d <bring_up_interface+0x10d>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bring_up_interface>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
mov    0x2e7e(%rip),%rax
mov    (%rax),%r14d
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11e6 <bring_up_interface+0x96>
mov    %eax,%ebx
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
jne    11f9 <bring_up_interface+0xa9>
mov    0x8(%rsp),%eax
mov    %eax,%ecx
not    %ecx
xor    %ebp,%ebp
test   %ecx,%r14d
je     11d4 <bring_up_interface+0x84>
or     %r14d,%eax
mov    %eax,0x8(%rsp)
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
jne    1213 <bring_up_interface+0xc3>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11db <bring_up_interface+0x8b>
lea    0xe20(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
jmp    11db <bring_up_interface+0x8b>
mov    %eax,%r14d
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
mov    %r14d,%ebp
jmp    11d4 <bring_up_interface+0x84>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFF_UP = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [32 x i8] c"Unable to create control socket\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [13 x i8] c"SIOCGIFFLAGS\00"
@SIOCSIFFLAGS = global i32 0
@.str.2 = [13 x i8] c"SIOCSIFFLAGS\00"
define i32 @bring_up_interface(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @IFF_UP
store i32 %9, i32* %4
%10 = load i32, i32* @IFF_UP
store i32 %10, i32* %5
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %64
18: ; preds = %1
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load i8*, i8** %3
%22 = call i32 @strcpy(i32 %20, i8* %21)
%23 = load i32, i32* %7
%24 = load i32, i32* @SIOCGIFFLAGS
%25 = call i32 @ioctl(i32 %23, i32 %24, %struct.ifreq* %6)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %18
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %2
br label %64
32: ; preds = %18
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* %5
%36 = xor i32 %34, %35
%37 = load i32, i32* %4
%38 = and i32 %36, %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %60
40: ; preds = %32
%41 = load i32, i32* %4
%42 = xor i32 %41, -1
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%44 = load i32, i32* %43
%45 = and i32 %44, %42
store i32 %45, i32* %43
%46 = load i32, i32* %4
%47 = load i32, i32* %5
%48 = and i32 %46, %47
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%50 = load i32, i32* %49
%51 = or i32 %50, %48
store i32 %51, i32* %49
%52 = load i32, i32* %7
%53 = load i32, i32* @SIOCSIFFLAGS
%54 = call i32 @ioctl(i32 %52, i32 %53, %struct.ifreq* %6)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %40
%58 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %57, %40
br label %60
60: ; preds = %59, %32
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %8
store i32 %63, i32* %2
br label %64
64: ; preds = %60, %28, %16
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFF_UP = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [32 x i8] c"Unable to create control socket\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [13 x i8] c"SIOCGIFFLAGS\00"
@SIOCSIFFLAGS = global i32 0
@.str.2 = [13 x i8] c"SIOCSIFFLAGS\00"
define i32 @bring_up_interface(i8* %0) {
%2 = alloca %struct.ifreq
%3 = load i32, i32* @IFF_UP
%4 = bitcast %struct.ifreq* %2 to i8*
%5 = load i32, i32* @PF_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 = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %35
11: ; preds = %1
%12 = tail call i32 @strcpy(i32 undef, i8* %0)
%13 = load i32, i32* @SIOCGIFFLAGS
%14 = call i32 @ioctl(i32 %7, i32 %13, %struct.ifreq* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @close(i32 %7)
br label %35
19: ; preds = %11
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = xor i32 %21, -1
%23 = and i32 %3, %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %19
%26 = or i32 %21, %3
store i32 %26, i32* %20
%27 = load i32, i32* @SIOCSIFFLAGS
%28 = call i32 @ioctl(i32 %7, i32 %27, %struct.ifreq* %2)
%29 = icmp eq i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %25, %30, %19
%33 = phi i32 [ %28, %30 ], [ 0, %25 ], [ 0, %19 ]
%34 = call i32 @close(i32 %7)
br label %35
35: ; preds = %32, %16, %9
%36 = phi i32 [ -1, %9 ], [ -1, %16 ], [ %33, %32 ]
ret i32 %36
}