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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int tty_connect(char *path) {
  struct sockaddr_un sun;
  int connect_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  memset(&sun, 0, sizeof(struct sockaddr_un));
  sun.sun_family = AF_UNIX;
  strncpy(sun.sun_path, path, strlen(path));
  if (connect(connect_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
    perror("Cannot connect to socket");
    syslog(LOG_ERR, "Cannot connect to socket");
    exit(-1);
  }
  return connect_fd;
}


x86 assembly (clang, -O0):
<tty_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    122f <tty_connect+0xaf>
lea    0xdf5(%rip),%rdi
call   1070 <perror@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
call   1060 <syslog@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%r15d
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     11ff <tty_connect+0x7f>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%rbx
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <syslog@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str = [25 x i8] c"Cannot connect to socket\00"
@LOG_ERR = global i32 0
define i32 @tty_connect(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%9 = load i32, i32* @AF_UNIX
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i8*, i8** %2
%14 = load i8*, i8** %2
%15 = call i32 @strlen(i8* %14)
%16 = call i32 @strncpy(i32 %12, i8* %13, i32 %15)
%17 = load i32, i32* %4
%18 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%19 = call i64 @connect(i32 %17, %struct.sockaddr* %18, i32 8)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @LOG_ERR
%24 = call i32 @syslog(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %1
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str = [25 x i8] c"Cannot connect to socket\00"
@LOG_ERR = global i32 0
define i32 @tty_connect(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @PF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @strlen(i8* %0)
%13 = call i32 @strncpy(i32 %11, i8* %0, i32 %12)
%14 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%15 = call i64 @connect(i32 %6, %struct.sockaddr* %14, i32 8)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @LOG_ERR
%20 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %1
ret i32 %6
}