#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 /*<<< orphan*/ printf(char *);
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("\nerror: connection to server failed.");
      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     11ad <establish_connection+0x7d>
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%edx
lea    -0x18(%rbp),%rsi
call   1040 <connect@plt>
cmp    $0xffffffff,%eax
jne    11a8 <establish_connection+0x78>
lea    0xe70(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <establish_connection+0x7d>
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     1193 <establish_connection+0x63>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1c,%edx
call   1040 <connect@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cmp    $0xffffffff,%ecx
jne    1193 <establish_connection+0x63>
lea    0xe83(%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 = [37 x i8] c"\0Aerror: connection to server failed.\00"
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 %22
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 %21
19: ; preds = %14
%20 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %21
21: ; preds = %19, %14
br label %22
22: ; preds = %21, %0
%23 = load i32, i32* %1
ret i32 %23
}

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 = [37 x i8] c"\0Aerror: connection to server failed.\00"
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 %16, 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 %16
14: ; preds = %11
%15 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %11, %14, %0
%17 = phi i32 [ -1, %14 ], [ %9, %11 ], [ -1, %0 ]
ret i32 %17
}