#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;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int establish_connection() {
  int sock;
  struct sockaddr name = {AF_UNIX, "colorserver"};
  socklen_t namelen = sizeof(struct sockaddr) + 12;
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock != -1) {
    if (connect(sock, &name, namelen) == -1) {
      printf("error: connection to server failed. ERRNO:%d\n", errno);
      sock = -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<establish_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ea9(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x18(%rbp)
lea    0xeb3(%rip),%rax
mov    %eax,-0x10(%rbp)
movl   $0x1c,-0x1c(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     11b6 <establish_connection+0x86>
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%edx
lea    -0x18(%rbp),%rsi
call   1040 <connect@plt>
cmp    $0xffffffff,%eax
jne    11b1 <establish_connection+0x81>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <establish_connection+0x86>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establish_connection>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eac(%rip),%rax
movslq (%rax),%rdi
mov    %rdi,(%rsp)
lea    0xeb6(%rip),%rax
mov    %eax,0x8(%rsp)
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,%ebx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ebx
je     119c <establish_connection+0x6c>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1c,%edx
call   1040 <connect@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cmp    $0xffffffff,%ecx
jne    119c <establish_connection+0x6c>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i8*, i32 }
@AF_UNIX = global i32 0
@.str = [12 x i8] c"colorserver\00"
@SOCK_STREAM = global i32 0
@.str.1 = [46 x i8] c"error: connection to server failed. ERRNO:%d\0A\00"
@errno = global i32 0
define i32 @establish_connection() {
%1 = alloca i32
%2 = alloca %struct.sockaddr
%3 = alloca i32
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i32 0, i32 0
%5 = load i32, i32* @AF_UNIX
%6 = sext i32 %5 to i64
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %4
%8 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i32 0, i32 1
store i32 ptrtoint ([12 x i8]* @.str to i32), i32* %8
store i32 28, i32* %3
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %1
%12 = load i32, i32* %1
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %23
14: ; preds = %0
%15 = load i32, i32* %1
%16 = load i32, i32* %3
%17 = call i32 @connect(i32 %15, %struct.sockaddr* %2, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @errno
%21 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 -1, i32* %1
br label %22
22: ; preds = %19, %14
br label %23
23: ; preds = %22, %0
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
%struct.sockaddr = type { i8*, i32 }
@AF_UNIX = global i32 0
@.str = [12 x i8] c"colorserver\00"
@SOCK_STREAM = global i32 0
@.str.1 = [46 x i8] c"error: connection to server failed. ERRNO:%d\0A\00"
@errno = global i32 0
define i32 @establish_connection() {
%1 = alloca %struct.sockaddr
%2 = bitcast %struct.sockaddr* %1 to i8*
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%4 = load i32, i32* @AF_UNIX
%5 = sext i32 %4 to i64
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %3
%7 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 1
store i32 ptrtoint ([12 x i8]* @.str to i32), i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %4, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %17, label %11
11: ; preds = %0
%12 = call i32 @connect(i32 %9, %struct.sockaddr* %1, i32 28)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %11, %14, %0
%18 = phi i32 [ -1, %14 ], [ %9, %11 ], [ -1, %0 ]
ret i32 %18
}