#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 in6_rtmsg {
  int rtmsg_dst_len;
  int rtmsg_flags;
  int rtmsg_metric;
  int rtmsg_ifindex;
  int /*<<< orphan*/ rtmsg_dst;
};
struct in6_addr {
  int dummy;
};
typedef int /*<<< orphan*/ rt;

/* Variables and functions */
int RTF_HOST;
int RTF_UP;
int /*<<< orphan*/ SIOCADDRT;
int /*<<< orphan*/ SIOCDELRT;
int ioctl(int, int /*<<< orphan*/, struct in6_rtmsg *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct in6_addr *, int);
int /*<<< orphan*/ memset(struct in6_rtmsg *, int /*<<< orphan*/, int);
int ipv6_route_op(int op, int sock_fd, struct in6_addr *addr, int prefix_len,
                  int metric, int ifindex) {
  struct in6_rtmsg rt;
  memset(&rt, 0, sizeof(rt));
  rt.rtmsg_dst_len = prefix_len;
  rt.rtmsg_flags = ((prefix_len == 128) ? (RTF_UP | RTF_HOST) : RTF_UP);
  rt.rtmsg_metric = metric;
  rt.rtmsg_ifindex = 0;
  rt.rtmsg_ifindex = ifindex;
  memcpy(&rt.rtmsg_dst, addr, sizeof(struct in6_addr));
  if (op == 0) {
    return ioctl(sock_fd, SIOCADDRT, &rt);
  } else if (op == 1) {
    return ioctl(sock_fd, SIOCDELRT, &rt);
  } else
    return -1;
}


x86 assembly (clang, -O0):
<ipv6_route_op>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x80,-0x1c(%rbp)
jne    118a <ipv6_route_op+0x5a>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    0x2e58(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x3c(%rbp)
jmp    1196 <ipv6_route_op+0x66>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x10,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11ec <ipv6_route_op+0xbc>
mov    -0xc(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    121a <ipv6_route_op+0xea>
cmpl   $0x1,-0x8(%rbp)
jne    1213 <ipv6_route_op+0xe3>
mov    -0xc(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    121a <ipv6_route_op+0xea>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv6_route_op>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%r12d
mov    %r8d,%r13d
mov    %ecx,%ebx
mov    %rdx,%r15
mov    %esi,0xc(%rsp)
mov    %edi,%ebp
lea    0x10(%rsp),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    %ebx,0x10(%rsp)
mov    0x2e5a(%rip),%rax
cmp    $0x80,%ebx
jne    1180 <ipv6_route_op+0x50>
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%r14d
or     (%rax),%r14d
mov    %r14d,0x14(%rsp)
mov    %r13d,0x18(%rsp)
mov    %r12d,0x1c(%rsp)
lea    0x20(%rsp),%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
test   %ebp,%ebp
je     11bb <ipv6_route_op+0x8b>
mov    $0xffffffff,%eax
cmp    $0x1,%ebp
jne    11d2 <ipv6_route_op+0xa2>
mov    0x2e2f(%rip),%rax
jmp    11c2 <ipv6_route_op+0x92>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    0xc(%rsp),%edi
call   1040 <ioctl@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
%struct.in6_rtmsg = type { i32, i32, i32, i32, i32 }
@RTF_UP = global i32 0
@RTF_HOST = global i32 0
@SIOCADDRT = global i32 0
@SIOCDELRT = global i32 0
define i32 @ipv6_route_op(i32 %0, i32 %1, %struct.in6_addr* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.in6_addr*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.in6_rtmsg
store i32 %0, i32* %8
store i32 %1, i32* %9
store %struct.in6_addr* %2, %struct.in6_addr** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
%15 = call i32 @memset(%struct.in6_rtmsg* %14, i32 0, i32 20)
%16 = load i32, i32* %11
%17 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %14, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %11
%19 = icmp eq i32 %18, 128
br i1 %19, label %20, label %24
20: ; preds = %6
%21 = load i32, i32* @RTF_UP
%22 = load i32, i32* @RTF_HOST
%23 = or i32 %21, %22
br label %26
24: ; preds = %6
%25 = load i32, i32* @RTF_UP
br label %26
26: ; preds = %24, %20
%27 = phi i32 [ %23, %20 ], [ %25, %24 ]
%28 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %14, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* %12
%30 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %14, i32 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %14, i32 0, i32 3
store i32 0, i32* %31
%32 = load i32, i32* %13
%33 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %14, i32 0, i32 3
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %14, i32 0, i32 4
%35 = load %struct.in6_addr*, %struct.in6_addr** %10
%36 = call i32 @memcpy(i32* %34, %struct.in6_addr* %35, i32 4)
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %26
%40 = load i32, i32* %9
%41 = load i32, i32* @SIOCADDRT
%42 = call i32 @ioctl(i32 %40, i32 %41, %struct.in6_rtmsg* %14)
store i32 %42, i32* %7
br label %51
43: ; preds = %26
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = load i32, i32* %9
%48 = load i32, i32* @SIOCDELRT
%49 = call i32 @ioctl(i32 %47, i32 %48, %struct.in6_rtmsg* %14)
store i32 %49, i32* %7
br label %51
50: ; preds = %43
store i32 -1, i32* %7
br label %51
51: ; preds = %50, %46, %39
%52 = load i32, i32* %7
ret i32 %52
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
%struct.in6_rtmsg = type { i32, i32, i32, i32, i32 }
@RTF_UP = global i32 0
@RTF_HOST = global i32 0
@SIOCADDRT = global i32 0
@SIOCDELRT = global i32 0
define i32 @ipv6_route_op(i32 %0, i32 %1, %struct.in6_addr* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca %struct.in6_rtmsg
%8 = bitcast %struct.in6_rtmsg* %7 to i8*
%9 = call i32 @memset(%struct.in6_rtmsg* %7, i32 0, i32 20)
%10 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %7, i64 0, i32 0
store i32 %3, i32* %10
%11 = icmp eq i32 %3, 128
%12 = load i32, i32* @RTF_UP
%13 = load i32, i32* @RTF_HOST
%14 = select i1 %11, i32 %13, i32 0
%15 = or i32 %14, %12
%16 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %7, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %7, i64 0, i32 2
store i32 %4, i32* %17
%18 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %7, i64 0, i32 3
store i32 %5, i32* %18
%19 = getelementptr inbounds %struct.in6_rtmsg, %struct.in6_rtmsg* %7, i64 0, i32 4
%20 = call i32 @memcpy(i32* %19, %struct.in6_addr* %2, i32 4)
switch i32 %0, label %26 [
i32 0, label %22
i32 1, label %21
]
21: ; preds = %6
br label %22
22: ; preds = %6, %21
%23 = phi i32* [ @SIOCDELRT, %21 ], [ @SIOCADDRT, %6 ]
%24 = load i32, i32* %23
%25 = call i32 @ioctl(i32 %1, i32 %24, %struct.in6_rtmsg* %7)
br label %26
26: ; preds = %22, %6
%27 = phi i32 [ -1, %6 ], [ %25, %22 ]
ret i32 %27
}