#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int socket_local_client(const char *name) {
  int fd;
  struct sockaddr_un addr;
  fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("socket");
    return -1;
  }
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, name);
  if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
    perror("connect");
    goto error;
  }
  return fd;
error:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<socket_local_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119a <socket_local_client+0x4a>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <socket_local_client+0xa8>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    11de <socket_local_client+0x8e>
lea    0xe33(%rip),%rdi
call   1050 <perror@plt>
jmp    11e9 <socket_local_client+0x99>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f8 <socket_local_client+0xa8>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_local_client>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11a9 <socket_local_client+0x59>
mov    %eax,%ebx
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     11b7 <socket_local_client+0x67>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe50(%rip),%rdi
call   1050 <perror@plt>
jmp    11ca <socket_local_client+0x7a>
lea    0xe49(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    119f <socket_local_client+0x4f>

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_UNIX = global i32 0
@.str.1 = [8 x i8] c"connect\00"
define i32 @socket_local_client(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_LOCAL
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
10: ; preds = %1
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = tail call i32 @strcpy(i32 undef, i8* %0)
%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 %20
17: ; preds = %10
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @close(i32 %6)
br label %20
20: ; preds = %10, %17, %8
%21 = phi i32 [ -1, %8 ], [ -1, %17 ], [ %6, %10 ]
ret i32 %21
}