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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int connect_through_socket(int socketfd, struct addrinfo *res) {
  assert(socketfd != -1);
  assert(res != NULL);
  int ret = connect(socketfd, res->ai_addr, res->ai_addrlen);
  if (ret == 0) {
    return 0;
  } else {
    perror("connect_through_socket-connect()");
    close(socketfd);
    return -1;
  }
}


x86 assembly (clang, -O0):
<connect_through_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11a0 <connect_through_socket+0x60>
movl   $0x0,-0x4(%rbp)
jmp    11bb <connect_through_socket+0x7b>
lea    0xe59(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%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_through_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %edi,%edi
cmp    $0xffffffff,%ebp
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1060 <connect@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    117f <connect_through_socket+0x3f>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
jmp    1178 <connect_through_socket+0x38>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@.str = [33 x i8] c"connect_through_socket-connect()\00"
define i32 @connect_through_socket(i32 %0, %struct.addrinfo* %1) {
%3 = icmp ne i32 %0, -1
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne %struct.addrinfo* %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @connect(i32 %0, i32 %10, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @close(i32 %0)
br label %18
18: ; preds = %2, %15
%19 = phi i32 [ -1, %15 ], [ 0, %2 ]
ret i32 %19
}