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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EINVAL;
int IFF_UP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ log_debug(int, char *, char *);
int /*<<< orphan*/ log_error(char *, ...);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int net_ifup_netdev(char *netdev) {
  struct ifreq ifr;
  int sock;
  int ret = 0;
  if (!strlen(netdev)) {
    log_error("No netdev name in fw entry.");
    return EINVAL;
  }
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    log_error("Could not open socket to manage network "
              "(err %d - %s)",
              errno, strerror(errno));
    return errno;
  }
  memset(&ifr, 0, sizeof(ifr));
  strlcpy(ifr.ifr_name, netdev, IFNAMSIZ);
  if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
    log_error("Could not bring up netdev %s (err %d - %s)", netdev, errno,
              strerror(errno));
    ret = errno;
    goto done;
  }
  if (ifr.ifr_flags & IFF_UP) {
    log_debug(3, "%s up", netdev);
    goto done;
  }
  log_debug(3, "bringing %s up", netdev);
  memset(&ifr, 0, sizeof(ifr));
  strlcpy(ifr.ifr_name, netdev, IFNAMSIZ);
  ifr.ifr_flags = IFF_UP;
  if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
    log_error("Could not bring up netdev %s (err %d - %s)", netdev, errno,
              strerror(errno));
    ret = errno;
    goto done;
  }
done:
  close(sock);
  return ret;
}


