#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 sockaddr_nl {
  int dummy;
};
struct rtmsg {
  int rtm_family;
  int /*<<< orphan*/ rtm_type;
  int /*<<< orphan*/ rtm_scope;
  int /*<<< orphan*/ rtm_protocol;
  int /*<<< orphan*/ rtm_table;
};
struct rtattr {
  int rta_len;
  int /*<<< orphan*/ rta_type;
};
struct nlmsghdr {
  int nlmsg_len;
  int nlmsg_flags;
  int /*<<< orphan*/ nlmsg_type;
};
struct msghdr {
  int member_1;
  int member_3;
  int /*<<< orphan*/ member_6;
  int /*<<< orphan*/ member_5;
  int /*<<< orphan*/ *member_4;
  struct iovec *member_2;
  struct sockaddr_nl *member_0;
};
struct iovec {
  int member_1;
  struct nlmsghdr *member_0;
};

/* Variables and functions */
int NLMSG_ALIGN(int);
scalar_t__ NLMSG_DATA(struct nlmsghdr *);
int NLMSG_LENGTH(int);
int NLM_F_ACK;
int NLM_F_CREATE;
int NLM_F_REPLACE;
int NLM_F_REQUEST;
scalar_t__ RTA_DATA(struct rtattr *);
int /*<<< orphan*/ RTA_GATEWAY;
void *RTA_LENGTH(int);
scalar_t__ RTA_NEXT(struct rtattr *, int);
int /*<<< orphan*/ RTA_OIF;
int /*<<< orphan*/ RTM_NEWROUTE;
scalar_t__ RTM_RTA(struct rtmsg *);
int /*<<< orphan*/ RTN_UNICAST;
int /*<<< orphan*/ RTPROT_STATIC;
int /*<<< orphan*/ RT_SCOPE_UNIVERSE;
int /*<<< orphan*/ RT_TABLE_MAIN;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int pton(int, scalar_t__, char const *);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int add_gateway(int fd, struct sockaddr_nl *sa, int domain, const char *ip) {
  char buf[4096];
  int ip_len = 0;
  memset(buf, 0, 4096);
  struct nlmsghdr *nl;
  nl = (struct nlmsghdr *)buf;
  nl->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  nl->nlmsg_type = RTM_NEWROUTE;
  nl->nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_ACK | NLM_F_CREATE;
  struct rtmsg *rt;
  rt = (struct rtmsg *)NLMSG_DATA(nl);
  rt->rtm_family = domain;
  rt->rtm_table = RT_TABLE_MAIN;
  rt->rtm_protocol = RTPROT_STATIC;
  rt->rtm_scope = RT_SCOPE_UNIVERSE;
  rt->rtm_type = RTN_UNICAST;
  struct rtattr *rta = (struct rtattr *)RTM_RTA(rt);
  rta->rta_type = RTA_GATEWAY;
  ip_len = pton(domain, RTA_DATA(rta), ip);
  rta->rta_len = RTA_LENGTH(ip_len);
  nl->nlmsg_len = NLMSG_ALIGN(nl->nlmsg_len) + rta->rta_len;
  int l = 4096 - nl->nlmsg_len;
  rta = (struct rtattr *)RTA_NEXT(rta, l);
  rta->rta_type = RTA_OIF;
  rta->rta_len = RTA_LENGTH(sizeof(int));
  *((int *)RTA_DATA(rta)) = 2;
  nl->nlmsg_len += rta->rta_len;
  struct iovec iov = {nl, nl->nlmsg_len};
  struct msghdr msg = {sa, sizeof(*sa), &iov, 1, NULL, 0, 0};
  int r = sendmsg(fd, &msg, 0);
  return (r < 0) ? -1 : 0;
}


