#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 {
  int /*<<< orphan*/ sa_family;
};

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, unsigned int);
int errno;
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ui_connect(struct sockaddr *dest, unsigned int dest_len) {
  int sock;
  sock = socket(dest->sa_family, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket");
    return -errno;
  }
  if (connect(sock, dest, dest_len)) {
    perror("connect");
    close(sock);
    return -errno;
  }
  return sock;
}


x86 assembly (clang, -O0):
<ui_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1191 <ui_connect+0x51>
lea    0xe87(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
mov    0x2e51(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <ui_connect+0x97>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
je     11d1 <ui_connect+0x91>
lea    0xe56(%rip),%rdi
call   1040 <perror@plt>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
mov    0x2e11(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <ui_connect+0x97>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ui_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbp
mov    (%rdi),%edi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     1187 <ui_connect+0x47>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <connect@plt>
test   %rax,%rax
jne    11a2 <ui_connect+0x62>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe72(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e3e(%rip),%rax
sub    (%rax),%r15d
mov    %r15d,%ebx
jmp    117a <ui_connect+0x3a>
lea    0xe5e(%rip),%rdi
call   1040 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2e1c(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    117a <ui_connect+0x3a>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@errno = global i32 0
@.str.1 = [8 x i8] c"connect\00"
define i32 @ui_connect(%struct.sockaddr* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr*
%5 = alloca i32
%6 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i32 %1, i32* %5
%7 = load %struct.sockaddr*, %struct.sockaddr** %4
%8 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %3
br label %32
18: ; preds = %2
%19 = load i32, i32* %6
%20 = load %struct.sockaddr*, %struct.sockaddr** %4
%21 = load i32, i32* %5
%22 = call i64 @connect(i32 %19, %struct.sockaddr* %20, i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %3
br label %32
30: ; preds = %18
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %32
32: ; preds = %30, %24, %14
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@errno = global i32 0
@.str.1 = [8 x i8] c"connect\00"
define i32 @ui_connect(%struct.sockaddr* %0, i32 %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
br label %20
12: ; preds = %2
%13 = tail call i64 @connect(i32 %6, %struct.sockaddr* %0, i32 %1)
%14 = icmp eq i64 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @close(i32 %6)
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %20
20: ; preds = %12, %15, %8
%21 = phi i32 [ %11, %8 ], [ %19, %15 ], [ %6, %12 ]
ret i32 %21
}