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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int EINVAL;
int /*<<< orphan*/ SOCK_STREAM;
size_t SUN_LEN(struct sockaddr_un *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, size_t);
int errno;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
FILE *connect_daemon(const char *name) {
  int sock;
  struct sockaddr_un addr_un;
  struct sockaddr *addrp;
  size_t addrlen;
  FILE *fp;
  if (strlen(name) + 1 >= sizeof addr_un.sun_path) {
    errno = EINVAL;
    return NULL;
  }
  memset(&addr_un, 0, sizeof addr_un);
  addr_un.sun_family = AF_LOCAL;
  strncpy(addr_un.sun_path, name, sizeof(addr_un.sun_path) - 1);
  addr_un.sun_path[sizeof(addr_un.sun_path) - 1] = 0;
  addrlen = SUN_LEN(&addr_un);
  addrp = (struct sockaddr *)&addr_un;
  sock = socket(AF_LOCAL, SOCK_STREAM, 0);
  if (sock == -1)
    return NULL;
  if (connect(sock, addrp, addrlen)) {
    int saveerr = errno;
    close(sock);
    errno = saveerr;
    return NULL;
  }
  fp = fdopen(sock, "r+b");
  if (!fp) {
    int saveerr = errno;
    close(sock);
    errno = saveerr;
    return NULL;
  }
  return fp;
}


x86 assembly (clang, -O0):
<connect_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    $0x8,%rax
jb     11c3 <connect_daemon+0x43>
mov    0x2e1d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1c(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12df <connect_daemon+0x15f>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movq   $0x0,0x38(%rax)
lea    -0x28(%rbp),%rdi
call   1090 <SUN_LEN@plt>
mov    %rax,-0x38(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1245 <connect_daemon+0xc5>
movq   $0x0,-0x8(%rbp)
jmp    12df <connect_daemon+0x15f>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1080 <connect@plt>
cmp    $0x0,%rax
je     128c <connect_daemon+0x10c>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x44(%rbp),%ecx
mov    0x2d53(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12df <connect_daemon+0x15f>
mov    -0x14(%rbp),%edi
lea    0xd6a(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12d7 <connect_daemon+0x157>
mov    0x2d1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%ecx
mov    0x2d08(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12df <connect_daemon+0x15f>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_daemon>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
call   1040 <strlen@plt>
add    $0xfffffff9,%eax
cmp    $0xfffffff7,%eax
ja     11a8 <connect_daemon+0x28>
mov    0x2e27(%rip),%rax
mov    (%rax),%ebx
jmp    124a <connect_daemon+0xca>
lea    0x8(%rsp),%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e23(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    0x8(%rsp),%rax
movq   $0x0,0x38(%rax)
mov    %r14,%rdi
call   1090 <SUN_LEN@plt>
mov    %rax,%r14
mov    (%r15),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1255 <connect_daemon+0xd5>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    %r14,%rdx
call   1080 <connect@plt>
test   %rax,%rax
jne    123a <connect_daemon+0xba>
lea    0xdd5(%rip),%rsi
mov    %ebp,%edi
call   1070 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1255 <connect_daemon+0xd5>
mov    0x2d8f(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d7f(%rip),%rax
mov    %ebx,(%rax)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [4 x i8] c"r+b\00"
define i32* @connect_daemon(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca %struct.sockaddr*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = icmp uge i64 %14, 8
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32* null, i32** %2
br label %60
18: ; preds = %1
%19 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 16)
%20 = load i32, i32* @AF_LOCAL
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%23 = load i64*, i64** %22
%24 = load i8*, i8** %3
%25 = call i32 @strncpy(i64* %23, i8* %24, i32 7)
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%27 = load i64*, i64** %26
%28 = getelementptr inbounds i64, i64* %27, i64 7
store i64 0, i64* %28
%29 = call i64 @SUN_LEN(%struct.sockaddr_un* %5)
store i64 %29, i64* %7
%30 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
store %struct.sockaddr* %30, %struct.sockaddr** %6
%31 = load i32, i32* @AF_LOCAL
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %18
store i32* null, i32** %2
br label %60
37: ; preds = %18
%38 = load i32, i32* %4
%39 = load %struct.sockaddr*, %struct.sockaddr** %6
%40 = load i64, i64* %7
%41 = call i64 @connect(i32 %38, %struct.sockaddr* %39, i64 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* @errno
store i32 %44, i32* %9
%45 = load i32, i32* %4
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %9
store i32 %47, i32* @errno
store i32* null, i32** %2
br label %60
48: ; preds = %37
%49 = load i32, i32* %4
%50 = call i32* @fdopen(i32 %49, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %50, i32** %8
%51 = load i32*, i32** %8
%52 = icmp ne i32* %51, null
br i1 %52, label %58, label %53
53: ; preds = %48
%54 = load i32, i32* @errno
store i32 %54, i32* %10
%55 = load i32, i32* %4
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %10
store i32 %57, i32* @errno
store i32* null, i32** %2
br label %60
58: ; preds = %48
%59 = load i32*, i32** %8
store i32* %59, i32** %2
br label %60
60: ; preds = %58, %53, %43, %36, %16
%61 = load i32*, i32** %2
ret i32* %61
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [4 x i8] c"r+b\00"
define noalias i32* @connect_daemon(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = add i32 %4, -7
%6 = icmp ult i32 %5, -8
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i32, i32* @EINVAL
br label %36
9: ; preds = %1
%10 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%11 = load i32, i32* @AF_LOCAL
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%14 = load i64*, i64** %13
%15 = call i32 @strncpy(i64* %14, i8* %0, i32 7)
%16 = load i64*, i64** %13
%17 = getelementptr inbounds i64, i64* %16, i64 7
store i64 0, i64* %17
%18 = call i64 @SUN_LEN(%struct.sockaddr_un* %2)
%19 = load i32, i32* @AF_LOCAL
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %38, label %23
23: ; preds = %9
%24 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%25 = call i64 @connect(i32 %21, %struct.sockaddr* %24, i64 %18)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = call i32 @close(i32 %21)
br label %36
30: ; preds = %23
%31 = call i32* @fdopen(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = call i32 @close(i32 %21)
br label %36
36: ; preds = %7, %27, %33
%37 = phi i32 [ %34, %33 ], [ %28, %27 ], [ %8, %7 ]
store i32 %37, i32* @errno
br label %38
38: ; preds = %36, %30, %9
%39 = phi i32* [ null, %9 ], [ %31, %30 ], [ null, %36 ]
ret i32* %39
}