x86 assembly (clang, -O0):
<net_ifup_netdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    11d4 <net_ifup_netdev+0x44>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <log_error@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13b6 <net_ifup_netdev+0x226>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1237 <net_ifup_netdev+0xa7>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <log_error@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13b6 <net_ifup_netdev+0x226>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
call   1090 <strlcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    12c5 <net_ifup_netdev+0x135>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
mov    %eax,%ecx
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1030 <log_error@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    13a8 <net_ifup_netdev+0x218>
mov    -0x18(%rbp),%eax
mov    0x2d09(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12f4 <net_ifup_netdev+0x164>
mov    -0x10(%rbp),%rdx
mov    $0x3,%edi
lea    0xd9c(%rip),%rsi
call   1050 <log_debug@plt>
jmp    13a8 <net_ifup_netdev+0x218>
mov    -0x10(%rbp),%rdx
mov    $0x3,%edi
lea    0xd79(%rip),%rsi
call   1050 <log_debug@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2c91(%rip),%rax
mov    (%rax),%edx
call   1090 <strlcpy@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cb4(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    13a3 <net_ifup_netdev+0x213>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2c3c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   1030 <log_error@plt>
mov    0x2c17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    13a8 <net_ifup_netdev+0x218>
jmp    13a8 <net_ifup_netdev+0x218>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_ifup_netdev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %eax,%eax
je     1241 <net_ifup_netdev+0xb1>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     125d <net_ifup_netdev+0xcd>
mov    %eax,%r14d
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2dcb(%rip),%r12
mov    (%r12),%edx
mov    %rbx,%rsi
call   1090 <strlcpy@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbp,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
js     12e0 <net_ifup_netdev+0x150>
mov    0x2dbb(%rip),%r13
mov    0x0(%r13),%eax
test   %eax,(%rsp)
je     1288 <net_ifup_netdev+0xf8>
lea    0xe59(%rip),%rsi
mov    $0x3,%edi
mov    %rbx,%rdx
call   1050 <log_debug@plt>
xor    %ebp,%ebp
jmp    1309 <net_ifup_netdev+0x179>
lea    0xdb8(%rip),%rdi
xor    %eax,%eax
call   1030 <log_error@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%ebp
jmp    1311 <net_ifup_netdev+0x181>
mov    0x2d4c(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xda7(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <log_error@plt>
mov    0x0(%rbp),%ebp
jmp    1311 <net_ifup_netdev+0x181>
lea    0xdee(%rip),%rsi
mov    $0x3,%edi
mov    %rbx,%rdx
call   1050 <log_debug@plt>
mov    %rsp,%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x4(%rsp),%edi
mov    (%r12),%edx
mov    %rbx,%rsi
call   1090 <strlcpy@plt>
mov    0x0(%r13),%eax
mov    %eax,(%rsp)
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %r15,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
jns    1309 <net_ifup_netdev+0x179>
mov    0x2cc9(%rip),%r15
mov    (%r15),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xd5a(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <log_error@plt>
mov    (%r15),%ebp
mov    %r14d,%edi
call   1080 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [28 x i8] c"No netdev name in fw entry.\00"
@EINVAL = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [54 x i8] c"Could not open socket to manage network (err %d - %s)\00"
@errno = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.2 = [43 x i8] c"Could not bring up netdev %s (err %d - %s)\00"
@IFF_UP = global i32 0
@.str.3 = [6 x i8] c"%s up\00"
@.str.4 = [15 x i8] c"bringing %s up\00"
@SIOCSIFFLAGS = global i32 0
define i32 @net_ifup_netdev(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EINVAL
store i32 %12, i32* %2
br label %78
13: ; preds = %1
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %21)
%23 = load i32, i32* @errno
store i32 %23, i32* %2
br label %78
24: ; preds = %13
%25 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load i8*, i8** %3
%29 = load i32, i32* @IFNAMSIZ
%30 = call i32 @strlcpy(i32 %27, i8* %28, i32 %29)
%31 = load i32, i32* %5
%32 = load i32, i32* @SIOCGIFFLAGS
%33 = call i64 @ioctl(i32 %31, i32 %32, %struct.ifreq* %4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %24
%36 = load i8*, i8** %3
%37 = load i32, i32* @errno
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %36, i32 %37, i32 %39)
%41 = load i32, i32* @errno
store i32 %41, i32* %6
br label %74
42: ; preds = %24
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @IFF_UP
%46 = and i32 %44, %45
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = load i8*, i8** %3
%50 = call i32 @log_debug(i32 3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %49)
br label %74
51: ; preds = %42
%52 = load i8*, i8** %3
%53 = call i32 @log_debug(i32 3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %52)
%54 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%55 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%56 = load i32, i32* %55
%57 = load i8*, i8** %3
%58 = load i32, i32* @IFNAMSIZ
%59 = call i32 @strlcpy(i32 %56, i8* %57, i32 %58)
%60 = load i32, i32* @IFF_UP
%61 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
store i32 %60, i32* %61
%62 = load i32, i32* %5
%63 = load i32, i32* @SIOCSIFFLAGS
%64 = call i64 @ioctl(i32 %62, i32 %63, %struct.ifreq* %4)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %51
%67 = load i8*, i8** %3
%68 = load i32, i32* @errno
%69 = load i32, i32* @errno
%70 = call i32 @strerror(i32 %69)
%71 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %67, i32 %68, i32 %70)
%72 = load i32, i32* @errno
store i32 %72, i32* %6
br label %74
73: ; preds = %51
br label %74
74: ; preds = %73, %66, %48, %35
%75 = load i32, i32* %5
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %6
store i32 %77, i32* %2
br label %78
78: ; preds = %74, %18, %10
%79 = load i32, i32* %2
ret i32 %79
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [28 x i8] c"No netdev name in fw entry.\00"
@EINVAL = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [54 x i8] c"Could not open socket to manage network (err %d - %s)\00"
@errno = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.2 = [43 x i8] c"Could not bring up netdev %s (err %d - %s)\00"
@IFF_UP = global i32 0
@.str.3 = [6 x i8] c"%s up\00"
@.str.4 = [15 x i8] c"bringing %s up\00"
@SIOCSIFFLAGS = global i32 0
define i32 @net_ifup_netdev(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EINVAL
br label %59
9: ; preds = %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16)
%18 = load i32, i32* @errno
br label %59
19: ; preds = %9
%20 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @IFNAMSIZ
%24 = call i32 @strlcpy(i32 %22, i8* %0, i32 %23)
%25 = load i32, i32* @SIOCGIFFLAGS
%26 = call i64 @ioctl(i32 %12, i32 %25, %struct.ifreq* %2)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %19
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %29, i32 %30)
%32 = load i32, i32* @errno
br label %56
33: ; preds = %19
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @IFF_UP
%37 = and i32 %36, %35
%38 = icmp eq i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %33
%40 = call i32 @log_debug(i32 3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %56
41: ; preds = %33
%42 = call i32 @log_debug(i32 3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%43 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%44 = load i32, i32* %21
%45 = load i32, i32* @IFNAMSIZ
%46 = call i32 @strlcpy(i32 %44, i8* %0, i32 %45)
%47 = load i32, i32* @IFF_UP
store i32 %47, i32* %34
%48 = load i32, i32* @SIOCSIFFLAGS
%49 = call i64 @ioctl(i32 %12, i32 %48, %struct.ifreq* %2)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %41
%52 = load i32, i32* @errno
%53 = call i32 @strerror(i32 %52)
%54 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %52, i32 %53)
%55 = load i32, i32* @errno
br label %56
56: ; preds = %41, %51, %39, %28
%57 = phi i32 [ %32, %28 ], [ 0, %39 ], [ %55, %51 ], [ 0, %41 ]
%58 = call i32 @close(i32 %12)
br label %59
59: ; preds = %56, %14, %6
%60 = phi i32 [ %18, %14 ], [ %57, %56 ], [ %8, %6 ]
ret i32 %60
}