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

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
               int /*<<< orphan*/ *);
int socket_check(int fd) {
  int ret;
  int code;
  size_t len = sizeof(int);
  ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &code, (socklen_t *)&len);
  if ((ret || code) != 0)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<socket_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x4,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ebb(%rip),%rax
mov    (%rax),%esi
mov    0x2ea2(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%r8
lea    -0x10(%rbp),%rcx
call   1030 <getsockopt@plt>
mov    %eax,-0xc(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x19(%rbp)
jne    1161 <socket_check+0x51>
cmpl   $0x0,-0x10(%rbp)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
cmp    $0x0,%eax
je     117e <socket_check+0x6e>
movl   $0x1,-0x4(%rbp)
jmp    1185 <socket_check+0x75>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_check>:
sub    $0x18,%rsp
movq   $0x4,0x10(%rsp)
mov    0x2ec4(%rip),%rax
mov    (%rax),%esi
mov    0x2eab(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1030 <getsockopt@plt>
xor    %ecx,%ecx
or     0xc(%rsp),%eax
setne  %cl
mov    %ecx,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @socket_check(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i32 %0, i32* %3
store i64 4, i64* %6
%7 = load i32, i32* %3
%8 = load i32, i32* @SOL_SOCKET
%9 = load i32, i32* @SO_ERROR
%10 = bitcast i64* %6 to i32*
%11 = call i32 @getsockopt(i32 %7, i32 %8, i32 %9, i32* %5, i32* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br label %17
17: ; preds = %14, %1
%18 = phi i1 [ true, %1 ], [ %16, %14 ]
%19 = zext i1 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 1, i32* %2
br label %23
22: ; preds = %17
store i32 0, i32* %2
br label %23
23: ; preds = %22, %21
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @socket_check(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = bitcast i32* %2 to i8*
%5 = bitcast i64* %3 to i8*
store i64 4, i64* %3
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_ERROR
%8 = bitcast i64* %3 to i32*
%9 = call i32 @getsockopt(i32 %0, i32 %6, i32 %7, i32* %2, i32* %8)
%10 = icmp ne i32 %9, 0
%11 = load i32, i32* %2
%12 = icmp ne i32 %11, 0
%13 = select i1 %10, i1 true, i1 %12
%14 = zext i1 %13 to i32
ret i32 %14
}