#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;
};

/* Variables and functions */
int /*<<< orphan*/ IFF_RUNNING;
int /*<<< orphan*/ IFF_UP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int interfaceIoctl(int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int tunBringUp(const char *dev) {
  struct ifreq ifr;
  int err;
  memset(&ifr, 0, sizeof ifr);
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if ((err = interfaceIoctl(SIOCGIFFLAGS, &ifr)) < 0) {
    perror("tunBringUp: ioctl SIOCGIFFLAGS");
    return err;
  }
  ifr.ifr_flags |= IFF_UP;
  ifr.ifr_flags |= IFF_RUNNING;
  if ((err = interfaceIoctl(SIOCSIFFLAGS, &ifr)) < 0) {
    perror("tunBringUp: ioctl set IFF_UP IFF_RUNNING");
    return err;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tunBringUp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1060 <interfaceIoctl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11a6 <tunBringUp+0x66>
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <tunBringUp+0xc0>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1060 <interfaceIoctl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11f9 <tunBringUp+0xb9>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <tunBringUp+0xc0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tunBringUp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
call   1060 <interfaceIoctl@plt>
test   %eax,%eax
js     11c9 <tunBringUp+0x89>
mov    0x2e45(%rip),%rax
mov    0x8(%rsp),%ecx
or     (%rax),%ecx
mov    0x2e40(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <interfaceIoctl@plt>
test   %eax,%eax
js     11d2 <tunBringUp+0x92>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe30(%rip),%rdi
jmp    11d9 <tunBringUp+0x99>
lea    0xe46(%rip),%rdi
mov    %eax,%r14d
call   1050 <perror@plt>
jmp    11bd <tunBringUp+0x7d>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str = [31 x i8] c"tunBringUp: ioctl SIOCGIFFLAGS\00"
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.1 = [41 x i8] c"tunBringUp: ioctl set IFF_UP IFF_RUNNING\00"
define i32 @tunBringUp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%8 = load i32, i32* %7
%9 = load i8*, i8** %3
%10 = load i32, i32* @IFNAMSIZ
%11 = call i32 @strncpy(i32 %8, i8* %9, i32 %10)
%12 = load i32, i32* @SIOCGIFFLAGS
%13 = call i32 @interfaceIoctl(i32 %12, %struct.ifreq* %4)
store i32 %13, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %5
store i32 %17, i32* %2
br label %34
18: ; preds = %1
%19 = load i32, i32* @IFF_UP
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%21 = load i32, i32* %20
%22 = or i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @IFF_RUNNING
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @SIOCSIFFLAGS
%28 = call i32 @interfaceIoctl(i32 %27, %struct.ifreq* %4)
store i32 %28, i32* %5
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %5
store i32 %32, i32* %2
br label %34
33: ; preds = %18
store i32 0, i32* %2
br label %34
34: ; preds = %33, %30, %15
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str = [31 x i8] c"tunBringUp: ioctl SIOCGIFFLAGS\00"
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.1 = [41 x i8] c"tunBringUp: ioctl set IFF_UP IFF_RUNNING\00"
define i32 @tunBringUp(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%5 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%6 = load i32, i32* %5
%7 = load i32, i32* @IFNAMSIZ
%8 = call i32 @strncpy(i32 %6, i8* %0, i32 %7)
%9 = load i32, i32* @SIOCGIFFLAGS
%10 = call i32 @interfaceIoctl(i32 %9, %struct.ifreq* %2)
%11 = icmp slt i32 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %1
%13 = load i32, i32* @IFF_UP
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = or i32 %15, %13
%17 = load i32, i32* @IFF_RUNNING
%18 = or i32 %17, %16
store i32 %18, i32* %14
%19 = load i32, i32* @SIOCSIFFLAGS
%20 = call i32 @interfaceIoctl(i32 %19, %struct.ifreq* %2)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %12, %1
%23 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), %12 ]
%24 = phi i32 [ %10, %1 ], [ %20, %12 ]
%25 = call i32 @perror(i8* %23)
br label %26
26: ; preds = %22, %12
%27 = phi i32 [ 0, %12 ], [ %24, %22 ]
ret i32 %27
}