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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFF_UP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int const);
scalar_t__ ioctl(int const, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int lo_up() {
  const int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    perror("socket");
    return 1;
  }
  struct ifreq req;
  memset(&req, 0, sizeof(req));
  strncpy(req.ifr_name, "lo", IFNAMSIZ);
  if (ioctl(sock, SIOCGIFFLAGS, &req) < 0) {
    perror("SIOCGIFFLAGS");
    return 1;
  }
  req.ifr_flags |= IFF_UP;
  if (ioctl(sock, SIOCSIFFLAGS, &req) < 0) {
    perror("SIOCSIFFLAGS");
    return 1;
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<lo_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a6 <lo_up+0x46>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <lo_up+0xfa>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    0xe3e(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1205 <lo_up+0xa5>
lea    0xe16(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <lo_up+0xfa>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    124b <lo_up+0xeb>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <lo_up+0xfa>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lo_up>:
push   %r14
push   %rbx
push   %rax
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11fb <lo_up+0x9b>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
lea    0xe5e(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1204 <lo_up+0xa4>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
or     %eax,(%rsp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     120d <lo_up+0xad>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdfe(%rip),%rdi
jmp    1214 <lo_up+0xb4>
lea    0xdff(%rip),%rdi
jmp    1214 <lo_up+0xb4>
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11f3 <lo_up+0x93>

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

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