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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void *strlen(char const *);
int writev(int, struct iovec *, int);
int splashy_prepare(const char *prompt) {
  int fd;
  struct sockaddr addr = {AF_UNIX, "\0/splashy"};
  struct iovec iov[2];
  if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
    return -1;
  }
  if (connect(fd, &addr, sizeof addr) == -1) {
    close(fd);
    return -1;
  }
  iov[0].iov_base = "getpass ";
  iov[0].iov_len = strlen("getpass ");
  iov[1].iov_base = (char *)prompt;
  iov[1].iov_len = strlen(prompt) + 1;
  if (writev(fd, iov, 2) == -1) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<splashy_prepare>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x28(%rbp)
lea    0xe8f(%rip),%rax
mov    %eax,-0x20(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11a5 <splashy_prepare+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <splashy_prepare+0xeb>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    11d3 <splashy_prepare+0x83>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <splashy_prepare+0xeb>
lea    0xe30(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe25(%rip),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x2,%edx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
jne    1235 <splashy_prepare+0xe5>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <splashy_prepare+0xeb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<splashy_prepare>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%r14
mov    0x2e7e(%rip),%rax
movslq (%rax),%rax
mov    %rax,(%rsp)
lea    0xe90(%rip),%rax
mov    %eax,0x8(%rsp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11fa <splashy_prepare+0xaa>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     11f3 <splashy_prepare+0xa3>
lea    0xe56(%rip),%rdi
mov    %rdi,0x10(%rsp)
call   1040 <strlen@plt>
mov    %rax,0x18(%rsp)
mov    %r14,0x20(%rsp)
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
mov    %rax,0x28(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
je     11f3 <splashy_prepare+0xa3>
mov    %ebp,%ebx
jmp    11fa <splashy_prepare+0xaa>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i8*, i32 }
%struct.iovec = type { i8*, i8* }
@AF_UNIX = global i32 0
@.str = [10 x i8] c"\00/splashy\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [9 x i8] c"getpass \00"
define i32 @splashy_prepare(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr
%6 = alloca [2 x %struct.iovec]
store i8* %0, i8** %3
%7 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i32 0, i32 0
%8 = load i32, i32* @AF_UNIX
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %7
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i32 0, i32 1
store i32 ptrtoint ([10 x i8]* @.str to i32), i32* %11
%12 = load i32, i32* @PF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %47
17: ; preds = %1
%18 = load i32, i32* %4
%19 = call i32 @connect(i32 %18, %struct.sockaddr* %5, i32 16)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %2
br label %47
24: ; preds = %17
%25 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0
%26 = getelementptr inbounds %struct.iovec, %struct.iovec* %25, i32 0, i32 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %26
%27 = call i8* @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%28 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0
%29 = getelementptr inbounds %struct.iovec, %struct.iovec* %28, i32 0, i32 1
store i8* %27, i8** %29
%30 = load i8*, i8** %3
%31 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 1
%32 = getelementptr inbounds %struct.iovec, %struct.iovec* %31, i32 0, i32 0
store i8* %30, i8** %32
%33 = load i8*, i8** %3
%34 = call i8* @strlen(i8* %33)
%35 = getelementptr i8, i8* %34, i64 1
%36 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 1
%37 = getelementptr inbounds %struct.iovec, %struct.iovec* %36, i32 0, i32 1
store i8* %35, i8** %37
%38 = load i32, i32* %4
%39 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0
%40 = call i32 @writev(i32 %38, %struct.iovec* %39, i32 2)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %24
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %2
br label %47
45: ; preds = %24
%46 = load i32, i32* %4
store i32 %46, i32* %2
br label %47
47: ; preds = %45, %42, %21, %16
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr = type { i8*, i32 }
%struct.iovec = type { i8*, i8* }
@AF_UNIX = global i32 0
@.str = [10 x i8] c"\00/splashy\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [9 x i8] c"getpass \00"
define i32 @splashy_prepare(i8* %0) {
%2 = alloca %struct.sockaddr
%3 = alloca [2 x %struct.iovec]
%4 = bitcast %struct.sockaddr* %2 to i8*
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%6 = load i32, i32* @AF_UNIX
%7 = sext i32 %6 to i64
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %5
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 1
store i32 ptrtoint ([10 x i8]* @.str to i32), i32* %9
%10 = bitcast [2 x %struct.iovec]* %3 to i8*
%11 = load i32, i32* @PF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %31, label %15
15: ; preds = %1
%16 = call i32 @connect(i32 %13, %struct.sockaddr* %2, i32 16)
%17 = icmp eq i32 %16, -1
br i1 %17, label %29, label %18
18: ; preds = %15
%19 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %3, i64 0, i64 0
%20 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %3, i64 0, i64 0, i32 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %20
%21 = call i8* @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %3, i64 0, i64 0, i32 1
store i8* %21, i8** %22
%23 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %3, i64 0, i64 1, i32 0
store i8* %0, i8** %23
%24 = call i8* @strlen(i8* %0)
%25 = getelementptr i8, i8* %24, i64 1
%26 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %3, i64 0, i64 1, i32 1
store i8* %25, i8** %26
%27 = call i32 @writev(i32 %13, %struct.iovec* %19, i32 2)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %18, %15
%30 = call i32 @close(i32 %13)
br label %31
31: ; preds = %29, %18, %1
%32 = phi i32 [ -1, %1 ], [ %13, %18 ], [ -1, %29 ]
ret i32 %32
}