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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ set_nonblocking(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_socket(struct sockaddr_in *addr) {
  int fd, ret;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1)
    goto fail;
  ret = connect(fd, (struct sockaddr *)addr, sizeof(*addr));
  if (ret == -1) {
    perror("connect");
    goto fail;
  }
  if (!set_nonblocking(fd))
    goto fail;
  return fd;
fail:
  if (fd != -1)
    close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1187 <connect_socket+0x37>
jmp    11d7 <connect_socket+0x87>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b6 <connect_socket+0x66>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
jmp    11d7 <connect_socket+0x87>
mov    -0x14(%rbp),%edi
call   1040 <set_nonblocking@plt>
cmp    $0x0,%eax
jne    11cc <connect_socket+0x7c>
jmp    11d7 <connect_socket+0x87>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <connect_socket+0xa0>
cmpl   $0xffffffff,-0x14(%rbp)
je     11e9 <connect_socket+0x99>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     11b4 <connect_socket+0x64>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     11a1 <connect_socket+0x51>
mov    %ebp,%edi
call   1040 <set_nonblocking@plt>
test   %eax,%eax
je     11ad <connect_socket+0x5d>
mov    %ebp,%r14d
jmp    11b4 <connect_socket+0x64>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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