#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*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ ioctl(int, int, char *);
int /*<<< orphan*/ logperror(char *);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
void rtioctl(struct in_addr addr, int op) {
  int sock;
  struct rtentry rt;
  struct sockaddr_in *sin;
  bzero((char *)&rt, 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
mov    $0x20,%esi
call   1060 <bzero@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e59(%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    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ff <rtioctl+0xaf>
lea    0xe0b(%rip),%rdi
call   1030 <logperror@plt>
jmp    125d <rtioctl+0x10d>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1255 <rtioctl+0x105>
mov    -0xc(%rbp),%eax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%eax
jne    1244 <rtioctl+0xf4>
mov    0x2d8f(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%rax
je     1250 <rtioctl+0x100>
lea    0xdc5(%rip),%rdi
call   1030 <logperror@plt>
jmp    1255 <rtioctl+0x105>
mov    -0x10(%rbp),%edi
call   1050 <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
mov    $0x20,%esi
call   1060 <bzero@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%rdi
mov    %rdi,0x20(%rsp)
mov    %rdi,0x10(%rsp)
mov    %rdi,0x18(%rsp)
mov    %ebp,0x10(%rsp)
mov    0x2e23(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     1207 <rtioctl+0xb7>
mov    %eax,%ebp
lea    0x8(%rsp),%rdx
mov    %eax,%edi
mov    %ebx,%esi
call   1040 <ioctl@plt>
test   %rax,%rax
jns    11f9 <rtioctl+0xa9>
mov    0x2de5(%rip),%rax
cmp    %ebx,(%rax)
jne    11ed <rtioctl+0x9d>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
je     11f9 <rtioctl+0xa9>
lea    0xe1c(%rip),%rdi
call   1030 <logperror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf2(%rip),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <logperror@plt>

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 @bzero(i8* %9, 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 @bzero(i8* %4, 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 = tail 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 = tail 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
}