#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_down(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_down>:
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_down+0x49>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <set_if_down+0xe4>
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_down+0x8c>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <set_if_down+0xe4>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1225 <set_if_down+0xd5>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <set_if_down+0xe4>
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_down>:
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     11cd <set_if_down+0x7d>
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     11d6 <set_if_down+0x86>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,(%rsp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11df <set_if_down+0x8f>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2c(%rip),%rdi
jmp    11e6 <set_if_down+0x96>
lea    0xe30(%rip),%rdi
jmp    11e6 <set_if_down+0x96>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11c5 <set_if_down+0x75>

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_down(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 %38
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 %38
23: ; preds = %12
%24 = load i32, i32* @IFF_UP
%25 = xor i32 %24, -1
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = and i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* %5
%30 = load i32, i32* @SIOCSIFFLAGS
%31 = call i64 @ioctl(i32 %29, i32 %30, %struct.ifreq* %4)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = 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 %38
35: ; preds = %23
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
store i32 0, i32* %2
br label %38
38: ; preds = %35, %33, %21, %10
%39 = load i32, i32* %2
ret i32 %39
}

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_down(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 %30
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 %30
17: ; preds = %10
%18 = load i32, i32* @IFF_UP
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @SIOCSIFFLAGS
%24 = call i64 @ioctl(i32 %6, i32 %23, %struct.ifreq* %2)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %30
28: ; preds = %17
%29 = call i32 @close(i32 %6)
br label %30
30: ; preds = %28, %26, %15, %8
%31 = phi i32 [ -1, %8 ], [ -1, %15 ], [ -1, %26 ], [ 0, %28 ]
ret i32 %31
}