#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 st_mode;
};

/* Variables and functions */
scalar_t__ ENOENT;
int S_IFMT;
int S_IFSOCK;
scalar_t__ errno;
int stat(char const *, struct stat *);
int unlink(char const *);
int set_reuseaddr_unix(const char *path) {
  struct stat sb;
  errno = 0;
  if (stat(path, &sb) == -1) {
    if (errno == ENOENT)
      return 0;
    else
      return -1;
  }
  if ((sb.st_mode & S_IFMT) == S_IFSOCK) {
    return unlink(path);
  }
  return -1;
}


x86 assembly (clang, -O0):
<set_reuseaddr_unix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1182 <set_reuseaddr_unix+0x62>
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
jne    1176 <set_reuseaddr_unix+0x56>
movl   $0x0,-0x4(%rbp)
jmp    11b5 <set_reuseaddr_unix+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <set_reuseaddr_unix+0x95>
mov    -0x18(%rbp),%eax
mov    0x2e54(%rip),%rcx
and    (%rcx),%eax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%eax
jne    11ae <set_reuseaddr_unix+0x8e>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
jmp    11b5 <set_reuseaddr_unix+0x95>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_reuseaddr_unix>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2ea2(%rip),%r14
movq   $0x0,(%r14)
mov    %rsp,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     116d <set_reuseaddr_unix+0x4d>
mov    0x2e97(%rip),%rax
mov    (%rax),%ecx
and    (%rsp),%ecx
mov    0x2e83(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
jne    1181 <set_reuseaddr_unix+0x61>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <unlink@plt>
mov    (%r14),%rcx
mov    0x2e49(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@S_IFMT = global i32 0
@S_IFSOCK = global i32 0
define i32 @set_reuseaddr_unix(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
store i64 0, i64* @errno
%5 = load i8*, i8** %3
%6 = call i32 @stat(i8* %5, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %8
store i32 0, i32* %2
br label %25
13: ; preds = %8
store i32 -1, i32* %2
br label %25
14: ; preds = %1
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @S_IFMT
%18 = and i32 %16, %17
%19 = load i32, i32* @S_IFSOCK
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i8*, i8** %3
%23 = call i32 @unlink(i8* %22)
store i32 %23, i32* %2
br label %25
24: ; preds = %14
store i32 -1, i32* %2
br label %25
25: ; preds = %24, %21, %13, %12
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@S_IFMT = global i32 0
@S_IFSOCK = global i32 0
define i32 @set_reuseaddr_unix(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
store i64 0, i64* @errno
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp ne i64 %7, %8
%10 = sext i1 %9 to i32
br label %20
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IFMT
%15 = and i32 %14, %13
%16 = load i32, i32* @S_IFSOCK
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @unlink(i8* %0)
br label %20
20: ; preds = %11, %6, %18
%21 = phi i32 [ %19, %18 ], [ %10, %6 ], [ -1, %11 ]
ret i32 %21
}