#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*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct rtentry {
  char *rt_dev;
  scalar_t__ rt_flags;
  scalar_t__ rt_dst;
  scalar_t__ rt_genmask;
  scalar_t__ rt_gateway;
};
typedef int /*<<< orphan*/ rt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCADDRT;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct rtentry *);
int /*<<< orphan*/ memset(struct rtentry *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void add_default_route(const char *devname) {
  struct rtentry rt;
  struct sockaddr_in *sin;
  int fd;
  fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (fd < 0)
    err(1, "opening IP socket");
  memset(&rt, 0, sizeof(rt));
  sin = (struct sockaddr_in *)&rt.rt_dst;
  sin->sin_family = AF_INET;
  sin->sin_addr.s_addr = INADDR_ANY;
  rt.rt_gateway = rt.rt_genmask = rt.rt_dst;
  rt.rt_dev = (char *)devname;
  rt.rt_flags = 0;
  if (ioctl(fd, SIOCADDRT, &rt) != 0)
    err(1, "adding route");
  close(fd);
}


x86 assembly (clang, -O0):
<add_default_route>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    119a <add_default_route+0x4a>
mov    $0x1,%edi
lea    0xe6b(%rip),%rsi
call   1060 <err@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
lea    -0x30(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x38(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2e0b(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1221 <add_default_route+0xd1>
mov    $0x1,%edi
lea    0xdf6(%rip),%rsi
call   1060 <err@plt>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_default_route>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%r14
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1193 <add_default_route+0x43>
lea    0xe77(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x18(%rsp),%rax
mov    %rax,0x20(%rsp)
mov    %rax,0x28(%rsp)
mov    %r14,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
je     1207 <add_default_route+0xb7>
lea    0xe15(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.rtentry = type { i8*, i64, i64, i64, i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [18 x i8] c"opening IP socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SIOCADDRT = global i32 0
@.str.1 = [13 x i8] c"adding route\00"
define void @add_default_route(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.rtentry
%4 = alloca %struct.sockaddr_in*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @IPPROTO_IP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = call i32 @memset(%struct.rtentry* %3, i32 0, i32 40)
%16 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i32 0, i32 2
%17 = bitcast i64* %16 to %struct.sockaddr_in*
store %struct.sockaddr_in* %17, %struct.sockaddr_in** %4
%18 = load i32, i32* @AF_INET
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 1
store i32 %18, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %21, i32* %24
%25 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i32 0, i32 2
%26 = load i64, i64* %25
%27 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i32 0, i32 3
store i64 %26, i64* %27
%28 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i32 0, i32 4
store i64 %26, i64* %28
%29 = load i8*, i8** %2
%30 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i32 0, i32 0
store i8* %29, i8** %30
%31 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i32 0, i32 1
store i64 0, i64* %31
%32 = load i32, i32* %5
%33 = load i32, i32* @SIOCADDRT
%34 = call i64 @ioctl(i32 %32, i32 %33, %struct.rtentry* %3)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %14
%37 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %14
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
ret void
}

LLVM IR (-O2):
%struct.rtentry = type { i8*, i64, i64, i64, i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [18 x i8] c"opening IP socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SIOCADDRT = global i32 0
@.str.1 = [13 x i8] c"adding route\00"
define void @add_default_route(i8* %0) {
%2 = alloca %struct.rtentry
%3 = bitcast %struct.rtentry* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @IPPROTO_IP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = call i32 @memset(%struct.rtentry* %2, i32 0, i32 40)
%13 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %2, i64 0, i32 2
%14 = bitcast i64* %13 to %struct.sockaddr_in*
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = bitcast i64* %13 to i32*
store i32 %17, i32* %18
%19 = load i64, i64* %13
%20 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %2, i64 0, i32 3
store i64 %19, i64* %20
%21 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %2, i64 0, i32 4
store i64 %19, i64* %21
%22 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %2, i64 0, i32 0
store i8* %0, i8** %22
%23 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %2, i64 0, i32 1
store i64 0, i64* %23
%24 = load i32, i32* @SIOCADDRT
%25 = call i64 @ioctl(i32 %7, i32 %24, %struct.rtentry* %2)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %11
%28 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %11
%30 = call i32 @close(i32 %7)
ret void
}