#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 scalar_t__ uint8_t;
typedef int uint16_t;

/* Variables and functions */
int SPI_Read(int);
int /*<<< orphan*/ SPI_Write(int, int);
int /*<<< orphan*/ close(scalar_t__);
uint8_t socket(uint8_t sock, uint8_t eth_protocol, uint16_t tcp_port) {
  uint8_t retval = 0;
  if (sock != 0)
    return retval;
  if (SPI_Read(0x0403) == 0x00) {
    close(sock);
  }
  SPI_Write(0x0400, eth_protocol);
  SPI_Write(0x0404, ((tcp_port & 0xFF00) >> 8));
  SPI_Write(0x0404 + 1, (tcp_port & 0x00FF));
  SPI_Write(0x0401, 0x01);
  while (SPI_Read(0x0401))
    ;
  if (SPI_Read(0x0403) == 0x13)
    retval = 1;
  else
    close(sock);
  return retval;
}


x86 assembly (clang, -O0):
<socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1163 <socket+0x33>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1210 <socket+0xe0>
mov    $0x403,%edi
call   1030 <SPI_Read@plt>
cmp    $0x0,%eax
jne    117f <socket+0x4f>
mov    -0x10(%rbp),%rdi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    $0x400,%edi
call   1050 <SPI_Write@plt>
mov    -0x1c(%rbp),%esi
and    $0xff00,%esi
sar    $0x8,%esi
mov    $0x404,%edi
call   1050 <SPI_Write@plt>
mov    -0x1c(%rbp),%esi
and    $0xff,%esi
mov    $0x405,%edi
call   1050 <SPI_Write@plt>
mov    $0x401,%edi
mov    $0x1,%esi
call   1050 <SPI_Write@plt>
mov    $0x401,%edi
call   1030 <SPI_Read@plt>
cmp    $0x0,%eax
je     11df <socket+0xaf>
jmp    11c7 <socket+0x97>
mov    $0x403,%edi
call   1030 <SPI_Read@plt>
cmp    $0x13,%eax
jne    11ff <socket+0xcf>
movq   $0x1,-0x28(%rbp)
jmp    1208 <socket+0xd8>
mov    -0x10(%rbp),%rdi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     113d <socket+0xd>
xor    %ebx,%ebx
jmp    11bb <socket+0x8b>
mov    %edx,%ebx
mov    %rsi,%r14
mov    $0x403,%edi
call   1030 <SPI_Read@plt>
test   %eax,%eax
jne    1157 <socket+0x27>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x400,%edi
mov    %r14d,%esi
call   1050 <SPI_Write@plt>
movzbl %bh,%esi
mov    $0x404,%edi
call   1050 <SPI_Write@plt>
movzbl %bl,%esi
mov    $0x405,%edi
call   1050 <SPI_Write@plt>
mov    $0x401,%edi
mov    $0x1,%esi
call   1050 <SPI_Write@plt>
nopl   (%rax)
mov    $0x401,%edi
call   1030 <SPI_Read@plt>
test   %eax,%eax
jne    1190 <socket+0x60>
mov    $0x403,%edi
call   1030 <SPI_Read@plt>
mov    $0x1,%ebx
cmp    $0x13,%eax
je     11bb <socket+0x8b>
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @socket(i64 %0, i64 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i64 %0, i64* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i64 0, i64* %8
%9 = load i64, i64* %5
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i64, i64* %8
store i64 %12, i64* %4
br label %44
13: ; preds = %3
%14 = call i32 @SPI_Read(i32 1027)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i64, i64* %5
%18 = call i32 @close(i64 %17)
br label %19
19: ; preds = %16, %13
%20 = load i64, i64* %6
%21 = trunc i64 %20 to i32
%22 = call i32 @SPI_Write(i32 1024, i32 %21)
%23 = load i32, i32* %7
%24 = and i32 %23, 65280
%25 = ashr i32 %24, 8
%26 = call i32 @SPI_Write(i32 1028, i32 %25)
%27 = load i32, i32* %7
%28 = and i32 %27, 255
%29 = call i32 @SPI_Write(i32 1029, i32 %28)
%30 = call i32 @SPI_Write(i32 1025, i32 1)
br label %31
31: ; preds = %34, %19
%32 = call i32 @SPI_Read(i32 1025)
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
br label %31
35: ; preds = %31
%36 = call i32 @SPI_Read(i32 1027)
%37 = icmp eq i32 %36, 19
br i1 %37, label %38, label %39
38: ; preds = %35
store i64 1, i64* %8
br label %42
39: ; preds = %35
%40 = load i64, i64* %5
%41 = call i32 @close(i64 %40)
br label %42
42: ; preds = %39, %38
%43 = load i64, i64* %8
store i64 %43, i64* %4
br label %44
44: ; preds = %42, %11
%45 = load i64, i64* %4
ret i64 %45
}

LLVM IR (-O2):
define i64 @socket(i64 %0, i64 %1, i32 %2) {
%4 = icmp eq i64 %0, 0
br i1 %4, label %5, label %27
5: ; preds = %3
%6 = tail call i32 @SPI_Read(i32 1027)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @close(i64 0)
br label %10
10: ; preds = %8, %5
%11 = trunc i64 %1 to i32
%12 = tail call i32 @SPI_Write(i32 1024, i32 %11)
%13 = lshr i32 %2, 8
%14 = and i32 %13, 255
%15 = tail call i32 @SPI_Write(i32 1028, i32 %14)
%16 = and i32 %2, 255
%17 = tail call i32 @SPI_Write(i32 1029, i32 %16)
%18 = tail call i32 @SPI_Write(i32 1025, i32 1)
br label %19
19: ; preds = %19, %10
%20 = tail call i32 @SPI_Read(i32 1025)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %19
22: ; preds = %19
%23 = tail call i32 @SPI_Read(i32 1027)
%24 = icmp eq i32 %23, 19
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 @close(i64 0)
br label %27
27: ; preds = %25, %22, %3
%28 = phi i64 [ 0, %3 ], [ 0, %25 ], [ 1, %22 ]
ret i64 %28
}