#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 sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ ENOENT;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
void nonexisting_socket(void) {
  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    err(1, "nonexisting-socket creation failed");
  }
  struct sockaddr_un address;
  address.sun_family = AF_UNIX;
  strncpy(address.sun_path, "/var/lib/sss/nonexisting/nonexisting",
          sizeof(address.sun_path));
  int ret = connect(sock, (struct sockaddr *)&address, sizeof(address));
  if (ret == 0 || errno != ENOENT) {
    err(1, "nonexisting-socket connect didn't fail with ENOENT");
  }
}


x86 assembly (clang, -O0):
<nonexisting_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    117f <nonexisting_socket+0x3f>
mov    $0x1,%edi
lea    0xe86(%rip),%rsi
call   1040 <err@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe8e(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11d7 <nonexisting_socket+0x97>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
je     11e8 <nonexisting_socket+0xa8>
mov    $0x1,%edi
lea    0xe65(%rip),%rsi
call   1040 <err@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nonexisting_socket>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%r14
mov    (%r14),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1175 <nonexisting_socket+0x35>
lea    0xe95(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
lea    0xea0(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
test   %eax,%eax
je     11b6 <nonexisting_socket+0x76>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
je     11c7 <nonexisting_socket+0x87>
lea    0xe8b(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [35 x i8] c"nonexisting-socket creation failed\00"
@.str.1 = [37 x i8] c"/var/lib/sss/nonexisting/nonexisting\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [51 x i8] c"nonexisting-socket connect didn't fail with ENOENT\00"
define void @nonexisting_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @err(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strncpy(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 4)
%17 = load i32, i32* %1
%18 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%19 = call i32 @connect(i32 %17, %struct.sockaddr* %18, i32 8)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %11
%23 = load i64, i64* @errno
%24 = load i64, i64* @ENOENT
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22, %11
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [35 x i8] c"nonexisting-socket creation failed\00"
@.str.1 = [37 x i8] c"/var/lib/sss/nonexisting/nonexisting\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [51 x i8] c"nonexisting-socket connect didn't fail with ENOENT\00"
define void @nonexisting_socket() {
%1 = alloca %struct.sockaddr_un
%2 = load i32, i32* @AF_UNIX
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = bitcast %struct.sockaddr_un* %1 to i8*
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 4)
%13 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%14 = call i32 @connect(i32 %4, %struct.sockaddr* %13, i32 8)
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %8
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %22, label %20
20: ; preds = %16, %8
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
ret void
}