#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 {
  int sa_family;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sockaddr;
typedef int /*<<< orphan*/ sa_family_t;

/* Variables and functions */
scalar_t__ getsockname(int, struct sockaddr *, int *);
int socket_family(int fd) {
  struct sockaddr sockaddr = {};
  socklen_t len = sizeof(sockaddr);
  if (getsockname(fd, &sockaddr, &len) < 0)
    return -1;
  if (len < sizeof(sa_family_t))
    return -1;
  return sockaddr.sa_family;
}


x86 assembly (clang, -O0):
<socket_family>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movl   $0x4,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <getsockname@plt>
cmp    $0x0,%rax
jge    1168 <socket_family+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <socket_family+0x68>
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jae    1182 <socket_family+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <socket_family+0x68>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_family>:
push   %rax
movl   $0x0,(%rsp)
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1030 <getsockname@plt>
cmpl   $0x4,0x4(%rsp)
mov    $0xffffffff,%edx
mov    $0xffffffff,%ecx
jb     1141 <socket_family+0x31>
mov    (%rsp),%ecx
test   %rax,%rax
cmovs  %edx,%ecx
mov    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
define i32 @socket_family(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr
%5 = alloca i32
store i32 %0, i32* %3
%6 = bitcast %struct.sockaddr* %4 to i8*
store i32 4, i32* %5
%7 = load i32, i32* %3
%8 = call i64 @getsockname(i32 %7, %struct.sockaddr* %4, i32* %5)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %19
11: ; preds = %1
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = icmp ult i64 %13, 4
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 -1, i32* %2
br label %19
16: ; preds = %11
%17 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i32 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %2
br label %19
19: ; preds = %16, %15, %10
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define i32 @socket_family(i32 %0) {
%2 = alloca %struct.sockaddr
%3 = alloca i32
%4 = bitcast %struct.sockaddr* %2 to i8*
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
store i32 0, i32* %5
%6 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%7 = call i64 @getsockname(i32 %0, %struct.sockaddr* %2, i32* %3)
%8 = icmp slt i64 %7, 0
%9 = load i32, i32* %3
%10 = icmp ult i32 %9, 4
%11 = select i1 %8, i1 true, i1 %10
%12 = load i32, i32* %5
%13 = select i1 %11, i32 -1, i32 %12
ret i32 %13
}