x86 assembly (clang, -O0):
<add_gateway>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1090,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x1024(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    $0x14,%edi
call   1060 <NLMSG_LENGTH@plt>
mov    %eax,%ecx
mov    -0x1030(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2d90(%rip),%rax
mov    (%rax),%ecx
mov    -0x1030(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dad(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcc(%rip),%rax
or     (%rax),%ecx
mov    0x2d93(%rip),%rax
or     (%rax),%ecx
mov    0x2da2(%rip),%rax
or     (%rax),%ecx
mov    -0x1030(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x1030(%rbp),%rdi
call   1090 <NLMSG_DATA@plt>
mov    %rax,-0x1038(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x1038(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2d40(%rip),%rax
mov    (%rax),%ecx
mov    -0x1038(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    0x2d55(%rip),%rax
mov    (%rax),%ecx
mov    -0x1038(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    0x2d22(%rip),%rax
mov    (%rax),%ecx
mov    -0x1038(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2d3f(%rip),%rax
mov    (%rax),%ecx
mov    -0x1038(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x1038(%rbp),%rdi
call   1040 <RTM_RTA@plt>
mov    %rax,-0x1040(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%ecx
mov    -0x1040(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1088(%rbp)
mov    -0x1040(%rbp),%rdi
call   10a0 <RTA_DATA@plt>
mov    -0x1088(%rbp),%edi
mov    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   10c0 <pton@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x1024(%rbp),%edi
call   10b0 <RTA_LENGTH@plt>
mov    %eax,%ecx
mov    -0x1040(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1030(%rbp),%rax
mov    (%rax),%edi
call   1070 <NLMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x1040(%rbp),%rax
add    (%rax),%ecx
mov    -0x1030(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1030(%rbp),%rcx
mov    $0x1000,%eax
sub    (%rcx),%eax
mov    %eax,-0x1044(%rbp)
mov    -0x1040(%rbp),%rdi
mov    -0x1044(%rbp),%esi
call   1030 <RTA_NEXT@plt>
mov    %rax,-0x1040(%rbp)
mov    0x2c24(%rip),%rax
mov    (%rax),%ecx
mov    -0x1040(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   10b0 <RTA_LENGTH@plt>
mov    %eax,%ecx
mov    -0x1040(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1040(%rbp),%rdi
call   10a0 <RTA_DATA@plt>
movl   $0x2,(%rax)
mov    -0x1040(%rbp),%rax
mov    (%rax),%ecx
mov    -0x1030(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x1030(%rbp),%rax
mov    %eax,-0x1058(%rbp)
mov    -0x1030(%rbp),%rax
movslq (%rax),%rax
mov    %rax,-0x1050(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x1080(%rbp)
movl   $0x4,-0x107c(%rbp)
lea    -0x1058(%rbp),%rax
mov    %eax,-0x1078(%rbp)
movl   $0x1,-0x1074(%rbp)
movq   $0x0,-0x1070(%rbp)
movq   $0x0,-0x1068(%rbp)
movq   $0x0,-0x1060(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1080(%rbp),%rsi
xor    %edx,%edx
call   1050 <sendmsg@plt>
mov    %eax,-0x1084(%rbp)
mov    -0x1084(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
add    $0x1090,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_gateway>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1048,%rsp
mov    %rcx,%r13
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,0x4(%rsp)
lea    0x40(%rsp),%r12
mov    $0x1000,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    $0x14,%edi
call   1060 <NLMSG_LENGTH@plt>
mov    %eax,0x40(%rsp)
mov    0x2dd5(%rip),%rax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x48(%rsp)
mov    0x2dae(%rip),%rax
or     (%rax),%ecx
mov    0x2dbd(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x44(%rsp)
mov    %r12,%rdi
call   1090 <NLMSG_DATA@plt>
mov    %ebp,(%rax)
mov    0x2d76(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x10(%rax)
mov    0x2d92(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0xc(%rax)
mov    0x2d66(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%rax)
mov    0x2d8a(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rax)
mov    %rax,%rdi
call   1040 <RTM_RTA@plt>
mov    %rax,%r14
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r14)
mov    %r14,%rdi
call   10a0 <RTA_DATA@plt>
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r13,%rdx
call   10c0 <pton@plt>
mov    %eax,%edi
call   10b0 <RTA_LENGTH@plt>
mov    %eax,(%r14)
mov    0x40(%rsp),%edi
call   1070 <NLMSG_ALIGN@plt>
add    (%r14),%eax
mov    %eax,0x40(%rsp)
sub    %eax,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <RTA_NEXT@plt>
mov    %rax,%rbx
mov    0x2cd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   10b0 <RTA_LENGTH@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   10a0 <RTA_DATA@plt>
movl   $0x2,(%rax)
movslq (%rbx),%rax
movslq 0x40(%rsp),%rcx
add    %rax,%rcx
mov    %ecx,0x40(%rsp)
mov    %r12d,0x8(%rsp)
mov    %rcx,0x10(%rsp)
mov    %r15d,0x18(%rsp)
movl   $0x4,0x1c(%rsp)
lea    0x8(%rsp),%rax
mov    %eax,0x20(%rsp)
movl   $0x1,0x24(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rsp)
movq   $0x0,0x38(%rsp)
lea    0x18(%rsp),%rsi
mov    0x4(%rsp),%edi
xor    %edx,%edx
call   1050 <sendmsg@plt>
sar    $0x1f,%eax
add    $0x1048,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.nlmsghdr = type { i32, i32, i32 }
%struct.rtmsg = type { i32, i32, i32, i32, i32 }
%struct.rtattr = type { i32, i32 }
%struct.iovec = type { i32, %struct.nlmsghdr* }
%struct.msghdr = type { i32, i32, i32, i32, i32*, %struct.iovec*, %struct.sockaddr_nl* }
@RTM_NEWROUTE = global i32 0
@NLM_F_REQUEST = global i32 0
@NLM_F_REPLACE = global i32 0
@NLM_F_ACK = global i32 0
@NLM_F_CREATE = global i32 0
@RT_TABLE_MAIN = global i32 0
@RTPROT_STATIC = global i32 0
@RT_SCOPE_UNIVERSE = global i32 0
@RTN_UNICAST = global i32 0
@RTA_GATEWAY = global i32 0
@RTA_OIF = global i32 0
define i32 @add_gateway(i32 %0, %struct.sockaddr_nl* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_nl*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca %struct.nlmsghdr*
%12 = alloca %struct.rtmsg*
%13 = alloca %struct.rtattr*
%14 = alloca i32
%15 = alloca %struct.iovec
%16 = alloca %struct.msghdr
%17 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr_nl* %1, %struct.sockaddr_nl** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
store i32 0, i32* %10
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 0, i32 4096)
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%21 = bitcast i8* %20 to %struct.nlmsghdr*
store %struct.nlmsghdr* %21, %struct.nlmsghdr** %11
%22 = call i32 @NLMSG_LENGTH(i32 20)
%23 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%24 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* @RTM_NEWROUTE
%26 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%27 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %26, i32 0, i32 2
store i32 %25, i32* %27
%28 = load i32, i32* @NLM_F_REQUEST
%29 = load i32, i32* @NLM_F_REPLACE
%30 = or i32 %28, %29
%31 = load i32, i32* @NLM_F_ACK
%32 = or i32 %30, %31
%33 = load i32, i32* @NLM_F_CREATE
%34 = or i32 %32, %33
%35 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%36 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %35, i32 0, i32 1
store i32 %34, i32* %36
%37 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%38 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %37)
%39 = inttoptr i64 %38 to %struct.rtmsg*
store %struct.rtmsg* %39, %struct.rtmsg** %12
%40 = load i32, i32* %7
%41 = load %struct.rtmsg*, %struct.rtmsg** %12
%42 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %41, i32 0, i32 0
store i32 %40, i32* %42
%43 = load i32, i32* @RT_TABLE_MAIN
%44 = load %struct.rtmsg*, %struct.rtmsg** %12
%45 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %44, i32 0, i32 4
store i32 %43, i32* %45
%46 = load i32, i32* @RTPROT_STATIC
%47 = load %struct.rtmsg*, %struct.rtmsg** %12
%48 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %47, i32 0, i32 3
store i32 %46, i32* %48
%49 = load i32, i32* @RT_SCOPE_UNIVERSE
%50 = load %struct.rtmsg*, %struct.rtmsg** %12
%51 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %50, i32 0, i32 2
store i32 %49, i32* %51
%52 = load i32, i32* @RTN_UNICAST
%53 = load %struct.rtmsg*, %struct.rtmsg** %12
%54 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %53, i32 0, i32 1
store i32 %52, i32* %54
%55 = load %struct.rtmsg*, %struct.rtmsg** %12
%56 = call i64 @RTM_RTA(%struct.rtmsg* %55)
%57 = inttoptr i64 %56 to %struct.rtattr*
store %struct.rtattr* %57, %struct.rtattr** %13
%58 = load i32, i32* @RTA_GATEWAY
%59 = load %struct.rtattr*, %struct.rtattr** %13
%60 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %59, i32 0, i32 1
store i32 %58, i32* %60
%61 = load i32, i32* %7
%62 = load %struct.rtattr*, %struct.rtattr** %13
%63 = call i64 @RTA_DATA(%struct.rtattr* %62)
%64 = load i8*, i8** %8
%65 = call i32 @pton(i32 %61, i64 %63, i8* %64)
store i32 %65, i32* %10
%66 = load i32, i32* %10
%67 = call i8* @RTA_LENGTH(i32 %66)
%68 = ptrtoint i8* %67 to i32
%69 = load %struct.rtattr*, %struct.rtattr** %13
%70 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %69, i32 0, i32 0
store i32 %68, i32* %70
%71 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%72 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 @NLMSG_ALIGN(i32 %73)
%75 = load %struct.rtattr*, %struct.rtattr** %13
%76 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %75, i32 0, i32 0
%77 = load i32, i32* %76
%78 = add nsw i32 %74, %77
%79 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%80 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %79, i32 0, i32 0
store i32 %78, i32* %80
%81 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%82 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = sub nsw i32 4096, %83
store i32 %84, i32* %14
%85 = load %struct.rtattr*, %struct.rtattr** %13
%86 = load i32, i32* %14
%87 = call i64 @RTA_NEXT(%struct.rtattr* %85, i32 %86)
%88 = inttoptr i64 %87 to %struct.rtattr*
store %struct.rtattr* %88, %struct.rtattr** %13
%89 = load i32, i32* @RTA_OIF
%90 = load %struct.rtattr*, %struct.rtattr** %13
%91 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %90, i32 0, i32 1
store i32 %89, i32* %91
%92 = call i8* @RTA_LENGTH(i32 4)
%93 = ptrtoint i8* %92 to i32
%94 = load %struct.rtattr*, %struct.rtattr** %13
%95 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %94, i32 0, i32 0
store i32 %93, i32* %95
%96 = load %struct.rtattr*, %struct.rtattr** %13
%97 = call i64 @RTA_DATA(%struct.rtattr* %96)
%98 = inttoptr i64 %97 to i32*
store i32 2, i32* %98
%99 = load %struct.rtattr*, %struct.rtattr** %13
%100 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %99, i32 0, i32 0
%101 = load i32, i32* %100
%102 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%103 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %102, i32 0, i32 0
%104 = load i32, i32* %103
%105 = add nsw i32 %104, %101
store i32 %105, i32* %103
%106 = getelementptr inbounds %struct.iovec, %struct.iovec* %15, i32 0, i32 0
%107 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%108 = ptrtoint %struct.nlmsghdr* %107 to i32
store i32 %108, i32* %106
%109 = getelementptr inbounds %struct.iovec, %struct.iovec* %15, i32 0, i32 1
%110 = load %struct.nlmsghdr*, %struct.nlmsghdr** %11
%111 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %110, i32 0, i32 0
%112 = load i32, i32* %111
%113 = sext i32 %112 to i64
%114 = inttoptr i64 %113 to %struct.nlmsghdr*
store %struct.nlmsghdr* %114, %struct.nlmsghdr** %109
%115 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 0
%116 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %6
%117 = ptrtoint %struct.sockaddr_nl* %116 to i32
store i32 %117, i32* %115
%118 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 1
store i32 4, i32* %118
%119 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 2
%120 = ptrtoint %struct.iovec* %15 to i32
store i32 %120, i32* %119
%121 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 3
store i32 1, i32* %121
%122 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 4
store i32* null, i32** %122
%123 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 5
store %struct.iovec* null, %struct.iovec** %123
%124 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 6
store %struct.sockaddr_nl* null, %struct.sockaddr_nl** %124
%125 = load i32, i32* %5
%126 = call i32 @sendmsg(i32 %125, %struct.msghdr* %16, i32 0)
store i32 %126, i32* %17
%127 = load i32, i32* %17
%128 = icmp slt i32 %127, 0
%129 = zext i1 %128 to i64
%130 = select i1 %128, i32 -1, i32 0
ret i32 %130
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.iovec = type { i32, %struct.nlmsghdr* }
%struct.nlmsghdr = type { i32, i32, i32 }
%struct.msghdr = type { i32, i32, i32, i32, i32*, %struct.iovec*, %struct.sockaddr_nl* }
%struct.rtmsg = type { i32, i32, i32, i32, i32 }
%struct.rtattr = type { i32, i32 }
@RTM_NEWROUTE = global i32 0
@NLM_F_REQUEST = global i32 0
@NLM_F_REPLACE = global i32 0
@NLM_F_ACK = global i32 0
@NLM_F_CREATE = global i32 0
@RT_TABLE_MAIN = global i32 0
@RTPROT_STATIC = global i32 0
@RT_SCOPE_UNIVERSE = global i32 0
@RTN_UNICAST = global i32 0
@RTA_GATEWAY = global i32 0
@RTA_OIF = global i32 0
define i32 @add_gateway(i32 %0, %struct.sockaddr_nl* %1, i32 %2, i8* %3) {
%5 = alloca [4096 x i8]
%6 = alloca %struct.iovec
%7 = alloca %struct.msghdr
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 4096)
%10 = bitcast [4096 x i8]* %5 to %struct.nlmsghdr*
%11 = call i32 @NLMSG_LENGTH(i32 20)
%12 = bitcast [4096 x i8]* %5 to i32*
store i32 %11, i32* %12
%13 = load i32, i32* @RTM_NEWROUTE
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 8
%15 = bitcast i8* %14 to i32*
store i32 %13, i32* %15
%16 = load i32, i32* @NLM_F_REQUEST
%17 = load i32, i32* @NLM_F_REPLACE
%18 = or i32 %17, %16
%19 = load i32, i32* @NLM_F_ACK
%20 = or i32 %18, %19
%21 = load i32, i32* @NLM_F_CREATE
%22 = or i32 %20, %21
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 4
%24 = bitcast i8* %23 to i32*
store i32 %22, i32* %24
%25 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %10)
%26 = inttoptr i64 %25 to %struct.rtmsg*
%27 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %26, i64 0, i32 0
store i32 %2, i32* %27
%28 = load i32, i32* @RT_TABLE_MAIN
%29 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %26, i64 0, i32 4
store i32 %28, i32* %29
%30 = load i32, i32* @RTPROT_STATIC
%31 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %26, i64 0, i32 3
store i32 %30, i32* %31
%32 = load i32, i32* @RT_SCOPE_UNIVERSE
%33 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %26, i64 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* @RTN_UNICAST
%35 = getelementptr inbounds %struct.rtmsg, %struct.rtmsg* %26, i64 0, i32 1
store i32 %34, i32* %35
%36 = call i64 @RTM_RTA(%struct.rtmsg* %26)
%37 = inttoptr i64 %36 to %struct.rtattr*
%38 = load i32, i32* @RTA_GATEWAY
%39 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %37, i64 0, i32 1
store i32 %38, i32* %39
%40 = call i64 @RTA_DATA(%struct.rtattr* %37)
%41 = call i32 @pton(i32 %2, i64 %40, i8* %3)
%42 = call i8* @RTA_LENGTH(i32 %41)
%43 = ptrtoint i8* %42 to i64
%44 = trunc i64 %43 to i32
%45 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %37, i64 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %12
%47 = call i32 @NLMSG_ALIGN(i32 %46)
%48 = load i32, i32* %45
%49 = add nsw i32 %48, %47
store i32 %49, i32* %12
%50 = sub nsw i32 4096, %49
%51 = call i64 @RTA_NEXT(%struct.rtattr* %37, i32 %50)
%52 = inttoptr i64 %51 to %struct.rtattr*
%53 = load i32, i32* @RTA_OIF
%54 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %52, i64 0, i32 1
store i32 %53, i32* %54
%55 = call i8* @RTA_LENGTH(i32 4)
%56 = ptrtoint i8* %55 to i64
%57 = trunc i64 %56 to i32
%58 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %52, i64 0, i32 0
store i32 %57, i32* %58
%59 = call i64 @RTA_DATA(%struct.rtattr* %52)
%60 = inttoptr i64 %59 to i32*
store i32 2, i32* %60
%61 = load i32, i32* %58
%62 = load i32, i32* %12
%63 = add nsw i32 %62, %61
store i32 %63, i32* %12
%64 = bitcast %struct.iovec* %6 to i8*
%65 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 0
%66 = ptrtoint [4096 x i8]* %5 to i64
%67 = trunc i64 %66 to i32
store i32 %67, i32* %65
%68 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 1
%69 = sext i32 %63 to i64
%70 = inttoptr i64 %69 to %struct.nlmsghdr*
store %struct.nlmsghdr* %70, %struct.nlmsghdr** %68
%71 = bitcast %struct.msghdr* %7 to i8*
%72 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 0
%73 = ptrtoint %struct.sockaddr_nl* %1 to i64
%74 = trunc i64 %73 to i32
store i32 %74, i32* %72
%75 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 1
store i32 4, i32* %75
%76 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 2
%77 = ptrtoint %struct.iovec* %6 to i64
%78 = trunc i64 %77 to i32
store i32 %78, i32* %76
%79 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 3
store i32 1, i32* %79
%80 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 4
%81 = bitcast i32** %80 to i8*
%82 = call i32 @sendmsg(i32 %0, %struct.msghdr* %7, i32 0)
%83 = ashr i32 %82, 31
ret i32 %83
}