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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, scalar_t__);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ remove(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int connect_to_server(const char sock_name[]) {
  int sock;
  char lockfile[] = "/tmp/ndsctl.lock";
  struct sockaddr_un sa_un;
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  memset(&sa_un, 0, sizeof(sa_un));
  sa_un.sun_family = AF_UNIX;
  strncpy(sa_un.sun_path, sock_name, (sizeof(sa_un.sun_path) - 1));
  if (connect(sock, (struct sockaddr *)&sa_un,
              strlen(sa_un.sun_path) + sizeof(sa_un.sun_family))) {
    fprintf(stderr, "ndsctl: nodogsplash probably not started (Error: %s)\n",
            strerror(errno));
    remove(lockfile);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xe6d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe68(%rip),%al
mov    %al,-0x20(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%edi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
add    $0x4,%rdx
call   1080 <connect@plt>
cmp    $0x0,%rax
je     1268 <connect_to_server+0xe8>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xdc3(%rip),%rsi
call   1070 <fprintf@plt>
lea    -0x30(%rbp),%rdi
call   1040 <remove@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <connect_to_server+0xee>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
movaps 0xe6b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0x20(%rsp)
mov    0x2e42(%rip),%r12
mov    (%r12),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x8(%rsp),%edi
call   1050 <strlen@plt>
lea    0x4(%rax),%rdx
mov    %ebx,%edi
mov    %r15,%rsi
call   1080 <connect@plt>
test   %rax,%rax
je     123c <connect_to_server+0xbc>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ebx
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdee(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
lea    0x10(%rsp),%rdi
call   1040 <remove@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.connect_to_server.lockfile = [17 x i8] c"/tmp/ndsctl.lock\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"ndsctl: nodogsplash probably not started (Error: %s)\0A\00"
@errno = global i32 0
define i32 @connect_to_server(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [17 x i8]
%6 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%7 = bitcast [17 x i8]* %5 to i8*
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %3
%17 = call i32 @strncpy(i32 %15, i8* %16, i32 3)
%18 = load i32, i32* %4
%19 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @strlen(i32 %21)
%23 = add i64 %22, 4
%24 = call i64 @connect(i32 %18, %struct.sockaddr* %19, i64 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %1
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%32 = call i32 @remove(i8* %31)
store i32 -1, i32* %2
br label %35
33: ; preds = %1
%34 = load i32, i32* %4
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %26
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.connect_to_server.lockfile = [17 x i8] c"/tmp/ndsctl.lock\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"ndsctl: nodogsplash probably not started (Error: %s)\0A\00"
@errno = global i32 0
define i32 @connect_to_server(i8* %0) {
%2 = alloca [17 x i8]
%3 = alloca %struct.sockaddr_un
%4 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @strncpy(i32 %13, i8* %0, i32 3)
%15 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%16 = load i32, i32* %12
%17 = call i64 @strlen(i32 %16)
%18 = add i64 %17, 4
%19 = call i64 @connect(i32 %8, %struct.sockaddr* %15, i64 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %1
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = call i32 @remove(i8* %4)
br label %27
27: ; preds = %1, %21
%28 = phi i32 [ -1, %21 ], [ %8, %1 ]
ret i32 %28
}