int tgc_rxtx(int rx_sd, int tx_sd, unsigned char *buf, unsigned char key) {
  int nread = 0, nwritten = 0, i;
  if (rx_sd < 0 || tx_sd < 0 || !buf)
    return -3;
  if ((nread = read(rx_sd, buf, 4096)) == 0) {
    print_log_msg((5), __FUNCTION__, "End of data");
    return -6;
  }
  if (nread < 0) {
    print_log_msg((3), __FUNCTION__, "Error reading from socket");
    return -4;
  }
  if (key)
    for (i = 0; i < nread; buf[i++] ^= key)
      ;
  if ((nwritten = write(tx_sd, buf, nread)) < nread) {
    print_log_msg((3), __FUNCTION__, "Error writing to socket");
    return -5;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tgc_rxtx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1174 <tgc_rxtx+0x44>
cmpl   $0x0,-0xc(%rbp)
jl     1174 <tgc_rxtx+0x44>
cmpq   $0x0,-0x18(%rbp)
jne    1180 <tgc_rxtx+0x50>
movl   $0xfffffffd,-0x4(%rbp)
jmp    128a <tgc_rxtx+0x15a>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    11c5 <tgc_rxtx+0x95>
mov    $0x5,%edi
lea    0xe55(%rip),%rsi
lea    0xe57(%rip),%rdx
mov    $0x0,%al
call   1050 <print_log_msg@plt>
movl   $0xfffffffa,-0x4(%rbp)
jmp    128a <tgc_rxtx+0x15a>
cmpl   $0x0,-0x20(%rbp)
jge    11f5 <tgc_rxtx+0xc5>
mov    $0x3,%edi
lea    0xe25(%rip),%rsi
lea    0xe33(%rip),%rdx
mov    $0x0,%al
call   1050 <print_log_msg@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    128a <tgc_rxtx+0x15a>
cmpb   $0x0,-0x19(%rbp)
je     1240 <tgc_rxtx+0x110>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    123b <tgc_rxtx+0x10b>
jmp    1217 <tgc_rxtx+0xe7>
movzbl -0x19(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x28(%rbp)
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edx
xor    %esi,%edx
mov    %dl,(%rax,%rcx,1)
jmp    1206 <tgc_rxtx+0xd6>
jmp    1240 <tgc_rxtx+0x110>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    -0x20(%rbp),%eax
jge    1283 <tgc_rxtx+0x153>
mov    $0x3,%edi
lea    0xd97(%rip),%rsi
lea    0xdbf(%rip),%rdx
mov    $0x0,%al
call   1050 <print_log_msg@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    128a <tgc_rxtx+0x15a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tgc_rxtx>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%eax
or     %edi,%eax
mov    $0xfffffffd,%ebp
js     1313 <tgc_rxtx+0x1e3>
mov    %rdx,%rbx
test   %rdx,%rdx
je     1313 <tgc_rxtx+0x1e3>
mov    %ecx,%r12d
mov    %esi,%r14d
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %eax,%eax
call   1040 <read@plt>
test   %eax,%eax
je     118a <tgc_rxtx+0x5a>
js     11ae <tgc_rxtx+0x7e>
mov    %eax,%r15d
test   %r12b,%r12b
je     12dd <tgc_rxtx+0x1ad>
mov    %r15d,%eax
cmp    $0x8,%r15d
jae    11d2 <tgc_rxtx+0xa2>
xor    %ecx,%ecx
jmp    12d0 <tgc_rxtx+0x1a0>
lea    0xe6f(%rip),%rsi
lea    0xe71(%rip),%rdx
mov    $0x5,%edi
xor    %eax,%eax
call   1050 <print_log_msg@plt>
mov    $0xfffffffa,%ebp
jmp    1313 <tgc_rxtx+0x1e3>
lea    0xe4b(%rip),%rsi
lea    0xe59(%rip),%rdx
mov    $0x3,%edi
xor    %eax,%eax
call   1050 <print_log_msg@plt>
mov    $0xfffffffc,%ebp
jmp    1313 <tgc_rxtx+0x1e3>
movzbl %r12b,%edx
cmp    $0x20,%r15d
jae    11e3 <tgc_rxtx+0xb3>
xor    %ecx,%ecx
jmp    1299 <tgc_rxtx+0x169>
mov    %eax,%ecx
and    $0xffffffe0,%ecx
movd   %edx,%xmm0
punpcklbw %xmm0,%xmm0
pshuflw $0x0,%xmm0,%xmm0
pshufd $0x0,%xmm0,%xmm0
lea    -0x20(%rcx),%rsi
mov    %rsi,%rbp
shr    $0x5,%rbp
add    $0x1,%rbp
test   %rsi,%rsi
je     126a <tgc_rxtx+0x13a>
mov    %rbp,%rdi
and    $0xfffffffffffffffe,%rdi
xor    %esi,%esi
nopw   0x0(%rax,%rax,1)
movdqu (%rbx,%rsi,1),%xmm1
movdqu 0x10(%rbx,%rsi,1),%xmm2
movdqu 0x20(%rbx,%rsi,1),%xmm3
movdqu 0x30(%rbx,%rsi,1),%xmm4
pxor   %xmm0,%xmm1
pxor   %xmm0,%xmm2
movdqu %xmm1,(%rbx,%rsi,1)
movdqu %xmm2,0x10(%rbx,%rsi,1)
pxor   %xmm0,%xmm3
pxor   %xmm0,%xmm4
movdqu %xmm3,0x20(%rbx,%rsi,1)
movdqu %xmm4,0x30(%rbx,%rsi,1)
add    $0x40,%rsi
add    $0xfffffffffffffffe,%rdi
jne    1220 <tgc_rxtx+0xf0>
jmp    126c <tgc_rxtx+0x13c>
xor    %esi,%esi
test   $0x1,%bpl
je     1290 <tgc_rxtx+0x160>
movdqu (%rbx,%rsi,1),%xmm1
movdqu 0x10(%rbx,%rsi,1),%xmm2
pxor   %xmm0,%xmm1
pxor   %xmm0,%xmm2
movdqu %xmm1,(%rbx,%rsi,1)
movdqu %xmm2,0x10(%rbx,%rsi,1)
cmp    %rax,%rcx
je     12dd <tgc_rxtx+0x1ad>
test   $0x18,%al
je     12d0 <tgc_rxtx+0x1a0>
mov    %rcx,%rsi
mov    %eax,%ecx
and    $0xfffffff8,%ecx
movd   %edx,%xmm0
punpcklbw %xmm0,%xmm0
pshuflw $0x0,%xmm0,%xmm0
xchg   %ax,%ax
movq   (%rbx,%rsi,1),%xmm1
pxor   %xmm0,%xmm1
movq   %xmm1,(%rbx,%rsi,1)
add    $0x8,%rsi
cmp    %rsi,%rcx
jne    12b0 <tgc_rxtx+0x180>
cmp    %rax,%rcx
je     12dd <tgc_rxtx+0x1ad>
nopl   0x0(%rax)
xor    %r12b,(%rbx,%rcx,1)
add    $0x1,%rcx
cmp    %rcx,%rax
jne    12d0 <tgc_rxtx+0x1a0>
xor    %ebp,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    %r15d,%eax
jge    1313 <tgc_rxtx+0x1e3>
lea    0xd05(%rip),%rsi
lea    0xd2d(%rip),%rdx
mov    $0x3,%edi
xor    %eax,%eax
call   1050 <print_log_msg@plt>
mov    $0xfffffffb,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__FUNCTION__.tgc_rxtx = [9 x i8] c"tgc_rxtx\00"
@.str = [12 x i8] c"End of data\00"
@.str.1 = [26 x i8] c"Error reading from socket\00"
@.str.2 = [24 x i8] c"Error writing to socket\00"
define i32 @tgc_rxtx(i32 %0, i32 %1, i8* %2, i8 zeroext %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8 %3, i8* %9
store i32 0, i32* %10
store i32 0, i32* %11
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %4
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i8*, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %18, %15, %4
store i32 -3, i32* %5
br label %66
22: ; preds = %18
%23 = load i32, i32* %6
%24 = load i8*, i8** %8
%25 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %23, i8* %24, i32 4096)
store i32 %25, i32* %10
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @print_log_msg to i32 (i32, i8*, i8*, ...)*)(i32 5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.tgc_rxtx, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -6, i32* %5
br label %66
29: ; preds = %22
%30 = load i32, i32* %10
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @print_log_msg to i32 (i32, i8*, i8*, ...)*)(i32 3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.tgc_rxtx, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -4, i32* %5
br label %66
34: ; preds = %29
%35 = load i8, i8* %9
%36 = icmp ne i8 %35, 0
br i1 %36, label %37, label %56
37: ; preds = %34
store i32 0, i32* %12
br label %38
38: ; preds = %43, %37
%39 = load i32, i32* %12
%40 = load i32, i32* %10
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %55
42: ; preds = %38
br label %43
43: ; preds = %42
%44 = load i8, i8* %9
%45 = zext i8 %44 to i32
%46 = load i8*, i8** %8
%47 = load i32, i32* %12
%48 = add nsw i32 %47, 1
store i32 %48, i32* %12
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = xor i32 %52, %45
%54 = trunc i32 %53 to i8
store i8 %54, i8* %50
br label %38
55: ; preds = %38
br label %56
56: ; preds = %55, %34
%57 = load i32, i32* %7
%58 = load i8*, i8** %8
%59 = load i32, i32* %10
%60 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %57, i8* %58, i32 %59)
store i32 %60, i32* %11
%61 = load i32, i32* %10
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %56
%64 = call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @print_log_msg to i32 (i32, i8*, i8*, ...)*)(i32 3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.tgc_rxtx, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -5, i32* %5
br label %66
65: ; preds = %56
store i32 0, i32* %5
br label %66
66: ; preds = %65, %63, %32, %27, %21
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
@__FUNCTION__.tgc_rxtx = [9 x i8] c"tgc_rxtx\00"
@.str = [12 x i8] c"End of data\00"
@.str.1 = [26 x i8] c"Error reading from socket\00"
@.str.2 = [24 x i8] c"Error writing to socket\00"
define i32 @tgc_rxtx(i32 %0, i32 %1, i8* %2, i8 zeroext %3) {
%5 = or i32 %1, %0
%6 = icmp sgt i32 %5, -1
%7 = icmp ne i8* %2, null
%8 = and i1 %6, %7
br i1 %8, label %9, label %114
9: ; preds = %4
%10 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %0, i8* %2, i32 4096)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @print_log_msg to i32 (i32, i8*, i8*, ...)*)(i32 5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.tgc_rxtx, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %114
14: ; preds = %9
%15 = icmp slt i32 %10, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @print_log_msg to i32 (i32, i8*, i8*, ...)*)(i32 3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.tgc_rxtx, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %114
18: ; preds = %14
%19 = icmp eq i8 %3, 0
br i1 %19, label %109, label %20
20: ; preds = %18
%21 = zext i32 %10 to i64
%22 = icmp ult i32 %10, 8
br i1 %22, label %100, label %23
23: ; preds = %20
%24 = icmp ult i32 %10, 32
br i1 %24, label %84, label %25
25: ; preds = %23
%26 = and i64 %21, 4294967264
%27 = insertelement <16 x i8> poison, i8 %3, i64 0
%28 = shufflevector <16 x i8> %27, <16 x i8> poison, <16 x i32> zeroinitializer
%29 = insertelement <16 x i8> poison, i8 %3, i64 0
%30 = shufflevector <16 x i8> %29, <16 x i8> poison, <16 x i32> zeroinitializer
%31 = add nsw i64 %26, -32
%32 = lshr exact i64 %31, 5
%33 = add nuw nsw i64 %32, 1
%34 = and i64 %33, 1
%35 = icmp eq i64 %31, 0
br i1 %35, label %65, label %36
36: ; preds = %25
%37 = and i64 %33, 1152921504606846974
br label %38
38: ; preds = %38, %36
%39 = phi i64 [ 0, %36 ], [ %62, %38 ]
%40 = phi i64 [ 0, %36 ], [ %63, %38 ]
%41 = getelementptr inbounds i8, i8* %2, i64 %39
%42 = bitcast i8* %41 to <16 x i8>*
%43 = load <16 x i8>, <16 x i8>* %42
%44 = getelementptr inbounds i8, i8* %41, i64 16
%45 = bitcast i8* %44 to <16 x i8>*
%46 = load <16 x i8>, <16 x i8>* %45
%47 = xor <16 x i8> %43, %28
%48 = xor <16 x i8> %46, %30
%49 = bitcast i8* %41 to <16 x i8>*
store <16 x i8> %47, <16 x i8>* %49
%50 = bitcast i8* %44 to <16 x i8>*
store <16 x i8> %48, <16 x i8>* %50
%51 = or i64 %39, 32
%52 = getelementptr inbounds i8, i8* %2, i64 %51
%53 = bitcast i8* %52 to <16 x i8>*
%54 = load <16 x i8>, <16 x i8>* %53
%55 = getelementptr inbounds i8, i8* %52, i64 16
%56 = bitcast i8* %55 to <16 x i8>*
%57 = load <16 x i8>, <16 x i8>* %56
%58 = xor <16 x i8> %54, %28
%59 = xor <16 x i8> %57, %30
%60 = bitcast i8* %52 to <16 x i8>*
store <16 x i8> %58, <16 x i8>* %60
%61 = bitcast i8* %55 to <16 x i8>*
store <16 x i8> %59, <16 x i8>* %61
%62 = add nuw i64 %39, 64
%63 = add i64 %40, 2
%64 = icmp eq i64 %63, %37
br i1 %64, label %65, label %38
65: ; preds = %38, %25
%66 = phi i64 [ 0, %25 ], [ %62, %38 ]
%67 = icmp eq i64 %34, 0
br i1 %67, label %79, label %68
68: ; preds = %65
%69 = getelementptr inbounds i8, i8* %2, i64 %66
%70 = bitcast i8* %69 to <16 x i8>*
%71 = load <16 x i8>, <16 x i8>* %70
%72 = getelementptr inbounds i8, i8* %69, i64 16
%73 = bitcast i8* %72 to <16 x i8>*
%74 = load <16 x i8>, <16 x i8>* %73
%75 = xor <16 x i8> %71, %28
%76 = xor <16 x i8> %74, %30
%77 = bitcast i8* %69 to <16 x i8>*
store <16 x i8> %75, <16 x i8>* %77
%78 = bitcast i8* %72 to <16 x i8>*
store <16 x i8> %76, <16 x i8>* %78
br label %79
79: ; preds = %65, %68
%80 = icmp eq i64 %26, %21
br i1 %80, label %109, label %81
81: ; preds = %79
%82 = and i64 %21, 24
%83 = icmp eq i64 %82, 0
br i1 %83, label %100, label %84
84: ; preds = %23, %81
%85 = phi i64 [ %26, %81 ], [ 0, %23 ]
%86 = and i64 %21, 4294967288
%87 = insertelement <8 x i8> poison, i8 %3, i64 0
%88 = shufflevector <8 x i8> %87, <8 x i8> poison, <8 x i32> zeroinitializer
br label %89
89: ; preds = %89, %84
%90 = phi i64 [ %85, %84 ], [ %96, %89 ]
%91 = getelementptr inbounds i8, i8* %2, i64 %90
%92 = bitcast i8* %91 to <8 x i8>*
%93 = load <8 x i8>, <8 x i8>* %92
%94 = xor <8 x i8> %93, %88
%95 = bitcast i8* %91 to <8 x i8>*
store <8 x i8> %94, <8 x i8>* %95
%96 = add nuw i64 %90, 8
%97 = icmp eq i64 %96, %86
br i1 %97, label %98, label %89
98: ; preds = %89
%99 = icmp eq i64 %86, %21
br i1 %99, label %109, label %100
100: ; preds = %20, %81, %98
%101 = phi i64 [ 0, %20 ], [ %26, %81 ], [ %86, %98 ]
br label %102
102: ; preds = %100, %102
%103 = phi i64 [ %104, %102 ], [ %101, %100 ]
%104 = add nuw nsw i64 %103, 1
%105 = getelementptr inbounds i8, i8* %2, i64 %103
%106 = load i8, i8* %105
%107 = xor i8 %106, %3
store i8 %107, i8* %105
%108 = icmp eq i64 %104, %21
br i1 %108, label %109, label %102
109: ; preds = %102, %79, %98, %18
%110 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %1, i8* %2, i32 %10)
%111 = icmp slt i32 %110, %10
br i1 %111, label %112, label %114
112: ; preds = %109
%113 = tail call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @print_log_msg to i32 (i32, i8*, i8*, ...)*)(i32 3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.tgc_rxtx, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %114
114: ; preds = %109, %4, %112, %16, %12
%115 = phi i32 [ -6, %12 ], [ -4, %16 ], [ -5, %112 ], [ -3, %4 ], [ 0, %109 ]
ret i32 %115
}