#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 */
typedef int /*<<< orphan*/ uint32_t;
struct in_addr {
  int dummy;
};
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
struct in_addr inet_makeaddr(int /*<<< orphan*/, unsigned int);
scalar_t__ ip4_addr_set(int, int /*<<< orphan*/, char *, struct in_addr, int);
int /*<<< orphan*/ ksft_print_msg(char *, int /*<<< orphan*/, int, ...);
scalar_t__ link_set_up(int, int /*<<< orphan*/, char *);
scalar_t__ netlink_sock(int *, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ switch_ns(int);
scalar_t__ tunnel_set_route(int, int /*<<< orphan*/ *, char *, struct in_addr,
                            struct in_addr);
int init_child(int nsfd, char *veth, unsigned int src, unsigned int dst) {
  struct in_addr intsrc = inet_makeaddr(((in_addr_t)0xc0a80000), src);
  struct in_addr tunsrc = inet_makeaddr(((in_addr_t)0x0a000000), src);
  struct in_addr tundst = inet_makeaddr(((in_addr_t)0x0a000000), dst);
  int route_sock = -1, ret = -1;
  uint32_t route_seq;
  if (switch_ns(nsfd))
    return -1;
  if (netlink_sock(&route_sock, &route_seq, NETLINK_ROUTE)) {
    ksft_print_msg("%d[%u] "
                   "Failed to open netlink route socket in child"
                   "\n",
                   getpid(), 455);
    return -1;
  }
  if (ip4_addr_set(route_sock, route_seq++, veth, intsrc, 30)) {
    ksft_print_msg("%d[%u] "
                   "Failed to set ipv4 addr"
                   "\n",
                   getpid(), 460);
    goto err;
  }
  if (link_set_up(route_sock, route_seq++, veth)) {
    ksft_print_msg("%d[%u] "
                   "Failed to bring up %s"
                   "\n",
                   getpid(), 465, veth);
    goto err;
  }
  if (tunnel_set_route(route_sock, &route_seq, veth, tunsrc, tundst)) {
    ksft_print_msg("%d[%u] "
                   "Failed to add tunnel route on %s"
                   "\n",
                   getpid(), 470, veth);
    goto err;
  }
  ret = 0;
err:
  close(route_sock);
  return ret;
}


x86 assembly (clang, -O0):
<init_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%esi
mov    $0xc0a80000,%edi
call   1040 <inet_makeaddr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%esi
mov    $0xa000000,%edi
call   1040 <inet_makeaddr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%esi
mov    $0xa000000,%edi
call   1040 <inet_makeaddr@plt>
mov    %eax,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0xffffffff,-0x38(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <switch_ns@plt>
cmp    $0x0,%rax
je     1201 <init_child+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <init_child+0x1a5>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
call   10b0 <netlink_sock@plt>
cmp    $0x0,%rax
je     1249 <init_child+0xb9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdcf(%rip),%rdi
mov    $0x1c7,%edx
mov    $0x0,%al
call   10a0 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <init_child+0x1a5>
mov    -0x34(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%ecx
mov    $0x1e,%r8d
call   1090 <ip4_addr_set@plt>
cmp    $0x0,%rax
je     1294 <init_child+0x104>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdb2(%rip),%rdi
mov    $0x1cc,%edx
mov    $0x0,%al
call   10a0 <ksft_print_msg@plt>
jmp    1327 <init_child+0x197>
mov    -0x34(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdx
call   1080 <link_set_up@plt>
cmp    $0x0,%rax
je     12da <init_child+0x14a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xd8c(%rip),%rdi
mov    $0x1d1,%edx
mov    $0x0,%al
call   10a0 <ksft_print_msg@plt>
jmp    1327 <init_child+0x197>
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x30(%rbp),%r8d
lea    -0x3c(%rbp),%rsi
call   1070 <tunnel_set_route@plt>
cmp    $0x0,%rax
je     1320 <init_child+0x190>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xd64(%rip),%rdi
mov    $0x1d6,%edx
mov    $0x0,%al
call   10a0 <ksft_print_msg@plt>
jmp    1327 <init_child+0x197>
movl   $0x0,-0x38(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_child>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rsi,%r15
mov    %edi,%r14d
mov    $0xc0a80000,%edi
mov    %edx,%esi
call   1040 <inet_makeaddr@plt>
mov    %eax,%r13d
mov    $0xa000000,%edi
mov    %ebx,%esi
call   1040 <inet_makeaddr@plt>
mov    %eax,%ebx
mov    $0xa000000,%edi
mov    %ebp,%esi
call   1040 <inet_makeaddr@plt>
mov    %eax,%r12d
movl   $0xffffffff,0x4(%rsp)
ff
mov    %r14d,%edi
call   1050 <switch_ns@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    12e3 <init_child+0x153>
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
call   10b0 <netlink_sock@plt>
test   %rax,%rax
je     122b <init_child+0x9b>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xde8(%rip),%rdi
mov    %eax,%esi
mov    $0x1c7,%edx
xor    %eax,%eax
call   10a0 <ksft_print_msg@plt>
jmp    12e3 <init_child+0x153>
mov    0x4(%rsp),%edi
mov    (%rsp),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rsp)
mov    %r15,%rdx
mov    %r13d,%ecx
mov    $0x1e,%r8d
call   1090 <ip4_addr_set@plt>
test   %rax,%rax
je     126c <init_child+0xdc>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdd9(%rip),%rdi
mov    %eax,%esi
mov    $0x1cc,%edx
xor    %eax,%eax
call   10a0 <ksft_print_msg@plt>
jmp    12d5 <init_child+0x145>
mov    0x4(%rsp),%edi
mov    (%rsp),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rsp)
mov    %r15,%rdx
call   1080 <link_set_up@plt>
test   %rax,%rax
je     129d <init_child+0x10d>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdc1(%rip),%rdi
mov    %eax,%esi
mov    $0x1d1,%edx
jmp    12cb <init_child+0x13b>
mov    0x4(%rsp),%edi
mov    %rsp,%rsi
mov    %r15,%rdx
mov    %ebx,%ecx
mov    %r12d,%r8d
call   1070 <tunnel_set_route@plt>
test   %rax,%rax
je     12f4 <init_child+0x164>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdaf(%rip),%rdi
mov    %eax,%esi
mov    $0x1d6,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   10a0 <ksft_print_msg@plt>
mov    $0xffffffff,%ebp
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    12da <init_child+0x14a>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@NETLINK_ROUTE = global i32 0
@.str = [53 x i8] c"%d[%u] Failed to open netlink route socket in child\0A\00"
@.str.1 = [32 x i8] c"%d[%u] Failed to set ipv4 addr\0A\00"
@.str.2 = [30 x i8] c"%d[%u] Failed to bring up %s\0A\00"
@.str.3 = [41 x i8] c"%d[%u] Failed to add tunnel route on %s\0A\00"
define i32 @init_child(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.in_addr
%11 = alloca %struct.in_addr
%12 = alloca %struct.in_addr
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%16 = load i32, i32* %8
%17 = call i32 @inet_makeaddr(i32 -1062731776, i32 %16)
%18 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %10, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %8
%20 = call i32 @inet_makeaddr(i32 167772160, i32 %19)
%21 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %11, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %9
%23 = call i32 @inet_makeaddr(i32 167772160, i32 %22)
%24 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i32 0, i32 0
store i32 %23, i32* %24
store i32 -1, i32* %13
store i32 -1, i32* %14
%25 = load i32, i32* %6
%26 = call i64 @switch_ns(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %4
store i32 -1, i32* %5
br label %77
29: ; preds = %4
%30 = load i32, i32* @NETLINK_ROUTE
%31 = call i64 @netlink_sock(i32* %13, i32* %15, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 (...) @getpid()
%35 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %34, i32 455)
store i32 -1, i32* %5
br label %77
36: ; preds = %29
%37 = load i32, i32* %13
%38 = load i32, i32* %15
%39 = add nsw i32 %38, 1
store i32 %39, i32* %15
%40 = load i8*, i8** %7
%41 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %10, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @ip4_addr_set(i32 %37, i32 %38, i8* %40, i32 %42, i32 30)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %36
%46 = call i32 (...) @getpid()
%47 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %46, i32 460)
br label %73
48: ; preds = %36
%49 = load i32, i32* %13
%50 = load i32, i32* %15
%51 = add nsw i32 %50, 1
store i32 %51, i32* %15
%52 = load i8*, i8** %7
%53 = call i64 @link_set_up(i32 %49, i32 %50, i8* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %48
%56 = call i32 (...) @getpid()
%57 = load i8*, i8** %7
%58 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %56, i32 465, i8* %57)
br label %73
59: ; preds = %48
%60 = load i32, i32* %13
%61 = load i8*, i8** %7
%62 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %11, i32 0, i32 0
%63 = load i32, i32* %62
%64 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i64 @tunnel_set_route(i32 %60, i32* %15, i8* %61, i32 %63, i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %59
%69 = call i32 (...) @getpid()
%70 = load i8*, i8** %7
%71 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %69, i32 470, i8* %70)
br label %73
72: ; preds = %59
store i32 0, i32* %14
br label %73
73: ; preds = %72, %68, %55, %45
%74 = load i32, i32* %13
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %14
store i32 %76, i32* %5
br label %77
77: ; preds = %73, %33, %28
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
@NETLINK_ROUTE = global i32 0
@.str = [53 x i8] c"%d[%u] Failed to open netlink route socket in child\0A\00"
@.str.1 = [32 x i8] c"%d[%u] Failed to set ipv4 addr\0A\00"
@.str.2 = [30 x i8] c"%d[%u] Failed to bring up %s\0A\00"
@.str.3 = [41 x i8] c"%d[%u] Failed to add tunnel route on %s\0A\00"
define i32 @init_child(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = tail call i32 @inet_makeaddr(i32 -1062731776, i32 %2)
%8 = tail call i32 @inet_makeaddr(i32 167772160, i32 %2)
%9 = tail call i32 @inet_makeaddr(i32 167772160, i32 %3)
%10 = bitcast i32* %5 to i8*
store i32 -1, i32* %5
%11 = bitcast i32* %6 to i8*
%12 = tail call i64 @switch_ns(i32 %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %50
14: ; preds = %4
%15 = load i32, i32* @NETLINK_ROUTE
%16 = call i64 @netlink_sock(i32* %5, i32* %6, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %19, i32 455)
br label %50
21: ; preds = %14
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
%25 = call i64 @ip4_addr_set(i32 %22, i32 %23, i8* %1, i32 %7, i32 30)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %21
%28 = call i32 (...) @getpid()
%29 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 460)
br label %46
30: ; preds = %21
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
%34 = call i64 @link_set_up(i32 %31, i32 %32, i8* %1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %30
%37 = call i32 (...) @getpid()
%38 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 465, i8* %1)
br label %46
39: ; preds = %30
%40 = load i32, i32* %5
%41 = call i64 @tunnel_set_route(i32 %40, i32* %6, i8* %1, i32 %8, i32 %9)
%42 = icmp eq i64 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %39
%44 = call i32 (...) @getpid()
%45 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 470, i8* %1)
br label %46
46: ; preds = %39, %43, %36, %27
%47 = phi i32 [ -1, %27 ], [ -1, %36 ], [ -1, %43 ], [ 0, %39 ]
%48 = load i32, i32* %5
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %4, %46, %18
%51 = phi i32 [ -1, %18 ], [ %47, %46 ], [ -1, %4 ]
ret i32 %51
}