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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
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*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int net_set_flag(char *ifname, short flag) {
  struct ifreq ifr;
  int skfd;
  if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket error");
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
    perror("net_set_flag: ioctl SIOCGIFFLAGS");
    close(skfd);
    return (-1);
  }
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ifr.ifr_flags |= flag;
  if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
    perror("net_set_flag: ioctl SIOCSIFFLAGS");
    close(skfd);
    return -1;
  }
  close(skfd);
  return (0);
}


x86 assembly (clang, -O0):
<net_set_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11a0 <net_set_flag+0x50>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <net_set_flag+0x115>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11f4 <net_set_flag+0xa4>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <net_set_flag+0x115>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
movswl -0x12(%rbp),%ecx
movswl -0x20(%rbp),%eax
or     %ecx,%eax
mov    %ax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1256 <net_set_flag+0x106>
lea    0xdf1(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <net_set_flag+0x115>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_set_flag>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11e6 <net_set_flag+0x96>
mov    %eax,%ebx
mov    0x2e3d(%rip),%r15
mov    (%r15),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11f9 <net_set_flag+0xa9>
mov    0x4(%rsp),%edi
mov    (%r15),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
or     %r14w,(%rsp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1202 <net_set_flag+0xb2>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11db <net_set_flag+0x8b>
lea    0xe0d(%rip),%rdi
jmp    1209 <net_set_flag+0xb9>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
jmp    11db <net_set_flag+0x8b>

LLVM IR (-O0):
%struct.ifreq = type { i16, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@IFNAMSIZ = global i32 0
@SIOCGIFFLAGS = global i32 0
@.str.1 = [33 x i8] c"net_set_flag: ioctl SIOCGIFFLAGS\00"
@SIOCSIFFLAGS = global i32 0
@.str.2 = [33 x i8] c"net_set_flag: ioctl SIOCSIFFLAGS\00"
define i32 @net_set_flag(i8* %0, i16 signext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
14: ; preds = %2
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %4
%18 = load i32, i32* @IFNAMSIZ
%19 = call i32 @strncpy(i32 %16, i8* %17, i32 %18)
%20 = load i32, i32* %7
%21 = load i32, i32* @SIOCGIFFLAGS
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %6)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %52
28: ; preds = %14
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i8*, i8** %4
%32 = load i32, i32* @IFNAMSIZ
%33 = call i32 @strncpy(i32 %30, i8* %31, i32 %32)
%34 = load i16, i16* %5
%35 = sext i16 %34 to i32
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%37 = load i16, i16* %36
%38 = sext i16 %37 to i32
%39 = or i32 %38, %35
%40 = trunc i32 %39 to i16
store i16 %40, i16* %36
%41 = load i32, i32* %7
%42 = load i32, i32* @SIOCSIFFLAGS
%43 = call i64 @ioctl(i32 %41, i32 %42, %struct.ifreq* %6)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %28
%46 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %3
br label %52
49: ; preds = %28
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
store i32 0, i32* %3
br label %52
52: ; preds = %49, %45, %24, %12
%53 = load i32, i32* %3
ret i32 %53
}

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