#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 *, int);
int /*<<< orphan*/ writev(int, struct iovec *, int);
int send_cmd(char const *cmd) {
  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, "/var/run/l2tpctrl", 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    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11b7 <send_cmd+0x37>
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <send_cmd+0x11d>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    0xe23(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1225 <send_cmd+0xa5>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <send_cmd+0x11d>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1260 <send_cmd+0xe0>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <send_cmd+0x11d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x38(%rbp)
lea    0xda6(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x2,%edx
call   1040 <writev@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_cmd>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
cmpb   $0x0,(%rdi)
je     122d <send_cmd+0xad>
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e3e(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xe44(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x0(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     124d <send_cmd+0xcd>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     125b <send_cmd+0xdb>
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,0x18(%rsp)
lea    0xe14(%rip),%rax
mov    %rax,0x20(%rsp)
movl   $0x1,0x28(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <writev@plt>
jmp    1244 <send_cmd+0xc4>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    0x2d8b(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdbe(%rip),%rdi
call   1080 <perror@plt>
jmp    123f <send_cmd+0xbf>
lea    0xdb7(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    123f <send_cmd+0xbf>

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

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