#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_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct rtentry {
  int rt_flags;
  TYPE_1__ rt_gateway;
  TYPE_1__ rt_genmask;
  TYPE_1__ rt_dst;
  void *rt_dev;
};
typedef int /*<<< orphan*/ rt;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EEXIST;
int RTF_HOST;
int RTF_UP;
int /*<<< orphan*/ SIOCADDRT;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ init_sockaddr_in(TYPE_1__ *, int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct rtentry *);
int /*<<< orphan*/ memset(struct rtentry *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int add_host_route(const char *name, in_addr_t addr) {
  struct rtentry rt;
  int result;
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&rt, 0, sizeof(rt));
  rt.rt_dst.sa_family = AF_INET;
  rt.rt_flags = RTF_UP | RTF_HOST;
  rt.rt_dev = (void *)name;
  init_sockaddr_in(&rt.rt_dst, addr);
  init_sockaddr_in(&rt.rt_genmask, 0);
  init_sockaddr_in(&rt.rt_gateway, 0);
  result = ioctl(sockfd, SIOCADDRT, &rt);
  if (result < 0 && errno == EEXIST) {
    result = 0;
  }
  close(sockfd);
  return result;
}


x86 assembly (clang, -O0):
<add_host_route>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    0x2e31(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
add    $0xc,%rdi
mov    -0xc(%rbp),%esi
call   1060 <init_sockaddr_in@plt>
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
xor    %esi,%esi
call   1060 <init_sockaddr_in@plt>
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
xor    %esi,%esi
call   1060 <init_sockaddr_in@plt>
mov    -0x30(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1225 <add_host_route+0xd5>
mov    0x2db5(%rip),%rax
mov    (%rax),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    1225 <add_host_route+0xd5>
movl   $0x0,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_host_route>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2e7d(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%r12d
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x0(%r13),%eax
lea    0xc(%rsp),%rdi
mov    %eax,0xc(%rsp)
mov    0x2e06(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    %rbp,0x10(%rsp)
mov    %r15d,%esi
call   1060 <init_sockaddr_in@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <init_sockaddr_in@plt>
lea    0x4(%rsp),%rdi
xor    %esi,%esi
call   1060 <init_sockaddr_in@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
mov    %eax,%ebp
mov    0x2dc6(%rip),%rax
mov    (%rax),%rax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
cmovne %ebp,%ebx
test   %ebp,%ebp
cmovs  %ebx,%ebp
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rtentry = type { i32, %struct.TYPE_2__, %struct.TYPE_2__, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@RTF_UP = global i32 0
@RTF_HOST = global i32 0
@SIOCADDRT = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @add_host_route(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.rtentry
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = call i32 @memset(%struct.rtentry* %5, i32 0, i32 24)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %5, i32 0, i32 3
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = load i32, i32* @RTF_UP
%16 = load i32, i32* @RTF_HOST
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %5, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i8*, i8** %3
%20 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %5, i32 0, i32 4
store i8* %19, i8** %20
%21 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %5, i32 0, i32 3
%22 = load i32, i32* %4
%23 = call i32 @init_sockaddr_in(%struct.TYPE_2__* %21, i32 %22)
%24 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %5, i32 0, i32 2
%25 = call i32 @init_sockaddr_in(%struct.TYPE_2__* %24, i32 0)
%26 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %5, i32 0, i32 1
%27 = call i32 @init_sockaddr_in(%struct.TYPE_2__* %26, i32 0)
%28 = load i32, i32* %7
%29 = load i32, i32* @SIOCADDRT
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.rtentry* %5)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %2
%34 = load i64, i64* @errno
%35 = load i64, i64* @EEXIST
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 0, i32* %6
br label %38
38: ; preds = %37, %33, %2
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %6
ret i32 %41
}

LLVM IR (-O2):
%struct.rtentry = type { i32, %struct.TYPE_2__, %struct.TYPE_2__, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@RTF_UP = global i32 0
@RTF_HOST = global i32 0
@SIOCADDRT = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @add_host_route(i8* %0, i32 %1) {
%3 = alloca %struct.rtentry
%4 = bitcast %struct.rtentry* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = call i32 @memset(%struct.rtentry* %3, i32 0, i32 24)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 3
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i64 0, i32 0
store i32 %9, i32* %11
%12 = load i32, i32* @RTF_UP
%13 = load i32, i32* @RTF_HOST
%14 = or i32 %13, %12
%15 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 4
store i8* %0, i8** %16
%17 = call i32 @init_sockaddr_in(%struct.TYPE_2__* %10, i32 %1)
%18 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 2
%19 = call i32 @init_sockaddr_in(%struct.TYPE_2__* %18, i32 0)
%20 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 1
%21 = call i32 @init_sockaddr_in(%struct.TYPE_2__* %20, i32 0)
%22 = load i32, i32* @SIOCADDRT
%23 = call i32 @ioctl(i32 %7, i32 %22, %struct.rtentry* %3)
%24 = icmp slt i32 %23, 0
%25 = load i64, i64* @errno
%26 = load i64, i64* @EEXIST
%27 = icmp eq i64 %25, %26
%28 = select i1 %24, i1 %27, i1 false
%29 = select i1 %28, i32 0, i32 %23
%30 = call i32 @close(i32 %7)
ret i32 %29
}