#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*/ AF_INET;
int /*<<< orphan*/ IFF_UP;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ 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 set_if_up(char *if_name) {
  struct ifreq ifr;
  int sockfd;
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket error");
    return -1;
  }
  strcpy(ifr.ifr_name, if_name);
  if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
    perror("ioctl SIOCGIFFLAGS error");
    return -1;
  }
  ifr.ifr_flags |= IFF_UP;
  if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
    perror("ioctl SIOCSIFFLAGS error");
    return -1;
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<set_if_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1199 <set_if_up+0x49>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <set_if_up+0xe1>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11dc <set_if_up+0x8c>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <set_if_up+0xe1>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1222 <set_if_up+0xd2>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <set_if_up+0xe1>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_if_up>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11cb <set_if_up+0x7b>
mov    %eax,%ebx
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11d4 <set_if_up+0x84>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
or     %eax,(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11dd <set_if_up+0x8d>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2e(%rip),%rdi
jmp    11e4 <set_if_up+0x94>
lea    0xe32(%rip),%rdi
jmp    11e4 <set_if_up+0x94>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11c3 <set_if_up+0x73>

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [25 x i8] c"ioctl SIOCGIFFLAGS error\00"
@IFF_UP = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [25 x i8] c"ioctl SIOCSIFFLAGS error\00"
define i32 @set_if_up(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %29
10: ; preds = %1
%11 = tail call i32 @strcpy(i32 undef, i8* %0)
%12 = load i32, i32* @SIOCGIFFLAGS
%13 = call i64 @ioctl(i32 %6, i32 %12, %struct.ifreq* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %29
17: ; preds = %10
%18 = load i32, i32* @IFF_UP
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%20 = load i32, i32* %19
%21 = or i32 %20, %18
store i32 %21, i32* %19
%22 = load i32, i32* @SIOCSIFFLAGS
%23 = call i64 @ioctl(i32 %6, i32 %22, %struct.ifreq* %2)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %29
27: ; preds = %17
%28 = call i32 @close(i32 %6)
br label %29
29: ; preds = %27, %25, %15, %8
%30 = phi i32 [ -1, %8 ], [ -1, %15 ], [ -1, %25 ], [ 0, %27 ]
ret i32 %30
}