#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ set_error(char *, int, char *, ...);
char *strerror(int /*<<< orphan*/);
int tcp_set_option_internal(int, int);
int tcp_socket(char *, int);
int tcp_connect(char *addr, int port, int opt, char *ebuf, int ebuf_sz) {
  int socket_fd;
  struct sockaddr_in sa;
  assert(addr != NULL);
  assert(port > 0);
  if ((socket_fd = tcp_socket(ebuf, ebuf_sz)) == -1) {
    set_error(ebuf, ebuf_sz, "Failed to create a socket");
    return -1;
  }
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  if (inet_aton(addr, &sa.sin_addr) == 0) {
    struct hostent *he;
    he = gethostbyname(addr);
    if (he == NULL) {
      set_error(ebuf, ebuf_sz, "Failed to resolve address: %s", addr);
      close(socket_fd);
      return -1;
    }
    memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
  }
  if (connect(socket_fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    set_error(ebuf, ebuf_sz, "Failed to connect: %s", strerror(errno));
    close(socket_fd);
    return -1;
  }
  if (opt && tcp_set_option_internal(socket_fd, opt) == -1) {
    set_error(ebuf, ebuf_sz, "Failed to set connection option: %s",
              strerror(errno));
    close(socket_fd);
    return -1;
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
cmpl   $0x0,-0x14(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <tcp_socket@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    1226 <tcp_connect+0x76>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    0xded(%rip),%rdx
mov    $0x0,%al
call   1050 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <tcp_connect+0x1cb>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <inet_aton@plt>
cmp    $0x0,%rax
jne    12ad <tcp_connect+0xfd>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1299 <tcp_connect+0xe9>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xd9c(%rip),%rdx
mov    $0x0,%al
call   1050 <set_error@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <tcp_connect+0x1cb>
lea    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    130f <tcp_connect+0x15f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    %rax,%rcx
lea    0xd44(%rip),%rdx
mov    $0x0,%al
call   1050 <set_error@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <tcp_connect+0x1cb>
cmpl   $0x0,-0x18(%rbp)
je     1375 <tcp_connect+0x1c5>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10d0 <tcp_set_option_internal@plt>
cmp    $0xffffffff,%eax
jne    1375 <tcp_connect+0x1c5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    0x2c96(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x60(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    %rax,%rcx
lea    0xcf4(%rip),%rdx
mov    $0x0,%al
call   1050 <set_error@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <tcp_connect+0x1cb>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r14d
mov    %rcx,%r12
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%rbp
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1080 <assert@plt>
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1080 <assert@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1030 <tcp_socket@plt>
cmp    $0xffffffff,%eax
je     1291 <tcp_connect+0xe1>
mov    %eax,%r13d
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   10a0 <inet_aton@plt>
test   %rax,%rax
jne    124a <tcp_connect+0x9a>
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     12c7 <tcp_connect+0x117>
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     12a7 <tcp_connect+0xf7>
test   %r15d,%r15d
je     12ec <tcp_connect+0x13c>
mov    %r13d,%edi
mov    %r15d,%esi
call   10d0 <tcp_set_option_internal@plt>
cmp    $0xffffffff,%eax
jne    12ec <tcp_connect+0x13c>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdbf(%rip),%rdx
jmp    12bc <tcp_connect+0x10c>
lea    0xd68(%rip),%rdx
mov    %r12,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <set_error@plt>
jmp    12e6 <tcp_connect+0x136>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd7c(%rip),%rdx
mov    %r12,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
jmp    12d7 <tcp_connect+0x127>
lea    0xd4c(%rip),%rdx
mov    %r12,%rdi
mov    %r14d,%esi
mov    %rbp,%rcx
xor    %eax,%eax
call   1050 <set_error@plt>
mov    %r13d,%edi
call   1060 <close@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"Failed to create a socket\00"
@AF_INET = global i32 0
@.str.1 = [30 x i8] c"Failed to resolve address: %s\00"
@.str.2 = [22 x i8] c"Failed to connect: %s\00"
@errno = global i32 0
@.str.3 = [36 x i8] c"Failed to set connection option: %s\00"
define i32 @tcp_connect(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
%14 = alloca %struct.hostent*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* %8
%20 = icmp sgt i32 %19, 0
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i8*, i8** %10
%24 = load i32, i32* %11
%25 = call i32 @tcp_socket(i8* %23, i32 %24)
store i32 %25, i32* %12
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %5
%28 = load i8*, i8** %10
%29 = load i32, i32* %11
%30 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %28, i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %90
31: ; preds = %5
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* %8
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i8*, i8** %7
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%39 = call i64 @inet_aton(i8* %37, i32* %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %59
41: ; preds = %31
%42 = load i8*, i8** %7
%43 = call %struct.hostent* @gethostbyname(i8* %42)
store %struct.hostent* %43, %struct.hostent** %14
%44 = load %struct.hostent*, %struct.hostent** %14
%45 = icmp eq %struct.hostent* %44, null
br i1 %45, label %46, label %53
46: ; preds = %41
%47 = load i8*, i8** %10
%48 = load i32, i32* %11
%49 = load i8*, i8** %7
%50 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %47, i32 %48, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %49)
%51 = load i32, i32* %12
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %6
br label %90
53: ; preds = %41
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%55 = load %struct.hostent*, %struct.hostent** %14
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @memcpy(i32* %54, i32 %57, i32 4)
br label %59
59: ; preds = %53, %31
%60 = load i32, i32* %12
%61 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%62 = call i32 @connect(i32 %60, %struct.sockaddr* %61, i32 12)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %72
64: ; preds = %59
%65 = load i8*, i8** %10
%66 = load i32, i32* %11
%67 = load i32, i32* @errno
%68 = call i8* @strerror(i32 %67)
%69 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %65, i32 %66, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %68)
%70 = load i32, i32* %12
%71 = call i32 @close(i32 %70)
store i32 -1, i32* %6
br label %90
72: ; preds = %59
%73 = load i32, i32* %9
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %88
75: ; preds = %72
%76 = load i32, i32* %12
%77 = load i32, i32* %9
%78 = call i32 @tcp_set_option_internal(i32 %76, i32 %77)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %88
80: ; preds = %75
%81 = load i8*, i8** %10
%82 = load i32, i32* %11
%83 = load i32, i32* @errno
%84 = call i8* @strerror(i32 %83)
%85 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %81, i32 %82, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %84)
%86 = load i32, i32* %12
%87 = call i32 @close(i32 %86)
store i32 -1, i32* %6
br label %90
88: ; preds = %75, %72
%89 = load i32, i32* %12
store i32 %89, i32* %6
br label %90
90: ; preds = %88, %80, %64, %46, %27
%91 = load i32, i32* %6
ret i32 %91
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"Failed to create a socket\00"
@AF_INET = global i32 0
@.str.1 = [30 x i8] c"Failed to resolve address: %s\00"
@.str.2 = [22 x i8] c"Failed to connect: %s\00"
@errno = global i32 0
@.str.3 = [36 x i8] c"Failed to set connection option: %s\00"
define i32 @tcp_connect(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = icmp ne i8* %0, null
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = icmp sgt i32 %1, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = tail call i32 @tcp_socket(i8* %3, i32 %4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = tail call i32 (i8*, i32, i8*, ...) @set_error(i8* %3, i32 %4, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %55
18: ; preds = %5
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 %1)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%24 = call i64 @inet_aton(i8* %0, i32* %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %18
%27 = call %struct.hostent* @gethostbyname(i8* %0)
%28 = icmp eq %struct.hostent* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %3, i32 %4, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%31 = call i32 @close(i32 %14)
br label %55
32: ; preds = %26
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @memcpy(i32* %23, i32 %34, i32 4)
br label %36
36: ; preds = %32, %18
%37 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%38 = call i32 @connect(i32 %14, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %3, i32 %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = call i32 @close(i32 %14)
br label %55
45: ; preds = %36
%46 = icmp eq i32 %2, 0
br i1 %46, label %55, label %47
47: ; preds = %45
%48 = call i32 @tcp_set_option_internal(i32 %14, i32 %2)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i8*, i32, i8*, ...) @set_error(i8* %3, i32 %4, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = call i32 @close(i32 %14)
br label %55
55: ; preds = %29, %45, %47, %50, %40, %16
%56 = phi i32 [ -1, %16 ], [ -1, %40 ], [ -1, %50 ], [ %14, %47 ], [ %14, %45 ], [ -1, %29 ]
ret i32 %56
}