We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Unused optimisation.
### Version Information

master

### What's your issue about?
Investigate listed TODO: https://github.com/ethereum/vyper/blame/master/vyper/optimizer.py#L99
```python
    # Turns out this is actually not such a good optimization after all
    elif node.value == "with" and int_at(argz, 1) and not search_for_set(argz[2], argz[0].value) and False:
        o = replace_with_value(argz[2], argz[0].value, argz[1].value)
        return o
```

### How can it be fixed?

I have not fully investigated, but I suspect we can just drop the optimisation.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `vyper/optimizer.py`
Content:
```
1 import operator
2 
3 from vyper.parser.parser_utils import (
4     LLLnode,
5 )
6 from vyper.utils import (
7     LOADED_LIMIT_MAP,
8 )
9 
10 
11 def get_int_at(args, pos, signed=False):
12     value = args[pos].value
13 
14     if isinstance(value, int):
15         o = value
16     elif value == "mload" and args[pos].args[0].value in LOADED_LIMIT_MAP.keys():
17         o = LOADED_LIMIT_MAP[args[pos].args[0].value]
18     else:
19         return None
20 
21     if signed or o < 0:
22         return ((o + 2**255) % 2**256) - 2**255
23     else:
24         return o % 2**256
25 
26 
27 def int_at(args, pos):
28     return get_int_at(args, pos) is not None
29 
30 
31 def search_for_set(node, var):
32     if node.value == "set" and node.args[0].value == var:
33         return True
34 
35     for arg in node.args:
36         if search_for_set(arg, var):
37             return True
38 
39     return False
40 
41 
42 def replace_with_value(node, var, value):
43     if node.value == "with" and node.args[0].value == var:
44         return LLLnode(
45             node.value,
46             [
47                 node.args[0],
48                 replace_with_value(node.args[1], var, value),
49                 node.args[2]
50             ],
51             node.typ,
52             node.location,
53             node.annotation,
54         )
55     elif node.value == var:
56         return LLLnode(value, [], node.typ, node.location, node.annotation)
57     else:
58         return LLLnode(
59             node.value,
60             [replace_with_value(arg, var, value) for arg in node.args],
61             node.typ,
62             node.location,
63             node.annotation,
64         )
65 
66 
67 arith = {
68     "add": (operator.add, '+'),
69     "sub": (operator.sub, '-'),
70     "mul": (operator.mul, '*'),
71     "div": (operator.floordiv, '/'),
72     "mod": (operator.mod, '%'),
73 }
74 
75 
76 def _is_constant_add(node, args):
77     return (
78         (
79             node.value == "add" and int_at(args, 0)
80         ) and (
81             args[1].value == "add" and int_at(args[1].args, 0)
82         )
83     )
84 
85 
86 def _is_with_without_set(node, args):
87     # TODO: this unconditionally returns `False`.  Corresponding optimizer path
88     # should likely be removed.
89     return (
90         (
91             node.value == "with" and int_at(args, 1)
92         ) and (
93             not search_for_set(args[2], args[0].value)
94         ) and (
95             False
96         )
97     )
98 
99 
100 def has_cond_arg(node):
101     return node.value in ['if', 'if_unchecked', 'assert', 'assert_reason']
102 
103 
104 def optimize(node: LLLnode) -> LLLnode:
105     argz = [optimize(arg) for arg in node.args]
106     if node.value in arith and int_at(argz, 0) and int_at(argz, 1):
107         left, right = get_int_at(argz, 0), get_int_at(argz, 1)
108         calcer, symb = arith[node.value]
109         new_value = calcer(left, right)
110         if argz[0].annotation and argz[1].annotation:
111             annotation = argz[0].annotation + symb + argz[1].annotation
112         elif argz[0].annotation or argz[1].annotation:
113             annotation = (
114                 argz[0].annotation or str(left)
115             ) + symb + (
116                 argz[1].annotation or str(right)
117             )
118         else:
119             annotation = ''
120         return LLLnode(
121             new_value,
122             [],
123             node.typ,
124             None,
125             node.pos,
126             annotation,
127             add_gas_estimate=node.add_gas_estimate,
128             valency=node.valency,
129         )
130     elif _is_constant_add(node, argz):
131         calcer, symb = arith[node.value]
132         if argz[0].annotation and argz[1].args[0].annotation:
133             annotation = argz[0].annotation + symb + argz[1].args[0].annotation
134         elif argz[0].annotation or argz[1].args[0].annotation:
135             annotation = (
136                 argz[0].annotation or str(argz[0].value)
137             ) + symb + (
138                 argz[1].args[0].annotation or str(argz[1].args[0].value)
139             )
140         else:
141             annotation = ''
142         return LLLnode(
143             "add",
144             [
145                 LLLnode(argz[0].value + argz[1].args[0].value, annotation=annotation),
146                 argz[1].args[1],
147             ],
148             node.typ,
149             None,
150             node.annotation,
151             add_gas_estimate=node.add_gas_estimate,
152             valency=node.valency,
153         )
154     elif node.value == "add" and get_int_at(argz, 0) == 0:
155         return LLLnode(
156             argz[1].value,
157             argz[1].args,
158             node.typ,
159             node.location,
160             node.pos,
161             argz[1].annotation,
162             add_gas_estimate=node.add_gas_estimate,
163             valency=node.valency,
164         )
165     elif node.value == "add" and get_int_at(argz, 1) == 0:
166         return LLLnode(
167             argz[0].value,
168             argz[0].args,
169             node.typ,
170             node.location,
171             node.pos,
172             argz[0].annotation,
173             add_gas_estimate=node.add_gas_estimate,
174             valency=node.valency,
175         )
176     elif node.value == "clamp" and int_at(argz, 0) and int_at(argz, 1) and int_at(argz, 2):
177         if get_int_at(argz, 0, True) > get_int_at(argz, 1, True):
178             raise Exception("Clamp always fails")
179         elif get_int_at(argz, 1, True) > get_int_at(argz, 2, True):
180             raise Exception("Clamp always fails")
181         else:
182             return argz[1]
183     elif node.value == "clamp" and int_at(argz, 0) and int_at(argz, 1):
184         if get_int_at(argz, 0, True) > get_int_at(argz, 1, True):
185             raise Exception("Clamp always fails")
186         else:
187             return LLLnode(
188                 "clample",
189                 [argz[1], argz[2]],
190                 node.typ,
191                 node.location,
192                 node.pos,
193                 node.annotation,
194                 add_gas_estimate=node.add_gas_estimate,
195                 valency=node.valency,
196             )
197     elif node.value == "clamp_nonzero" and int_at(argz, 0):
198         if get_int_at(argz, 0) != 0:
199             return LLLnode(
200                 argz[0].value,
201                 [],
202                 node.typ,
203                 node.location,
204                 node.pos,
205                 node.annotation,
206                 add_gas_estimate=node.add_gas_estimate,
207                 valency=node.valency,
208             )
209         else:
210             raise Exception("Clamp always fails")
211     # [eq, x, 0] is the same as [iszero, x].
212     elif node.value == 'eq' and int_at(argz, 1) and argz[1].value == 0:
213         return LLLnode(
214             'iszero',
215             [argz[0]],
216             node.typ,
217             node.location,
218             node.pos,
219             node.annotation,
220             add_gas_estimate=node.add_gas_estimate,
221             valency=node.valency,
222         )
223     # [ne, x, y] has the same truthyness as [xor, x, y]
224     # rewrite 'ne' as 'xor' in places where truthy is accepted.
225     elif has_cond_arg(node) and argz[0].value == 'ne':
226         argz[0] = LLLnode.from_list(['xor'] + argz[0].args)
227         return LLLnode.from_list(
228                 [node.value] + argz,
229                 typ=node.typ,
230                 location=node.location,
231                 pos=node.pos,
232                 annotation=node.annotation,
233                 # let from_list handle valency and gas_estimate
234                 )
235     elif _is_with_without_set(node, argz):
236         # TODO: This block is currently unreachable due to
237         # `_is_with_without_set` unconditionally returning `False` this appears
238         # to be because this "is actually not such a good optimization after
239         # all" accordiing to previous comment.
240         o = replace_with_value(argz[2], argz[0].value, argz[1].value)
241         return o
242     elif node.value == "seq":
243         o = []
244         for arg in argz:
245             if arg.value == "seq":
246                 o.extend(arg.args)
247             else:
248                 o.append(arg)
249         return LLLnode(
250             node.value,
251             o,
252             node.typ,
253             node.location,
254             node.pos,
255             node.annotation,
256             add_gas_estimate=node.add_gas_estimate,
257             valency=node.valency,
258         )
259     elif node.total_gas is not None:
260         o = LLLnode(
261             node.value,
262             argz,
263             node.typ,
264             node.location,
265             node.pos,
266             node.annotation,
267             add_gas_estimate=node.add_gas_estimate,
268             valency=node.valency,
269         )
270         o.total_gas = node.total_gas - node.gas + o.gas
271         o.func_name = node.func_name
272         return o
273     else:
274         return LLLnode(
275             node.value,
276             argz,
277             node.typ,
278             node.location,
279             node.pos,
280             node.annotation,
281             add_gas_estimate=node.add_gas_estimate,
282             valency=node.valency,
283         )
284
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/vyper/optimizer.py b/vyper/optimizer.py
--- a/vyper/optimizer.py
+++ b/vyper/optimizer.py
@@ -1,4 +1,8 @@
 import operator
+from typing import (
+    Any,
+    List,
+)
 
 from vyper.parser.parser_utils import (
     LLLnode,
@@ -39,31 +43,6 @@
     return False
 
 
-def replace_with_value(node, var, value):
-    if node.value == "with" and node.args[0].value == var:
-        return LLLnode(
-            node.value,
-            [
-                node.args[0],
-                replace_with_value(node.args[1], var, value),
-                node.args[2]
-            ],
-            node.typ,
-            node.location,
-            node.annotation,
-        )
-    elif node.value == var:
-        return LLLnode(value, [], node.typ, node.location, node.annotation)
-    else:
-        return LLLnode(
-            node.value,
-            [replace_with_value(arg, var, value) for arg in node.args],
-            node.typ,
-            node.location,
-            node.annotation,
-        )
-
-
 arith = {
     "add": (operator.add, '+'),
     "sub": (operator.sub, '-'),
@@ -83,20 +62,6 @@
     )
 
 
-def _is_with_without_set(node, args):
-    # TODO: this unconditionally returns `False`.  Corresponding optimizer path
-    # should likely be removed.
-    return (
-        (
-            node.value == "with" and int_at(args, 1)
-        ) and (
-            not search_for_set(args[2], args[0].value)
-        ) and (
-            False
-        )
-    )
-
-
 def has_cond_arg(node):
     return node.value in ['if', 'if_unchecked', 'assert', 'assert_reason']
 
@@ -232,23 +197,16 @@
                 annotation=node.annotation,
                 # let from_list handle valency and gas_estimate
                 )
-    elif _is_with_without_set(node, argz):
-        # TODO: This block is currently unreachable due to
-        # `_is_with_without_set` unconditionally returning `False` this appears
-        # to be because this "is actually not such a good optimization after
-        # all" accordiing to previous comment.
-        o = replace_with_value(argz[2], argz[0].value, argz[1].value)
-        return o
     elif node.value == "seq":
-        o = []
+        xs: List[Any] = []
         for arg in argz:
             if arg.value == "seq":
-                o.extend(arg.args)
+                xs.extend(arg.args)
             else:
-                o.append(arg)
+                xs.append(arg)
         return LLLnode(
             node.value,
-            o,
+            xs,
             node.typ,
             node.location,
             node.pos,

{"golden_diff": "diff --git a/vyper/optimizer.py b/vyper/optimizer.py\n--- a/vyper/optimizer.py\n+++ b/vyper/optimizer.py\n@@ -1,4 +1,8 @@\n import operator\n+from typing import (\n+    Any,\n+    List,\n+)\n \n from vyper.parser.parser_utils import (\n     LLLnode,\n@@ -39,31 +43,6 @@\n     return False\n \n \n-def replace_with_value(node, var, value):\n-    if node.value == \"with\" and node.args[0].value == var:\n-        return LLLnode(\n-            node.value,\n-            [\n-                node.args[0],\n-                replace_with_value(node.args[1], var, value),\n-                node.args[2]\n-            ],\n-            node.typ,\n-            node.location,\n-            node.annotation,\n-        )\n-    elif node.value == var:\n-        return LLLnode(value, [], node.typ, node.location, node.annotation)\n-    else:\n-        return LLLnode(\n-            node.value,\n-            [replace_with_value(arg, var, value) for arg in node.args],\n-            node.typ,\n-            node.location,\n-            node.annotation,\n-        )\n-\n-\n arith = {\n     \"add\": (operator.add, '+'),\n     \"sub\": (operator.sub, '-'),\n@@ -83,20 +62,6 @@\n     )\n \n \n-def _is_with_without_set(node, args):\n-    # TODO: this unconditionally returns `False`.  Corresponding optimizer path\n-    # should likely be removed.\n-    return (\n-        (\n-            node.value == \"with\" and int_at(args, 1)\n-        ) and (\n-            not search_for_set(args[2], args[0].value)\n-        ) and (\n-            False\n-        )\n-    )\n-\n-\n def has_cond_arg(node):\n     return node.value in ['if', 'if_unchecked', 'assert', 'assert_reason']\n \n@@ -232,23 +197,16 @@\n                 annotation=node.annotation,\n                 # let from_list handle valency and gas_estimate\n                 )\n-    elif _is_with_without_set(node, argz):\n-        # TODO: This block is currently unreachable due to\n-        # `_is_with_without_set` unconditionally returning `False` this appears\n-        # to be because this \"is actually not such a good optimization after\n-        # all\" accordiing to previous comment.\n-        o = replace_with_value(argz[2], argz[0].value, argz[1].value)\n-        return o\n     elif node.value == \"seq\":\n-        o = []\n+        xs: List[Any] = []\n         for arg in argz:\n             if arg.value == \"seq\":\n-                o.extend(arg.args)\n+                xs.extend(arg.args)\n             else:\n-                o.append(arg)\n+                xs.append(arg)\n         return LLLnode(\n             node.value,\n-            o,\n+            xs,\n             node.typ,\n             node.location,\n             node.pos,\n", "issue": "Unused optimisation.\n### Version Information\r\n\r\nmaster\r\n\r\n### What's your issue about?\r\nInvestigate listed TODO: https://github.com/ethereum/vyper/blame/master/vyper/optimizer.py#L99\r\n```python\r\n    # Turns out this is actually not such a good optimization after all\r\n    elif node.value == \"with\" and int_at(argz, 1) and not search_for_set(argz[2], argz[0].value) and False:\r\n        o = replace_with_value(argz[2], argz[0].value, argz[1].value)\r\n        return o\r\n```\r\n\r\n### How can it be fixed?\r\n\r\nI have not fully investigated, but I suspect we can just drop the optimisation.\n", "before_files": [{"content": "import operator\n\nfrom vyper.parser.parser_utils import (\n    LLLnode,\n)\nfrom vyper.utils import (\n    LOADED_LIMIT_MAP,\n)\n\n\ndef get_int_at(args, pos, signed=False):\n    value = args[pos].value\n\n    if isinstance(value, int):\n        o = value\n    elif value == \"mload\" and args[pos].args[0].value in LOADED_LIMIT_MAP.keys():\n        o = LOADED_LIMIT_MAP[args[pos].args[0].value]\n    else:\n        return None\n\n    if signed or o < 0:\n        return ((o + 2**255) % 2**256) - 2**255\n    else:\n        return o % 2**256\n\n\ndef int_at(args, pos):\n    return get_int_at(args, pos) is not None\n\n\ndef search_for_set(node, var):\n    if node.value == \"set\" and node.args[0].value == var:\n        return True\n\n    for arg in node.args:\n        if search_for_set(arg, var):\n            return True\n\n    return False\n\n\ndef replace_with_value(node, var, value):\n    if node.value == \"with\" and node.args[0].value == var:\n        return LLLnode(\n            node.value,\n            [\n                node.args[0],\n                replace_with_value(node.args[1], var, value),\n                node.args[2]\n            ],\n            node.typ,\n            node.location,\n            node.annotation,\n        )\n    elif node.value == var:\n        return LLLnode(value, [], node.typ, node.location, node.annotation)\n    else:\n        return LLLnode(\n            node.value,\n            [replace_with_value(arg, var, value) for arg in node.args],\n            node.typ,\n            node.location,\n            node.annotation,\n        )\n\n\narith = {\n    \"add\": (operator.add, '+'),\n    \"sub\": (operator.sub, '-'),\n    \"mul\": (operator.mul, '*'),\n    \"div\": (operator.floordiv, '/'),\n    \"mod\": (operator.mod, '%'),\n}\n\n\ndef _is_constant_add(node, args):\n    return (\n        (\n            node.value == \"add\" and int_at(args, 0)\n        ) and (\n            args[1].value == \"add\" and int_at(args[1].args, 0)\n        )\n    )\n\n\ndef _is_with_without_set(node, args):\n    # TODO: this unconditionally returns `False`.  Corresponding optimizer path\n    # should likely be removed.\n    return (\n        (\n            node.value == \"with\" and int_at(args, 1)\n        ) and (\n            not search_for_set(args[2], args[0].value)\n        ) and (\n            False\n        )\n    )\n\n\ndef has_cond_arg(node):\n    return node.value in ['if', 'if_unchecked', 'assert', 'assert_reason']\n\n\ndef optimize(node: LLLnode) -> LLLnode:\n    argz = [optimize(arg) for arg in node.args]\n    if node.value in arith and int_at(argz, 0) and int_at(argz, 1):\n        left, right = get_int_at(argz, 0), get_int_at(argz, 1)\n        calcer, symb = arith[node.value]\n        new_value = calcer(left, right)\n        if argz[0].annotation and argz[1].annotation:\n            annotation = argz[0].annotation + symb + argz[1].annotation\n        elif argz[0].annotation or argz[1].annotation:\n            annotation = (\n                argz[0].annotation or str(left)\n            ) + symb + (\n                argz[1].annotation or str(right)\n            )\n        else:\n            annotation = ''\n        return LLLnode(\n            new_value,\n            [],\n            node.typ,\n            None,\n            node.pos,\n            annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif _is_constant_add(node, argz):\n        calcer, symb = arith[node.value]\n        if argz[0].annotation and argz[1].args[0].annotation:\n            annotation = argz[0].annotation + symb + argz[1].args[0].annotation\n        elif argz[0].annotation or argz[1].args[0].annotation:\n            annotation = (\n                argz[0].annotation or str(argz[0].value)\n            ) + symb + (\n                argz[1].args[0].annotation or str(argz[1].args[0].value)\n            )\n        else:\n            annotation = ''\n        return LLLnode(\n            \"add\",\n            [\n                LLLnode(argz[0].value + argz[1].args[0].value, annotation=annotation),\n                argz[1].args[1],\n            ],\n            node.typ,\n            None,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif node.value == \"add\" and get_int_at(argz, 0) == 0:\n        return LLLnode(\n            argz[1].value,\n            argz[1].args,\n            node.typ,\n            node.location,\n            node.pos,\n            argz[1].annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif node.value == \"add\" and get_int_at(argz, 1) == 0:\n        return LLLnode(\n            argz[0].value,\n            argz[0].args,\n            node.typ,\n            node.location,\n            node.pos,\n            argz[0].annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif node.value == \"clamp\" and int_at(argz, 0) and int_at(argz, 1) and int_at(argz, 2):\n        if get_int_at(argz, 0, True) > get_int_at(argz, 1, True):\n            raise Exception(\"Clamp always fails\")\n        elif get_int_at(argz, 1, True) > get_int_at(argz, 2, True):\n            raise Exception(\"Clamp always fails\")\n        else:\n            return argz[1]\n    elif node.value == \"clamp\" and int_at(argz, 0) and int_at(argz, 1):\n        if get_int_at(argz, 0, True) > get_int_at(argz, 1, True):\n            raise Exception(\"Clamp always fails\")\n        else:\n            return LLLnode(\n                \"clample\",\n                [argz[1], argz[2]],\n                node.typ,\n                node.location,\n                node.pos,\n                node.annotation,\n                add_gas_estimate=node.add_gas_estimate,\n                valency=node.valency,\n            )\n    elif node.value == \"clamp_nonzero\" and int_at(argz, 0):\n        if get_int_at(argz, 0) != 0:\n            return LLLnode(\n                argz[0].value,\n                [],\n                node.typ,\n                node.location,\n                node.pos,\n                node.annotation,\n                add_gas_estimate=node.add_gas_estimate,\n                valency=node.valency,\n            )\n        else:\n            raise Exception(\"Clamp always fails\")\n    # [eq, x, 0] is the same as [iszero, x].\n    elif node.value == 'eq' and int_at(argz, 1) and argz[1].value == 0:\n        return LLLnode(\n            'iszero',\n            [argz[0]],\n            node.typ,\n            node.location,\n            node.pos,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    # [ne, x, y] has the same truthyness as [xor, x, y]\n    # rewrite 'ne' as 'xor' in places where truthy is accepted.\n    elif has_cond_arg(node) and argz[0].value == 'ne':\n        argz[0] = LLLnode.from_list(['xor'] + argz[0].args)\n        return LLLnode.from_list(\n                [node.value] + argz,\n                typ=node.typ,\n                location=node.location,\n                pos=node.pos,\n                annotation=node.annotation,\n                # let from_list handle valency and gas_estimate\n                )\n    elif _is_with_without_set(node, argz):\n        # TODO: This block is currently unreachable due to\n        # `_is_with_without_set` unconditionally returning `False` this appears\n        # to be because this \"is actually not such a good optimization after\n        # all\" accordiing to previous comment.\n        o = replace_with_value(argz[2], argz[0].value, argz[1].value)\n        return o\n    elif node.value == \"seq\":\n        o = []\n        for arg in argz:\n            if arg.value == \"seq\":\n                o.extend(arg.args)\n            else:\n                o.append(arg)\n        return LLLnode(\n            node.value,\n            o,\n            node.typ,\n            node.location,\n            node.pos,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif node.total_gas is not None:\n        o = LLLnode(\n            node.value,\n            argz,\n            node.typ,\n            node.location,\n            node.pos,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n        o.total_gas = node.total_gas - node.gas + o.gas\n        o.func_name = node.func_name\n        return o\n    else:\n        return LLLnode(\n            node.value,\n            argz,\n            node.typ,\n            node.location,\n            node.pos,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n", "path": "vyper/optimizer.py"}], "after_files": [{"content": "import operator\nfrom typing import (\n    Any,\n    List,\n)\n\nfrom vyper.parser.parser_utils import (\n    LLLnode,\n)\nfrom vyper.utils import (\n    LOADED_LIMIT_MAP,\n)\n\n\ndef get_int_at(args, pos, signed=False):\n    value = args[pos].value\n\n    if isinstance(value, int):\n        o = value\n    elif value == \"mload\" and args[pos].args[0].value in LOADED_LIMIT_MAP.keys():\n        o = LOADED_LIMIT_MAP[args[pos].args[0].value]\n    else:\n        return None\n\n    if signed or o < 0:\n        return ((o + 2**255) % 2**256) - 2**255\n    else:\n        return o % 2**256\n\n\ndef int_at(args, pos):\n    return get_int_at(args, pos) is not None\n\n\ndef search_for_set(node, var):\n    if node.value == \"set\" and node.args[0].value == var:\n        return True\n\n    for arg in node.args:\n        if search_for_set(arg, var):\n            return True\n\n    return False\n\n\narith = {\n    \"add\": (operator.add, '+'),\n    \"sub\": (operator.sub, '-'),\n    \"mul\": (operator.mul, '*'),\n    \"div\": (operator.floordiv, '/'),\n    \"mod\": (operator.mod, '%'),\n}\n\n\ndef _is_constant_add(node, args):\n    return (\n        (\n            node.value == \"add\" and int_at(args, 0)\n        ) and (\n            args[1].value == \"add\" and int_at(args[1].args, 0)\n        )\n    )\n\n\ndef has_cond_arg(node):\n    return node.value in ['if', 'if_unchecked', 'assert', 'assert_reason']\n\n\ndef optimize(node: LLLnode) -> LLLnode:\n    argz = [optimize(arg) for arg in node.args]\n    if node.value in arith and int_at(argz, 0) and int_at(argz, 1):\n        left, right = get_int_at(argz, 0), get_int_at(argz, 1)\n        calcer, symb = arith[node.value]\n        new_value = calcer(left, right)\n        if argz[0].annotation and argz[1].annotation:\n            annotation = argz[0].annotation + symb + argz[1].annotation\n        elif argz[0].annotation or argz[1].annotation:\n            annotation = (\n                argz[0].annotation or str(left)\n            ) + symb + (\n                argz[1].annotation or str(right)\n            )\n        else:\n            annotation = ''\n        return LLLnode(\n            new_value,\n            [],\n            node.typ,\n            None,\n            node.pos,\n            annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif _is_constant_add(node, argz):\n        calcer, symb = arith[node.value]\n        if argz[0].annotation and argz[1].args[0].annotation:\n            annotation = argz[0].annotation + symb + argz[1].args[0].annotation\n        elif argz[0].annotation or argz[1].args[0].annotation:\n            annotation = (\n                argz[0].annotation or str(argz[0].value)\n            ) + symb + (\n                argz[1].args[0].annotation or str(argz[1].args[0].value)\n            )\n        else:\n            annotation = ''\n        return LLLnode(\n            \"add\",\n            [\n                LLLnode(argz[0].value + argz[1].args[0].value, annotation=annotation),\n                argz[1].args[1],\n            ],\n            node.typ,\n            None,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif node.value == \"add\" and get_int_at(argz, 0) == 0:\n        return LLLnode(\n            argz[1].value,\n            argz[1].args,\n            node.typ,\n            node.location,\n            node.pos,\n            argz[1].annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif node.value == \"add\" and get_int_at(argz, 1) == 0:\n        return LLLnode(\n            argz[0].value,\n            argz[0].args,\n            node.typ,\n            node.location,\n            node.pos,\n            argz[0].annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif node.value == \"clamp\" and int_at(argz, 0) and int_at(argz, 1) and int_at(argz, 2):\n        if get_int_at(argz, 0, True) > get_int_at(argz, 1, True):\n            raise Exception(\"Clamp always fails\")\n        elif get_int_at(argz, 1, True) > get_int_at(argz, 2, True):\n            raise Exception(\"Clamp always fails\")\n        else:\n            return argz[1]\n    elif node.value == \"clamp\" and int_at(argz, 0) and int_at(argz, 1):\n        if get_int_at(argz, 0, True) > get_int_at(argz, 1, True):\n            raise Exception(\"Clamp always fails\")\n        else:\n            return LLLnode(\n                \"clample\",\n                [argz[1], argz[2]],\n                node.typ,\n                node.location,\n                node.pos,\n                node.annotation,\n                add_gas_estimate=node.add_gas_estimate,\n                valency=node.valency,\n            )\n    elif node.value == \"clamp_nonzero\" and int_at(argz, 0):\n        if get_int_at(argz, 0) != 0:\n            return LLLnode(\n                argz[0].value,\n                [],\n                node.typ,\n                node.location,\n                node.pos,\n                node.annotation,\n                add_gas_estimate=node.add_gas_estimate,\n                valency=node.valency,\n            )\n        else:\n            raise Exception(\"Clamp always fails\")\n    # [eq, x, 0] is the same as [iszero, x].\n    elif node.value == 'eq' and int_at(argz, 1) and argz[1].value == 0:\n        return LLLnode(\n            'iszero',\n            [argz[0]],\n            node.typ,\n            node.location,\n            node.pos,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    # [ne, x, y] has the same truthyness as [xor, x, y]\n    # rewrite 'ne' as 'xor' in places where truthy is accepted.\n    elif has_cond_arg(node) and argz[0].value == 'ne':\n        argz[0] = LLLnode.from_list(['xor'] + argz[0].args)\n        return LLLnode.from_list(\n                [node.value] + argz,\n                typ=node.typ,\n                location=node.location,\n                pos=node.pos,\n                annotation=node.annotation,\n                # let from_list handle valency and gas_estimate\n                )\n    elif node.value == \"seq\":\n        xs: List[Any] = []\n        for arg in argz:\n            if arg.value == \"seq\":\n                xs.extend(arg.args)\n            else:\n                xs.append(arg)\n        return LLLnode(\n            node.value,\n            xs,\n            node.typ,\n            node.location,\n            node.pos,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n    elif node.total_gas is not None:\n        o = LLLnode(\n            node.value,\n            argz,\n            node.typ,\n            node.location,\n            node.pos,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n        o.total_gas = node.total_gas - node.gas + o.gas\n        o.func_name = node.func_name\n        return o\n    else:\n        return LLLnode(\n            node.value,\n            argz,\n            node.typ,\n            node.location,\n            node.pos,\n            node.annotation,\n            add_gas_estimate=node.add_gas_estimate,\n            valency=node.valency,\n        )\n", "path": "vyper/optimizer.py"}]}