#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 */
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ MSG_PEEK;
scalar_t__ errno;
scalar_t__ getpeername(int, struct sockaddr *, int *);
int ip2str(struct sockaddr_in *);
int /*<<< orphan*/ log_msg(char *, int, int, scalar_t__, char *, int);
int recv(int, unsigned char *, int, int /*<<< orphan*/);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/, struct timeval *);
int socket_is_usable(int sock) {
  fd_set testset;
  struct timeval delay;
  unsigned char buf[1];
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);
  int size;
  if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)) {
    log_msg(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c",
        131, 1, errno, "getpeername %s", ip2str(&addr));
    return -1;
  }
  FD_ZERO(&testset);
  FD_SET(sock, &testset);
  delay.tv_sec = 0;
  delay.tv_usec = 0;
  if (select(sock + 1, 0, &testset, 0, &delay) <= 0) {
    log_msg(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c",
        140, 1, 0, "socket %d is not writable", sock);
    return -1;
  }
  FD_ZERO(&testset);
  FD_SET(sock, &testset);
  delay.tv_sec = 0;
  delay.tv_usec = 0;
  if (select(sock + 1, &testset, 0, 0, &delay) > 0) {
    log_msg(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c",
        149, 4, 0, "socket %d is readable, checking for EOF", sock);
    errno = 0;
    if ((size = recv(sock, buf, sizeof(buf), MSG_PEEK)) < 0) {
      log_msg("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/"
              "share.c",
              152, 1, errno, "socket %d error", sock);
      return -1;
    } else if (size == 0) {
      log_msg("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/"
              "share.c",
              156, 1, errno, "socket %d has immediate EOF", sock);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<socket_is_usable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1040 <getpeername@plt>
cmp    $0x0,%rax
je     11e3 <socket_is_usable+0x73>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <ip2str@plt>
mov    -0x38(%rbp),%rcx
mov    %eax,%r9d
lea    0xe3f(%rip),%rdi
mov    $0x83,%esi
mov    $0x1,%edx
lea    0xeb4(%rip),%r8
call   1080 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <socket_is_usable+0x216>
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0xc(%rbp),%rdx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
call   1070 <select@plt>
cmp    $0x0,%rax
jg     125c <socket_is_usable+0xec>
mov    -0x8(%rbp),%r9d
lea    0xdca(%rip),%rdi
mov    $0x8c,%esi
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    0xe4a(%rip),%r8
call   1080 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <socket_is_usable+0x216>
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
call   1070 <select@plt>
cmp    $0x0,%rax
jle    137f <socket_is_usable+0x20f>
mov    -0x8(%rbp),%r9d
lea    0xd51(%rip),%rdi
mov    $0x95,%esi
mov    $0x4,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    0xdeb(%rip),%r8
call   1080 <log_msg@plt>
mov    0x2d08(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    0x2d03(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   1030 <recv@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    1334 <socket_is_usable+0x1c4>
mov    0x2cd4(%rip),%rax
mov    (%rax),%rcx
mov    -0x8(%rbp),%r9d
lea    0xcee(%rip),%rdi
mov    $0x98,%esi
mov    $0x1,%edx
lea    0xdb4(%rip),%r8
call   1080 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <socket_is_usable+0x216>
cmpl   $0x0,-0x30(%rbp)
jne    1375 <socket_is_usable+0x205>
mov    0x2c93(%rip),%rax
mov    (%rax),%rcx
mov    -0x8(%rbp),%r9d
lea    0xcad(%rip),%rdi
mov    $0x9c,%esi
mov    $0x1,%edx
lea    0xd83(%rip),%r8
call   1080 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <socket_is_usable+0x216>
jmp    137a <socket_is_usable+0x20a>
jmp    137f <socket_is_usable+0x20f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_is_usable>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edi,%r14d
movl   $0x4,0x18(%rsp)
lea    0x10(%rsp),%rsi
lea    0x18(%rsp),%rdx
call   1040 <getpeername@plt>
test   %rax,%rax
je     11d0 <socket_is_usable+0x60>
mov    0x2e38(%rip),%rax
mov    (%rax),%rbx
lea    0x10(%rsp),%rdi
call   1050 <ip2str@plt>
lea    0xe5c(%rip),%rdi
lea    0xedb(%rip),%r8
mov    $0x83,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
mov    %eax,%r9d
jmp    12f8 <socket_is_usable+0x188>
lea    0x1c(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
lea    0x1(%r14),%r15d
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
xor    %ebx,%ebx
lea    0x20(%rsp),%r8
mov    %r15d,%edi
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %rax,%rax
jle    12bd <socket_is_usable+0x14d>
lea    0x1c(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
lea    0x20(%rsp),%r8
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %rax,%rax
jle    1302 <socket_is_usable+0x192>
lea    0xdb9(%rip),%rdi
lea    0xe61(%rip),%r8
xor    %ebx,%ebx
mov    $0x95,%esi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    %r14d,%r9d
call   1080 <log_msg@plt>
mov    0x2d5d(%rip),%rbp
movq   $0x0,0x0(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%ecx
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <recv@plt>
test   %eax,%eax
js     12d9 <socket_is_usable+0x169>
jne    1302 <socket_is_usable+0x192>
mov    0x0(%rbp),%rcx
lea    0xd61(%rip),%rdi
lea    0xe41(%rip),%r8
mov    $0x9c,%esi
jmp    12f0 <socket_is_usable+0x180>
lea    0xd4c(%rip),%rdi
lea    0xdda(%rip),%r8
mov    $0x8c,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
jmp    12f5 <socket_is_usable+0x185>
mov    0x0(%rbp),%rcx
lea    0xd2c(%rip),%rdi
lea    0xdfc(%rip),%r8
mov    $0x98,%esi
mov    $0x1,%edx
mov    %r14d,%r9d
call   1080 <log_msg@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [134 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c\00"
@errno = global i64 0
@.str.1 = [15 x i8] c"getpeername %s\00"
@.str.2 = [26 x i8] c"socket %d is not writable\00"
@.str.3 = [40 x i8] c"socket %d is readable, checking for EOF\00"
@MSG_PEEK = global i32 0
@.str.4 = [16 x i8] c"socket %d error\00"
@.str.5 = [28 x i8] c"socket %d has immediate EOF\00"
define i32 @socket_is_usable(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca [1 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %8
%10 = load i32, i32* %3
%11 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%12 = call i64 @getpeername(i32 %10, %struct.sockaddr* %11, i32* %8)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i64, i64* @errno
%16 = call i32 @ip2str(%struct.sockaddr_in* %7)
%17 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 131, i32 1, i64 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 -1, i32* %2
br label %63
18: ; preds = %1
%19 = call i32 @FD_ZERO(i32* %4)
%20 = load i32, i32* %3
%21 = call i32 @FD_SET(i32 %20, i32* %4)
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i64 0, i64* %23
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
%26 = call i64 @select(i32 %25, i32* null, i32* %4, i32 0, %struct.timeval* %5)
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %18
%29 = load i32, i32* %3
%30 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 140, i32 1, i64 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %29)
store i32 -1, i32* %2
br label %63
31: ; preds = %18
%32 = call i32 @FD_ZERO(i32* %4)
%33 = load i32, i32* %3
%34 = call i32 @FD_SET(i32 %33, i32* %4)
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %35
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i64 0, i64* %36
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 1
%39 = call i64 @select(i32 %38, i32* %4, i32* null, i32 0, %struct.timeval* %5)
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %62
41: ; preds = %31
%42 = load i32, i32* %3
%43 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 149, i32 4, i64 0, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %42)
store i64 0, i64* @errno
%44 = load i32, i32* %3
%45 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%46 = load i32, i32* @MSG_PEEK
%47 = call i32 @recv(i32 %44, i8* %45, i32 1, i32 %46)
store i32 %47, i32* %9
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = load i64, i64* @errno
%51 = load i32, i32* %3
%52 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 152, i32 1, i64 %50, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %51)
store i32 -1, i32* %2
br label %63
53: ; preds = %41
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i64, i64* @errno
%58 = load i32, i32* %3
%59 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 156, i32 1, i64 %57, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %58)
store i32 -1, i32* %2
br label %63
60: ; preds = %53
br label %61
61: ; preds = %60
br label %62
62: ; preds = %61, %31
store i32 0, i32* %2
br label %63
63: ; preds = %62, %56, %49, %28, %14
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [134 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/bierdaci/egui/refs/heads/master/msg/share.c\00"
@errno = global i64 0
@.str.1 = [15 x i8] c"getpeername %s\00"
@.str.2 = [26 x i8] c"socket %d is not writable\00"
@.str.3 = [40 x i8] c"socket %d is readable, checking for EOF\00"
@MSG_PEEK = global i32 0
@.str.4 = [16 x i8] c"socket %d error\00"
@.str.5 = [28 x i8] c"socket %d has immediate EOF\00"
define i32 @socket_is_usable(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca [1 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast i32* %2 to i8*
%8 = bitcast %struct.timeval* %3 to i8*
%9 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%10 = bitcast %struct.sockaddr_in* %5 to i8*
%11 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%12 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%13 = call i64 @getpeername(i32 %0, %struct.sockaddr* %12, i32* %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = call i32 @ip2str(%struct.sockaddr_in* %5)
%18 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 131, i32 1, i64 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %47
19: ; preds = %1
%20 = call i32 @FD_ZERO(i32* %2)
%21 = call i32 @FD_SET(i32 %0, i32* %2)
%22 = add nsw i32 %0, 1
%23 = bitcast %struct.timeval* %3 to i8*
%24 = call i64 @select(i32 %22, i32* null, i32* %2, i32 0, %struct.timeval* %3)
%25 = icmp slt i64 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 140, i32 1, i64 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %47
28: ; preds = %19
%29 = call i32 @FD_ZERO(i32* %2)
%30 = call i32 @FD_SET(i32 %0, i32* %2)
%31 = bitcast %struct.timeval* %3 to i8*
%32 = call i64 @select(i32 %22, i32* %2, i32* null, i32 0, %struct.timeval* %3)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %28
%35 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 149, i32 4, i64 0, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %0)
store i64 0, i64* @errno
%36 = load i32, i32* @MSG_PEEK
%37 = call i32 @recv(i32 %0, i8* %9, i32 1, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i64, i64* @errno
%41 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 152, i32 1, i64 %40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %0)
br label %47
42: ; preds = %34
%43 = icmp eq i32 %37, 0
br i1 %43, label %44, label %47
44: ; preds = %42
%45 = load i64, i64* @errno
%46 = call i32 @log_msg(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0), i32 156, i32 1, i64 %45, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %0)
br label %47
47: ; preds = %28, %42, %44, %39, %26, %15
%48 = phi i32 [ -1, %15 ], [ -1, %26 ], [ -1, %39 ], [ -1, %44 ], [ 0, %42 ], [ 0, %28 ]
ret i32 %48
}