#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;
};
struct sockaddr_storage {
  scalar_t__ ss_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
scalar_t__ AF_UNIX;
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ *strstr(int /*<<< orphan*/, char *);
bool is_secrets_socket(int fd) {
  int ret;
  struct sockaddr_storage addr = {0};
  socklen_t addrlen = sizeof(addr);
  struct sockaddr_un *unix_socket;
  ret = getsockname(fd, (struct sockaddr *)&addr, &addrlen);
  if (ret != 0)
    return false;
  if (addr.ss_family != AF_UNIX)
    return false;
  unix_socket = (struct sockaddr_un *)&addr;
  return NULL != strstr(unix_socket->sun_path, "secrets.socket");
}


x86 assembly (clang, -O0):
<is_secrets_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movl   $0x10,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <getsockname@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     117b <is_secrets_socket+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    11c8 <is_secrets_socket+0x98>
mov    -0x20(%rbp),%rax
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
je     119b <is_secrets_socket+0x6b>
movl   $0x0,-0x4(%rbp)
jmp    11c8 <is_secrets_socket+0x98>
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_secrets_socket>:
sub    $0x28,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x10,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1030 <getsockname@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
je     1150 <is_secrets_socket+0x30>
add    $0x28,%rsp
ret
mov    0x10(%rsp),%rdi
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%rdi
jne    114b <is_secrets_socket+0x2b>
lea    0xe98(%rip),%rsi
call   1040 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64, i32 }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i64 0
@.str = [15 x i8] c"secrets.socket\00"
define i32 @is_secrets_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = alloca %struct.sockaddr_un*
store i32 %0, i32* %3
%8 = bitcast %struct.sockaddr_storage* %5 to i8*
store i32 16, i32* %6
%9 = load i32, i32* %3
%10 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%11 = call i32 @getsockname(i32 %9, %struct.sockaddr* %10, i32* %6)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 0, i32* %2
br label %29
15: ; preds = %1
%16 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %5, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @AF_UNIX
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 0, i32* %2
br label %29
21: ; preds = %15
%22 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_un*
store %struct.sockaddr_un* %22, %struct.sockaddr_un** %7
%23 = load %struct.sockaddr_un*, %struct.sockaddr_un** %7
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32* @strstr(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%27 = icmp ne i32* null, %26
%28 = zext i1 %27 to i32
store i32 %28, i32* %2
br label %29
29: ; preds = %21, %20, %14
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i64 0
@.str = [15 x i8] c"secrets.socket\00"
define i32 @is_secrets_socket(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_storage* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 16, i32* %3
%6 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%7 = call i32 @getsockname(i32 %0, %struct.sockaddr* %6, i32* %3)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %1
%10 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = load i64, i64* @AF_UNIX
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = trunc i64 %11 to i32
%16 = call i32* @strstr(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%17 = icmp ne i32* %16, null
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %9, %1, %14
%20 = phi i32 [ %18, %14 ], [ 0, %1 ], [ 0, %9 ]
ret i32 %20
}