#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_alg {
  scalar_t__ salg_name;
  scalar_t__ salg_type;
  int /*<<< orphan*/ salg_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_ALG;
int /*<<< orphan*/ PF_ALG;
int SOCK_CLOEXEC;
int SOCK_SEQPACKET;
scalar_t__ bind(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ memset(struct sockaddr_alg *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char const *);
int mkcryptfd(const char *type, const char *name) {
  struct sockaddr_alg sa;
  int fd;
  fd = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
  if (fd < 0)
    return -errno;
  memset(&sa, 0, sizeof(sa));
  sa.salg_family = AF_ALG;
  strcpy((char *)sa.salg_type, type);
  strcpy((char *)sa.salg_name, name);
  if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
    return -errno;
  return fd;
}


x86 assembly (clang, -O0):
<mkcryptfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e87(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1192 <mkcryptfd+0x52>
xor    %eax,%eax
mov    0x2e48(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <mkcryptfd+0xbc>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11f6 <mkcryptfd+0xb6>
xor    %eax,%eax
mov    0x2de4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <mkcryptfd+0xbc>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkcryptfd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
mov    0x2e66(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11cd <mkcryptfd+0x8d>
mov    %eax,%ebx
lea    0x8(%rsp),%r12
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x10(%rsp),%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x18,%edx
call   1050 <bind@plt>
test   %rax,%rax
jns    11d8 <mkcryptfd+0x98>
mov    0x2dfc(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_alg = type { i64, i64, i32 }
%struct.sockaddr = type { i32 }
@PF_ALG = global i32 0
@SOCK_SEQPACKET = global i32 0
@SOCK_CLOEXEC = global i32 0
@errno = global i32 0
@AF_ALG = global i32 0
define i32 @mkcryptfd(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.sockaddr_alg
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i32, i32* @PF_ALG
%9 = load i32, i32* @SOCK_SEQPACKET
%10 = load i32, i32* @SOCK_CLOEXEC
%11 = or i32 %9, %10
%12 = call i32 @socket(i32 %8, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %3
br label %41
18: ; preds = %2
%19 = call i32 @memset(%struct.sockaddr_alg* %6, i32 0, i32 24)
%20 = load i32, i32* @AF_ALG
%21 = getelementptr inbounds %struct.sockaddr_alg, %struct.sockaddr_alg* %6, i32 0, i32 2
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_alg, %struct.sockaddr_alg* %6, i32 0, i32 1
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i8*
%25 = load i8*, i8** %4
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_alg, %struct.sockaddr_alg* %6, i32 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to i8*
%30 = load i8*, i8** %5
%31 = call i32 @strcpy(i8* %29, i8* %30)
%32 = load i32, i32* %7
%33 = bitcast %struct.sockaddr_alg* %6 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 24)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %18
%37 = load i32, i32* @errno
%38 = sub nsw i32 0, %37
store i32 %38, i32* %3
br label %41
39: ; preds = %18
%40 = load i32, i32* %7
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %36, %15
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_alg = type { i64, i64, i32 }
%struct.sockaddr = type { i32 }
@PF_ALG = global i32 0
@SOCK_SEQPACKET = global i32 0
@SOCK_CLOEXEC = global i32 0
@errno = global i32 0
@AF_ALG = global i32 0
define i32 @mkcryptfd(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_alg
%4 = bitcast %struct.sockaddr_alg* %3 to i8*
%5 = load i32, i32* @PF_ALG
%6 = load i32, i32* @SOCK_SEQPACKET
%7 = load i32, i32* @SOCK_CLOEXEC
%8 = or i32 %7, %6
%9 = tail call i32 @socket(i32 %5, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %32
14: ; preds = %2
%15 = call i32 @memset(%struct.sockaddr_alg* %3, i32 0, i32 24)
%16 = load i32, i32* @AF_ALG
%17 = getelementptr inbounds %struct.sockaddr_alg, %struct.sockaddr_alg* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_alg, %struct.sockaddr_alg* %3, i64 0, i32 1
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to i8*
%21 = call i32 @strcpy(i8* %20, i8* %0)
%22 = getelementptr inbounds %struct.sockaddr_alg, %struct.sockaddr_alg* %3, i64 0, i32 0
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i8*
%25 = call i32 @strcpy(i8* %24, i8* %1)
%26 = bitcast %struct.sockaddr_alg* %3 to %struct.sockaddr*
%27 = call i64 @bind(i32 %9, %struct.sockaddr* %26, i32 24)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %14
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
br label %32
32: ; preds = %14, %29, %11
%33 = phi i32 [ %13, %11 ], [ %31, %29 ], [ %9, %14 ]
ret i32 %33
}