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

/* Type definitions */
struct TYPE_8__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_4__ sin_addr;
};
struct TYPE_7__ {
  void *sa_family;
};
struct TYPE_6__ {
  void *sa_family;
};
struct TYPE_5__ {
  void *sa_family;
};
struct rtentry {
  int rt_metric;
  char *rt_dev;
  TYPE_3__ rt_genmask;
  TYPE_2__ rt_gateway;
  TYPE_1__ rt_dst;
  int /*<<< orphan*/ rt_flags;
};
typedef int /*<<< orphan*/ rt;

/* Variables and functions */
void *AF_INET;
scalar_t__ INADDR_BROADCAST;
int /*<<< orphan*/ RTF_GATEWAY;
int /*<<< orphan*/ RTF_HOST;
int /*<<< orphan*/ RTF_UP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ inet_aton(char *, TYPE_4__ *);
scalar_t__ ioctl(int, int, struct rtentry *);
int /*<<< orphan*/ memset(struct rtentry *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int route_manip(int cmd, char *name, int metric, char *dst, char *gateway,
                char *genmask) {
  int s;
  struct rtentry rt;
  if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    goto err;
  memset(&rt, 0, sizeof(rt));
  if (dst)
    inet_aton(dst, &(((struct sockaddr_in *)(&rt.rt_dst))->sin_addr));
  if (gateway)
    inet_aton(gateway, &(((struct sockaddr_in *)(&rt.rt_gateway))->sin_addr));
  if (genmask)
    inet_aton(genmask, &(((struct sockaddr_in *)(&rt.rt_genmask))->sin_addr));
  rt.rt_metric = metric;
  rt.rt_flags = RTF_UP;
  if ((((struct sockaddr_in *)(&rt.rt_gateway))->sin_addr).s_addr)
    rt.rt_flags |= RTF_GATEWAY;
  if ((((struct sockaddr_in *)(&rt.rt_genmask))->sin_addr).s_addr ==
      INADDR_BROADCAST)
    rt.rt_flags |= RTF_HOST;
  rt.rt_dev = name;
  rt.rt_dst.sa_family = AF_INET;
  rt.rt_gateway.sa_family = AF_INET;
  rt.rt_genmask.sa_family = AF_INET;
  if (ioctl(s, cmd, &rt) < 0)
    goto err;
  close(s);
  return 0;
err:
  close(s);
  perror(name);
  return errno;
}


x86 assembly (clang, -O0):
<route_manip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11a9 <route_manip+0x49>
jmp    12c0 <route_manip+0x160>
lea    -0x68(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
je     11d5 <route_manip+0x75>
mov    -0x20(%rbp),%rdi
lea    -0x68(%rbp),%rsi
add    $0x20,%rsi
call   1060 <inet_aton@plt>
cmpq   $0x0,-0x28(%rbp)
je     11f1 <route_manip+0x91>
mov    -0x28(%rbp),%rdi
lea    -0x68(%rbp),%rsi
add    $0x18,%rsi
call   1060 <inet_aton@plt>
cmpq   $0x0,-0x30(%rbp)
je     120d <route_manip+0xad>
mov    -0x30(%rbp),%rdi
lea    -0x68(%rbp),%rsi
add    $0x10,%rsi
call   1060 <inet_aton@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1239 <route_manip+0xd9>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
or     -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x58(%rbp),%rax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
jne    125c <route_manip+0xfc>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
or     -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d61(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x68(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    12ac <route_manip+0x14c>
jmp    12c0 <route_manip+0x160>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12dd <route_manip+0x17d>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<route_manip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,%r13
mov    %r8,%rbx
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,%r15
mov    %edi,0x4(%rsp)
mov    0x2e60(%rip),%rax
mov    (%rax),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     127c <route_manip+0x11c>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
test   %r14,%r14
je     11c8 <route_manip+0x68>
lea    0x28(%rsp),%rsi
mov    %r14,%rdi
call   1060 <inet_aton@plt>
test   %rbx,%rbx
je     11da <route_manip+0x7a>
lea    0x20(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <inet_aton@plt>
test   %r13,%r13
je     11ec <route_manip+0x8c>
lea    0x18(%rsp),%rsi
mov    %r13,%rdi
call   1060 <inet_aton@plt>
mov    %r12d,0x8(%rsp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
cmpq   $0x0,0x20(%rsp)
je     1213 <route_manip+0xb3>
mov    0x2db3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x30(%rsp)
mov    0x18(%rsp),%rcx
mov    0x2dc1(%rip),%rdx
cmp    (%rdx),%rcx
jne    1231 <route_manip+0xd1>
mov    0x2d9d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x30(%rsp)
mov    %r15,0x10(%rsp)
mov    0x2dab(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x28(%rsp)
mov    %rax,0x20(%rsp)
mov    %rax,0x18(%rsp)
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    0x4(%rsp),%esi
call   1040 <ioctl@plt>
test   %rax,%rax
js     127c <route_manip+0x11c>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15,%rdi
call   1070 <perror@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
jmp    126d <route_manip+0x10d>

LLVM IR (-O0):
%struct.rtentry = type { i32, i8*, %struct.TYPE_7__, %struct.TYPE_6__, %struct.TYPE_5__, i32 }
%struct.TYPE_7__ = type { i8* }
%struct.TYPE_6__ = type { i8* }
%struct.TYPE_5__ = type { i8* }
%struct.sockaddr_in = type { %struct.TYPE_8__ }
%struct.TYPE_8__ = type { i64 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@RTF_UP = global i32 0
@RTF_GATEWAY = global i32 0
@INADDR_BROADCAST = global i64 0
@RTF_HOST = global i32 0
@errno = global i32 0
define i32 @route_manip(i32 %0, i8* %1, i32 %2, i8* %3, i8* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca %struct.rtentry
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i8* %3, i8** %11
store i8* %4, i8** %12
store i8* %5, i8** %13
%16 = load i8*, i8** @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = call i32 @socket(i8* %16, i32 %17, i32 0)
store i32 %18, i32* %14
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %6
br label %98
21: ; preds = %6
%22 = call i32 @memset(%struct.rtentry* %15, i32 0, i32 48)
%23 = load i8*, i8** %11
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i8*, i8** %11
%27 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 4
%28 = bitcast %struct.TYPE_5__* %27 to %struct.sockaddr_in*
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 0
%30 = call i32 @inet_aton(i8* %26, %struct.TYPE_8__* %29)
br label %31
31: ; preds = %25, %21
%32 = load i8*, i8** %12
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i8*, i8** %12
%36 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 3
%37 = bitcast %struct.TYPE_6__* %36 to %struct.sockaddr_in*
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %37, i32 0, i32 0
%39 = call i32 @inet_aton(i8* %35, %struct.TYPE_8__* %38)
br label %40
40: ; preds = %34, %31
%41 = load i8*, i8** %13
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load i8*, i8** %13
%45 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 2
%46 = bitcast %struct.TYPE_7__* %45 to %struct.sockaddr_in*
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %46, i32 0, i32 0
%48 = call i32 @inet_aton(i8* %44, %struct.TYPE_8__* %47)
br label %49
49: ; preds = %43, %40
%50 = load i32, i32* %10
%51 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* @RTF_UP
%53 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 5
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 3
%55 = bitcast %struct.TYPE_6__* %54 to %struct.sockaddr_in*
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %55, i32 0, i32 0
%57 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %56, i32 0, i32 0
%58 = load i64, i64* %57
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %49
%61 = load i32, i32* @RTF_GATEWAY
%62 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 5
%63 = load i32, i32* %62
%64 = or i32 %63, %61
store i32 %64, i32* %62
br label %65
65: ; preds = %60, %49
%66 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 2
%67 = bitcast %struct.TYPE_7__* %66 to %struct.sockaddr_in*
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %67, i32 0, i32 0
%69 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %68, i32 0, i32 0
%70 = load i64, i64* %69
%71 = load i64, i64* @INADDR_BROADCAST
%72 = icmp eq i64 %70, %71
br i1 %72, label %73, label %78
73: ; preds = %65
%74 = load i32, i32* @RTF_HOST
%75 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 5
%76 = load i32, i32* %75
%77 = or i32 %76, %74
store i32 %77, i32* %75
br label %78
78: ; preds = %73, %65
%79 = load i8*, i8** %9
%80 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 1
store i8* %79, i8** %80
%81 = load i8*, i8** @AF_INET
%82 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 4
%83 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %82, i32 0, i32 0
store i8* %81, i8** %83
%84 = load i8*, i8** @AF_INET
%85 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 3
%86 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %85, i32 0, i32 0
store i8* %84, i8** %86
%87 = load i8*, i8** @AF_INET
%88 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %15, i32 0, i32 2
%89 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %88, i32 0, i32 0
store i8* %87, i8** %89
%90 = load i32, i32* %14
%91 = load i32, i32* %8
%92 = call i64 @ioctl(i32 %90, i32 %91, %struct.rtentry* %15)
%93 = icmp slt i64 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %78
br label %98
95: ; preds = %78
%96 = load i32, i32* %14
%97 = call i32 @close(i32 %96)
store i32 0, i32* %7
br label %104
98: ; preds = %94, %20
%99 = load i32, i32* %14
%100 = call i32 @close(i32 %99)
%101 = load i8*, i8** %9
%102 = call i32 @perror(i8* %101)
%103 = load i32, i32* @errno
store i32 %103, i32* %7
br label %104
104: ; preds = %98, %95
%105 = load i32, i32* %7
ret i32 %105
}

LLVM IR (-O2):
%struct.rtentry = type { i32, i8*, %struct.TYPE_7__, %struct.TYPE_6__, %struct.TYPE_5__, i32 }
%struct.TYPE_7__ = type { i8* }
%struct.TYPE_6__ = type { i8* }
%struct.TYPE_5__ = type { i8* }
%struct.TYPE_8__ = type { i64 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@RTF_UP = global i32 0
@RTF_GATEWAY = global i32 0
@INADDR_BROADCAST = global i64 0
@RTF_HOST = global i32 0
@errno = global i32 0
define i32 @route_manip(i32 %0, i8* %1, i32 %2, i8* %3, i8* %4, i8* %5) {
%7 = alloca %struct.rtentry
%8 = bitcast %struct.rtentry* %7 to i8*
%9 = load i8*, i8** @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i8* %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %63, label %13
13: ; preds = %6
%14 = call i32 @memset(%struct.rtentry* %7, i32 0, i32 48)
%15 = icmp eq i8* %3, null
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 4
%18 = bitcast %struct.TYPE_5__* %17 to %struct.TYPE_8__*
%19 = call i32 @inet_aton(i8* %3, %struct.TYPE_8__* %18)
br label %20
20: ; preds = %16, %13
%21 = icmp eq i8* %4, null
br i1 %21, label %26, label %22
22: ; preds = %20
%23 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 3
%24 = bitcast %struct.TYPE_6__* %23 to %struct.TYPE_8__*
%25 = call i32 @inet_aton(i8* %4, %struct.TYPE_8__* %24)
br label %26
26: ; preds = %22, %20
%27 = icmp eq i8* %5, null
br i1 %27, label %32, label %28
28: ; preds = %26
%29 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 2
%30 = bitcast %struct.TYPE_7__* %29 to %struct.TYPE_8__*
%31 = call i32 @inet_aton(i8* %5, %struct.TYPE_8__* %30)
br label %32
32: ; preds = %28, %26
%33 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 0
store i32 %2, i32* %33
%34 = load i32, i32* @RTF_UP
%35 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 5
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 3
%37 = bitcast %struct.TYPE_6__* %36 to i64*
%38 = load i64, i64* %37
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %32
%41 = load i32, i32* @RTF_GATEWAY
%42 = or i32 %41, %34
store i32 %42, i32* %35
br label %43
43: ; preds = %40, %32
%44 = phi i32 [ %42, %40 ], [ %34, %32 ]
%45 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 2
%46 = bitcast %struct.TYPE_7__* %45 to i64*
%47 = load i64, i64* %46
%48 = load i64, i64* @INADDR_BROADCAST
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %53
50: ; preds = %43
%51 = load i32, i32* @RTF_HOST
%52 = or i32 %44, %51
store i32 %52, i32* %35
br label %53
53: ; preds = %50, %43
%54 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 1
store i8* %1, i8** %54
%55 = load i8*, i8** @AF_INET
%56 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i64 0, i32 4, i32 0
store i8* %55, i8** %56
%57 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %36, i64 0, i32 0
store i8* %55, i8** %57
%58 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %45, i64 0, i32 0
store i8* %55, i8** %58
%59 = call i64 @ioctl(i32 %11, i32 %0, %struct.rtentry* %7)
%60 = icmp slt i64 %59, 0
br i1 %60, label %63, label %61
61: ; preds = %53
%62 = call i32 @close(i32 %11)
br label %67
63: ; preds = %53, %6
%64 = call i32 @close(i32 %11)
%65 = call i32 @perror(i8* %1)
%66 = load i32, i32* @errno
br label %67
67: ; preds = %63, %61
%68 = phi i32 [ %66, %63 ], [ 0, %61 ]
ret i32 %68
}