#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*/ old_node;
typedef int /*<<< orphan*/ new_node;
typedef int /*<<< orphan*/ action;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int get_socket(int);
int /*<<< orphan*/ perror(char *);
int send(int, int *, int, int /*<<< orphan*/);
int switch_node(int pid, int old_node, int new_node) {
  int s = get_socket(pid);
  if (s < 0)
    return s;
  int action = 2;
  if (send(s, &action, sizeof(action), 0) == -1) {
    perror("send");
    return -2;
  }
  if (send(s, &old_node, sizeof(old_node), 0) == -1) {
    perror("send");
    return -2;
  }
  if (send(s, &new_node, sizeof(new_node), 0) == -1) {
    perror("send");
    return -2;
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<switch_node>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <get_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1171 <switch_node+0x31>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1223 <switch_node+0xe3>
movl   $0x2,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
jne    11ac <switch_node+0x6c>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1223 <switch_node+0xe3>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
jne    11e0 <switch_node+0xa0>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1223 <switch_node+0xe3>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
jne    1214 <switch_node+0xd4>
lea    0xdfd(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1223 <switch_node+0xe3>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_node>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,0x14(%rsp)
mov    %edx,0x10(%rsp)
call   1030 <get_socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b4 <switch_node+0x74>
movl   $0x2,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
je     11bd <switch_node+0x7d>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
je     11bd <switch_node+0x7d>
lea    0x10(%rsp),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
je     11bd <switch_node+0x7d>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe3c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xfffffffe,%ebp
jmp    11b2 <switch_node+0x72>

LLVM IR (-O0):
@.str = [5 x i8] c"send\00"
define i32 @switch_node(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = call i32 @get_socket(i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* %8
store i32 %15, i32* %4
br label %37
16: ; preds = %3
store i32 2, i32* %9
%17 = load i32, i32* %8
%18 = call i32 @send(i32 %17, i32* %9, i32 4, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %4
br label %37
22: ; preds = %16
%23 = load i32, i32* %8
%24 = call i32 @send(i32 %23, i32* %6, i32 4, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %4
br label %37
28: ; preds = %22
%29 = load i32, i32* %8
%30 = call i32 @send(i32 %29, i32* %7, i32 4, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %4
br label %37
34: ; preds = %28
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
store i32 0, i32* %4
br label %37
37: ; preds = %34, %32, %26, %20, %14
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str = [5 x i8] c"send\00"
define i32 @switch_node(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %1, i32* %4
store i32 %2, i32* %5
%7 = tail call i32 @get_socket(i32 %0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %29, label %9
9: ; preds = %3
%10 = bitcast i32* %6 to i8*
store i32 2, i32* %6
%11 = call i32 @send(i32 %7, i32* %6, i32 4, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %27
15: ; preds = %9
%16 = call i32 @send(i32 %7, i32* %4, i32 4, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %27
20: ; preds = %15
%21 = call i32 @send(i32 %7, i32* %5, i32 4, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %27
25: ; preds = %20
%26 = call i32 @close(i32 %7)
br label %27
27: ; preds = %25, %23, %18, %13
%28 = phi i32 [ -2, %13 ], [ -2, %18 ], [ -2, %23 ], [ 0, %25 ]
br label %29
29: ; preds = %3, %27
%30 = phi i32 [ %28, %27 ], [ %7, %3 ]
ret i32 %30
}