#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 dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ ENOENT;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, size_t);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t init_addr(struct sockaddr_un *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int open_conn(void) {
  int sd;
  struct sockaddr_un addr;
  size_t addr_len;
  sd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sd == -1) {
    perror("Failed to connect to walnutd");
    return -1;
  }
  addr_len = init_addr(&addr);
  if (connect(sd, (struct sockaddr *)&addr, addr_len) != 0) {
    if (errno == ENOENT)
      fprintf(stderr, "Failed to connect to walnutd: walnutd "
                      "does not appear to be running\n");
    else
      perror("Failed to connect to walnutd");
    return -1;
  }
  return sd;
}


x86 assembly (clang, -O0):
<open_conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1196 <open_conn+0x46>
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <open_conn+0xbf>
lea    -0x10(%rbp),%rdi
call   1040 <init_addr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <connect@plt>
cmp    $0x0,%rax
je     1209 <open_conn+0xb9>
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
jne    11f1 <open_conn+0xa1>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11fd <open_conn+0xad>
lea    0xe08(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <open_conn+0xbf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_conn>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11cb <open_conn+0x7b>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <init_addr@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1060 <connect@plt>
test   %rax,%rax
je     11c1 <open_conn+0x71>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
jne    11cb <open_conn+0x7b>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2e(%rip),%rdi
call   1050 <perror@plt>
jmp    11bc <open_conn+0x6c>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [29 x i8] c"Failed to connect to walnutd\00"
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [69 x i8] c"Failed to connect to walnutd: walnutd does not appear to be running\0A\00"
define i32 @open_conn() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i64
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %31
12: ; preds = %0
%13 = call i64 @init_addr(%struct.sockaddr_un* %3)
store i64 %13, i64* %4
%14 = load i32, i32* %2
%15 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%16 = load i64, i64* %4
%17 = call i64 @connect(i32 %14, %struct.sockaddr* %15, i64 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @ENOENT
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0))
br label %28
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
store i32 -1, i32* %1
br label %31
29: ; preds = %12
%30 = load i32, i32* %2
store i32 %30, i32* %1
br label %31
31: ; preds = %29, %28, %10
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [29 x i8] c"Failed to connect to walnutd\00"
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [69 x i8] c"Failed to connect to walnutd: walnutd does not appear to be running\0A\00"
define i32 @open_conn() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %23
9: ; preds = %0
%10 = call i64 @init_addr(%struct.sockaddr_un* %1)
%11 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%12 = call i64 @connect(i32 %5, %struct.sockaddr* %11, i64 %10)
%13 = icmp eq i64 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0))
br label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %9, %18, %21, %7
%24 = phi i32 [ -1, %7 ], [ -1, %21 ], [ -1, %18 ], [ %5, %9 ]
ret i32 %24
}