#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 stat {
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
char *EAGAIN;
char *EWOULDBLOCK;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
char *errno;
int /*<<< orphan*/ error_log(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ sleep(int);
int stat(char *, struct stat *);
void *socket_check(int fd, void *arg) {
  struct stat st = {0};
  char *copy_path;
  copy_path = (char *)arg;
  int connfd;
  bool connReceived = false;
  for (;;) {
    if (!connReceived) {
      sleep(1);
    }
    if (stat(copy_path, &st) == -1) {
      error_log("socket %s does not exist anymore\n", copy_path);
      exit(0);
    }
    connfd = accept(fd, (struct sockaddr *)NULL, NULL);
    if (connfd == -1) {
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        connReceived = false;
        continue;
      } else {
        error_log("failed to accept connections on the socket: %d\n", errno);
        exit(1);
      }
    }
    connReceived = true;
    close(connfd);
  }
}


x86 assembly (clang, -O0):
<socket_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11b2 <socket_check+0x42>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    11df <socket_check+0x6f>
mov    -0x20(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1060 <error_log@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    125b <socket_check+0xeb>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
je     122f <socket_check+0xbf>
mov    0x2dc4(%rip),%rax
mov    (%rax),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
jne    123b <socket_check+0xcb>
movl   $0x0,-0x28(%rbp)
jmp    119e <socket_check+0x2e>
mov    0x2d9e(%rip),%rax
mov    (%rax),%rsi
lea    0xdd6(%rip),%rdi
call   1060 <error_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
jmp    119e <socket_check+0x2e>

x86 assembly (clang, -O2):
<socket_check>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,(%rsp)
mov    %rsp,%r14
mov    0x2e5f(%rip),%r15
mov    0x2e40(%rip),%r12
mov    0x2e49(%rip),%r13
nop
mov    $0x1,%edi
call   1080 <sleep@plt>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     11f5 <socket_check+0x95>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <accept@plt>
cmp    $0xffffffff,%eax
je     11d0 <socket_check+0x70>
mov    %eax,%edi
call   1030 <close@plt>
jmp    11a0 <socket_check+0x40>
nopl   0x0(%rax)
mov    (%r15),%rsi
cmp    (%r12),%rsi
je     1190 <socket_check+0x30>
cmp    0x0(%r13),%rsi
je     1190 <socket_check+0x30>
lea    0xe3c(%rip),%rdi
call   1050 <error_log@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe04(%rip),%rdi
mov    %rbx,%rsi
call   1050 <error_log@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [34 x i8] c"socket %s does not exist anymore\0A\00"
@errno = global i8* null
@EAGAIN = global i8* null
@EWOULDBLOCK = global i8* null
@.str.1 = [48 x i8] c"failed to accept connections on the socket: %d\0A\00"
define i8* @socket_check(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = bitcast %struct.stat* %5 to i8*
%10 = load i8*, i8** %4
store i8* %10, i8** %6
store i32 0, i32* %8
br label %11
11: ; preds = %42, %37, %2
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @sleep(i32 1)
br label %16
16: ; preds = %14, %11
%17 = load i8*, i8** %6
%18 = call i32 @stat(i8* %17, %struct.stat* %5)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i8*, i8** %6
%22 = call i32 @error_log(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %16
%25 = load i32, i32* %3
%26 = call i32 @accept(i32 %25, %struct.sockaddr* null, i32* null)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %42
29: ; preds = %24
%30 = load i8*, i8** @errno
%31 = load i8*, i8** @EAGAIN
%32 = icmp eq i8* %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i8*, i8** @errno
%35 = load i8*, i8** @EWOULDBLOCK
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33, %29
store i32 0, i32* %8
br label %11
38: ; preds = %33
%39 = load i8*, i8** @errno
%40 = call i32 @error_log(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %24
store i32 1, i32* %8
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
br label %11
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [34 x i8] c"socket %s does not exist anymore\0A\00"
@errno = global i8* null
@EAGAIN = global i8* null
@EWOULDBLOCK = global i8* null
@.str.1 = [48 x i8] c"failed to accept connections on the socket: %d\0A\00"
define noalias i8* @socket_check(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
store i32 0, i32* %5
br label %6
6: ; preds = %16, %2
%7 = tail call i32 @sleep(i32 1)
%8 = call i32 @stat(i8* %1, %struct.stat* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %6, %26
%11 = tail call i32 @error_log(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %1)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %6, %26
%14 = tail call i32 @accept(i32 %0, %struct.sockaddr* null, i32* null)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = load i8*, i8** @errno
%18 = load i8*, i8** @EAGAIN
%19 = icmp eq i8* %17, %18
%20 = load i8*, i8** @EWOULDBLOCK
%21 = icmp eq i8* %17, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %6, label %23
23: ; preds = %16
%24 = tail call i32 @error_log(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %13
%27 = tail call i32 @close(i32 %14)
%28 = call i32 @stat(i8* %1, %struct.stat* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %10, label %13
}