}
 }
 
+static int add_to_sockmap(int sock_mapfd, int fd1, int fd2)
+{
+       u64 value;
+       u32 key;
+       int err;
+
+       key = 0;
+       value = fd1;
+       err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
+       if (err)
+               return err;
+
+       key = 1;
+       value = fd2;
+       return xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
+}
+
 static void redir_to_connected(int family, int sotype, int sock_mapfd,
                               int verd_mapfd, enum redir_mode mode)
 {
        unsigned int pass;
        socklen_t len;
        int err, n;
-       u64 value;
        u32 key;
        char b;
 
        if (p1 < 0)
                goto close_cli1;
 
-       key = 0;
-       value = p0;
-       err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
-       if (err)
-               goto close_peer1;
-
-       key = 1;
-       value = p1;
-       err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
+       err = add_to_sockmap(sock_mapfd, p0, p1);
        if (err)
                goto close_peer1;
 
        int s, c, p, err, n;
        unsigned int drop;
        socklen_t len;
-       u64 value;
        u32 key;
 
        zero_verdict_count(verd_mapfd);
        if (p < 0)
                goto close_cli;
 
-       key = 0;
-       value = s;
-       err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
-       if (err)
-               goto close_peer;
-
-       key = 1;
-       value = p;
-       err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
+       err = add_to_sockmap(sock_mapfd, s, p);
        if (err)
                goto close_peer;
 
        int s1, s2, c, err;
        unsigned int drop;
        socklen_t len;
-       u64 value;
        u32 key;
 
        zero_verdict_count(verd_map);
        if (s2 < 0)
                goto close_srv1;
 
-       key = 0;
-       value = s1;
-       err = xbpf_map_update_elem(sock_map, &key, &value, BPF_NOEXIST);
+       err = add_to_sockmap(sock_map, s1, s2);
        if (err)
                goto close_srv2;
 
-       key = 1;
-       value = s2;
-       err = xbpf_map_update_elem(sock_map, &key, &value, BPF_NOEXIST);
-
        /* Connect to s2, reuseport BPF selects s1 via sock_map[0] */
        len = sizeof(addr);
        err = xgetsockname(s2, sockaddr(&addr), &len);
        unsigned int pass;
        int retries = 100;
        int err, n;
-       u64 value;
        u32 key;
        char b;
 
        if (err)
                goto close_cli0;
 
-       key = 0;
-       value = p0;
-       err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
-       if (err)
-               goto close_cli1;
-
-       key = 1;
-       value = p1;
-       err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST);
+       err = add_to_sockmap(sock_mapfd, p0, p1);
        if (err)
                goto close_cli1;