#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 */
typedef struct TYPE_6__ TYPE_3__;
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
};
struct TYPE_6__ {
  void *sa_family;
};
struct TYPE_5__ {
  void *sa_family;
};
struct TYPE_4__ {
  void *sa_family;
};
struct rtentry {
  int rt_flags;
  TYPE_3__ rt_gateway;
  TYPE_2__ rt_genmask;
  TYPE_1__ rt_dst;
};

/* Variables and functions */
void *AF_INET;
scalar_t__ EEXIST;
int /*<<< orphan*/ IPPROTO_UDP;
int RTF_GATEWAY;
int RTF_UP;
int SIOCADDRT;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ ioctl(int, int, char *);
int /*<<< orphan*/ logperror(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
void rtioctl(struct in_addr addr, int op) {
  int sock;
  struct rtentry rt;
  struct sockaddr_in *sin;
  memset((char *)&rt, 0, sizeof(struct rtentry));
  rt.rt_dst.sa_family = AF_INET;
  rt.rt_gateway.sa_family = AF_INET;
  rt.rt_genmask.sa_family = AF_INET;
  sin = (struct sockaddr_in *)(&rt.rt_gateway);
  sin->sin_addr = addr;
  rt.rt_flags = RTF_UP | RTF_GATEWAY;
  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0) {
    logperror("rtioctl: socket");
    return;
  }
  if (ioctl(sock, op, (char *)&rt) < 0) {
    if (!(op == SIOCADDRT && errno == EEXIST))
      logperror("ioctl (add/delete route)");
  }
  (void)close(sock);
}


