#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;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ ESPIPE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ writev(int, struct iovec *, int);
int send_cmd(char const *cmd, char const *sockpath) {
  struct sockaddr_un addr;
  int fd;
  struct iovec v[2];
  if (!*cmd) {
    errno = ESPIPE;
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_LOCAL;
  strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path) - 1);
  fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("socket");
    return -1;
  }
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("connect");
    close(fd);
    return -1;
  }
  v[0].iov_base = (char *)cmd;
  v[0].iov_len = strlen(cmd);
  v[1].iov_base = "\n";
  v[1].iov_len = 1;
  writev(fd, v, 2);
  return fd;
}


x86 assembly (clang, -O0):
<send_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11bb <send_cmd+0x3b>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <send_cmd+0x11e>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1226 <send_cmd+0xa6>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <send_cmd+0x11e>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1261 <send_cmd+0xe1>
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <send_cmd+0x11e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x48(%rbp)
lea    0xd93(%rip),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x2,%edx
call   1040 <writev@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_cmd>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
cmpb   $0x0,(%rdi)
je     122f <send_cmd+0xaf>
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e38(%rip),%r15
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    (%r15),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1252 <send_cmd+0xd2>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1260 <send_cmd+0xe0>
mov    %r14,0x10(%rsp)
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,0x18(%rsp)
lea    0xe00(%rip),%rax
mov    %rax,0x20(%rsp)
movl   $0x1,0x28(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <writev@plt>
jmp    1246 <send_cmd+0xc6>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    0x2d89(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xda7(%rip),%rdi
call   1080 <perror@plt>
jmp    1241 <send_cmd+0xc1>
lea    0xda0(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1241 <send_cmd+0xc1>

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

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