#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 */

/* Variables and functions */
int saferead(int, char *, int);
int safewrite(int, char *, int);
int xsocket_start_sf(int fd) {
  int platform = 0;
  char check[2];
  char nonce[6];
  nonce[0] = 'T';
  nonce[1] = ' ';
  nonce[2] = (char)(platform & 0xff);
  nonce[3] = (char)((platform >> 8) & 0xff);
  nonce[4] = (char)((platform >> 16) & 0xff);
  nonce[5] = (char)((platform >> 24) & 0xff);
  if (safewrite(fd, nonce, 6) != 6 || saferead(fd, check, 6) != 6 ||
      check[0] != 'T' || check[1] < ' ') {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<xsocket_start_sf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movb   $0x54,-0x14(%rbp)
movb   $0x20,-0x13(%rbp)
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x12(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x11(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    %al,-0xf(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x6,%edx
call   1030 <safewrite@plt>
cmp    $0x6,%eax
jne    11bd <xsocket_start_sf+0x9d>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x6,%edx
call   1040 <saferead@plt>
cmp    $0x6,%eax
jne    11bd <xsocket_start_sf+0x9d>
movsbl -0xe(%rbp),%eax
cmp    $0x54,%eax
jne    11bd <xsocket_start_sf+0x9d>
movsbl -0xd(%rbp),%eax
cmp    $0x20,%eax
jge    11c9 <xsocket_start_sf+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <xsocket_start_sf+0xb0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xsocket_start_sf>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movw   $0x2054,0xa(%rsp)
movl   $0x0,0xc(%rsp)
lea    0xa(%rsp),%rsi
mov    $0x6,%edx
call   1030 <safewrite@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x6,%ecx
jne    1181 <xsocket_start_sf+0x61>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1040 <saferead@plt>
cmp    $0x6,%eax
setne  %al
cmpb   $0x54,0x8(%rsp)
setne  %cl
or     %al,%cl
cmpb   $0x20,0x9(%rsp)
setl   %al
or     %cl,%al
movzbl %al,%eax
neg    %eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @xsocket_start_sf(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i8]
%6 = alloca [6 x i8]
store i32 %0, i32* %3
store i32 0, i32* %4
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
store i8 84, i8* %7
%8 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 1
store i8 32, i8* %8
%9 = load i32, i32* %4
%10 = and i32 %9, 255
%11 = trunc i32 %10 to i8
%12 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 2
store i8 %11, i8* %12
%13 = load i32, i32* %4
%14 = ashr i32 %13, 8
%15 = and i32 %14, 255
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 3
store i8 %16, i8* %17
%18 = load i32, i32* %4
%19 = ashr i32 %18, 16
%20 = and i32 %19, 255
%21 = trunc i32 %20 to i8
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 4
store i8 %21, i8* %22
%23 = load i32, i32* %4
%24 = ashr i32 %23, 24
%25 = and i32 %24, 255
%26 = trunc i32 %25 to i8
%27 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 5
store i8 %26, i8* %27
%28 = load i32, i32* %3
%29 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%30 = call i32 @safewrite(i32 %28, i8* %29, i32 6)
%31 = icmp ne i32 %30, 6
br i1 %31, label %47, label %32
32: ; preds = %1
%33 = load i32, i32* %3
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%35 = call i32 @saferead(i32 %33, i8* %34, i32 6)
%36 = icmp ne i32 %35, 6
br i1 %36, label %47, label %37
37: ; preds = %32
%38 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 84
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp slt i32 %45, 32
br i1 %46, label %47, label %48
47: ; preds = %42, %37, %32, %1
store i32 -1, i32* %2
br label %49
48: ; preds = %42
store i32 0, i32* %2
br label %49
49: ; preds = %48, %47
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
define i32 @xsocket_start_sf(i32 %0) {
%2 = alloca [2 x i8]
%3 = alloca [6 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
store i8 84, i8* %5
%6 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 1
store i8 32, i8* %6
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 2
%8 = bitcast i8* %7 to i32*
store i32 0, i32* %8
%9 = call i32 @safewrite(i32 %0, i8* %5, i32 6)
%10 = icmp eq i32 %9, 6
br i1 %10, label %11, label %22
11: ; preds = %1
%12 = call i32 @saferead(i32 %0, i8* %4, i32 6)
%13 = icmp ne i32 %12, 6
%14 = load i8, i8* %4
%15 = icmp ne i8 %14, 84
%16 = select i1 %13, i1 true, i1 %15
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
%18 = load i8, i8* %17
%19 = icmp slt i8 %18, 32
%20 = select i1 %16, i1 true, i1 %19
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %11, %1
%23 = phi i32 [ -1, %1 ], [ %21, %11 ]
ret i32 %23
}