x86 assembly (clang, -O0):
<rtioctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,(%rax)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    0x2e0b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1201 <rtioctl+0xb1>
lea    0xe09(%rip),%rdi
call   1030 <logperror@plt>
jmp    125f <rtioctl+0x10f>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1257 <rtioctl+0x107>
mov    -0xc(%rbp),%eax
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%eax
jne    1246 <rtioctl+0xf6>
mov    0x2d8d(%rip),%rax
mov    (%rax),%rax
mov    0x2d93(%rip),%rcx
cmp    (%rcx),%rax
je     1252 <rtioctl+0x102>
lea    0xdc3(%rip),%rdi
call   1030 <logperror@plt>
jmp    1257 <rtioctl+0x107>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtioctl>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%rdi
mov    %rdi,0x20(%rsp)
mov    %rdi,0x10(%rsp)
mov    %rdi,0x18(%rsp)
mov    %ebp,0x10(%rsp)
mov    0x2e21(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     1204 <rtioctl+0xb4>
mov    %eax,%ebp
lea    0x8(%rsp),%rdx
mov    %eax,%edi
mov    %ebx,%esi
call   1050 <ioctl@plt>
test   %rax,%rax
jns    11fb <rtioctl+0xab>
mov    0x2de3(%rip),%rax
cmp    %ebx,(%rax)
jne    11ef <rtioctl+0x9f>
mov    0x2de0(%rip),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
je     11fb <rtioctl+0xab>
lea    0xe1a(%rip),%rdi
call   1030 <logperror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1210 <rtioctl+0xc0>
lea    0xdf5(%rip),%rdi
call   1030 <logperror@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.rtentry = type { i32, %struct.TYPE_6__, %struct.TYPE_5__, %struct.TYPE_4__ }
%struct.TYPE_6__ = type { i8* }
%struct.TYPE_5__ = type { i8* }
%struct.TYPE_4__ = type { i8* }
%struct.sockaddr_in = type { %struct.in_addr }
@AF_INET = global i8* null
@RTF_UP = global i32 0
@RTF_GATEWAY = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [16 x i8] c"rtioctl: socket\00"
@SIOCADDRT = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [25 x i8] c"ioctl (add/delete route)\00"
define void @rtioctl(i32 %0, i32 %1) {
%3 = alloca %struct.in_addr
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.rtentry
%7 = alloca %struct.sockaddr_in*
%8 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
store i32 %0, i32* %8
store i32 %1, i32* %4
%9 = bitcast %struct.rtentry* %6 to i8*
%10 = call i32 @memset(i8* %9, i32 0, i32 32)
%11 = load i8*, i8** @AF_INET
%12 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %6, i32 0, i32 3
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %12, i32 0, i32 0
store i8* %11, i8** %13
%14 = load i8*, i8** @AF_INET
%15 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %6, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %15, i32 0, i32 0
store i8* %14, i8** %16
%17 = load i8*, i8** @AF_INET
%18 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %6, i32 0, i32 2
%19 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %18, i32 0, i32 0
store i8* %17, i8** %19
%20 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %6, i32 0, i32 1
%21 = bitcast %struct.TYPE_6__* %20 to %struct.sockaddr_in*
store %struct.sockaddr_in* %21, %struct.sockaddr_in** %7
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 0
%24 = bitcast %struct.in_addr* %23 to i8*
%25 = bitcast %struct.in_addr* %3 to i8*
%26 = load i32, i32* @RTF_UP
%27 = load i32, i32* @RTF_GATEWAY
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %6, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i8*, i8** @AF_INET
%31 = load i32, i32* @SOCK_DGRAM
%32 = load i32, i32* @IPPROTO_UDP
%33 = call i32 @socket(i8* %30, i32 %31, i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %2
%37 = call i32 @logperror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %58
38: ; preds = %2
%39 = load i32, i32* %5
%40 = load i32, i32* %4
%41 = bitcast %struct.rtentry* %6 to i8*
%42 = call i64 @ioctl(i32 %39, i32 %40, i8* %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %55
44: ; preds = %38
%45 = load i32, i32* %4
%46 = load i32, i32* @SIOCADDRT
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i64, i64* @errno
%50 = load i64, i64* @EEXIST
%51 = icmp eq i64 %49, %50
br i1 %51, label %54, label %52
52: ; preds = %48, %44
%53 = call i32 @logperror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
br label %55
55: ; preds = %54, %38
%56 = load i32, i32* %5
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55, %36
ret void
}

LLVM IR (-O2):
%struct.rtentry = type { i32, %struct.TYPE_6__, %struct.TYPE_5__, %struct.TYPE_4__ }
%struct.TYPE_6__ = type { i8* }
%struct.TYPE_5__ = type { i8* }
%struct.TYPE_4__ = type { i8* }
@AF_INET = global i8* null
@RTF_UP = global i32 0
@RTF_GATEWAY = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [16 x i8] c"rtioctl: socket\00"
@SIOCADDRT = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [25 x i8] c"ioctl (add/delete route)\00"
define void @rtioctl(i32 %0, i32 %1) {
%3 = alloca %struct.rtentry
%4 = bitcast %struct.rtentry* %3 to i8*
%5 = call i32 @memset(i8* %4, i32 0, i32 32)
%6 = load i8*, i8** @AF_INET
%7 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 3, i32 0
store i8* %6, i8** %7
%8 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 1
%9 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %8, i64 0, i32 0
store i8* %6, i8** %9
%10 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 2, i32 0
store i8* %6, i8** %10
%11 = bitcast %struct.TYPE_6__* %8 to i32*
store i32 %0, i32* %11
%12 = load i32, i32* @RTF_UP
%13 = load i32, i32* @RTF_GATEWAY
%14 = or i32 %13, %12
%15 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_DGRAM
%17 = load i32, i32* @IPPROTO_UDP
%18 = call i32 @socket(i8* %6, i32 %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @logperror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %36
22: ; preds = %2
%23 = call i64 @ioctl(i32 %18, i32 %1, i8* %4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i32, i32* @SIOCADDRT
%27 = icmp eq i32 %26, %1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EEXIST
%31 = icmp eq i64 %29, %30
br i1 %31, label %34, label %32
32: ; preds = %28, %25
%33 = call i32 @logperror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %28, %32, %22
%35 = call i32 @close(i32 %18)
br label %36
36: ; preds = %34, %20
ret void
}