#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;
};
typedef int /*<<< orphan*/ servername;

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ shutdown(int, int);
scalar_t__ sock_init_sockaddr(struct sockaddr_in *, char *, unsigned short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int sock_connect(char *host, unsigned short int port) {
  struct sockaddr_in servername;
  int sock;
  int err = 0;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    fprintf(stderr, "sock_connect: Error creating socket");
    return sock;
  }
  if (sock_init_sockaddr(&servername, host, port) < 0)
    return -1;
  err = connect(sock, (struct sockaddr *)&servername, sizeof(servername));
  if (err < 0) {
    fprintf(stderr, "sock_connect: connect failed");
    shutdown(sock, 2);
    return -1;
  }
  fcntl(sock, F_SETFL, O_NONBLOCK);
  return sock;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c0 <sock_connect+0x60>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1253 <sock_connect+0xf3>
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdi
movzwl -0x12(%rbp),%edx
call   1070 <sock_init_sockaddr@plt>
cmp    $0x0,%rax
jge    11e7 <sock_connect+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <sock_connect+0xf3>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1233 <sock_connect+0xd3>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <sock_connect+0xf3>
mov    -0x1c(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d1 <sock_connect+0x71>
movzwl %bp,%edx
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1070 <sock_init_sockaddr@plt>
test   %rax,%rax
js     1209 <sock_connect+0xa9>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %eax,%eax
js     11e8 <sock_connect+0x88>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
jmp    120e <sock_connect+0xae>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1050 <fprintf@plt>
jmp    120e <sock_connect+0xae>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"sock_connect: Error creating socket\00"
@.str.1 = [29 x i8] c"sock_connect: connect failed\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @sock_connect(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
store i32 0, i32* %8
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %41
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = load i16, i16* %5
%21 = call i64 @sock_init_sockaddr(%struct.sockaddr_in* %6, i8* %19, i16 zeroext %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %3
br label %41
24: ; preds = %18
%25 = load i32, i32* %7
%26 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%27 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 4)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* %7
%34 = call i32 @shutdown(i32 %33, i32 2)
store i32 -1, i32* %3
br label %41
35: ; preds = %24
%36 = load i32, i32* %7
%37 = load i32, i32* @F_SETFL
%38 = load i32, i32* @O_NONBLOCK
%39 = call i32 @fcntl(i32 %36, i32 %37, i32 %38)
%40 = load i32, i32* %7
store i32 %40, i32* %3
br label %41
41: ; preds = %35, %30, %23, %14
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"sock_connect: Error creating socket\00"
@.str.1 = [29 x i8] c"sock_connect: connect failed\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @sock_connect(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %27
12: ; preds = %2
%13 = call i64 @sock_init_sockaddr(%struct.sockaddr_in* %3, i8* %0, i16 zeroext %1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %27, label %15
15: ; preds = %12
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i32 @connect(i32 %7, %struct.sockaddr* %16, i32 4)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @shutdown(i32 %7, i32 2)
br label %27
23: ; preds = %15
%24 = load i32, i32* @F_SETFL
%25 = load i32, i32* @O_NONBLOCK
%26 = call i32 @fcntl(i32 %7, i32 %24, i32 %25)
br label %27
27: ; preds = %12, %23, %19, %9
%28 = phi i32 [ %7, %9 ], [ -1, %19 ], [ %7, %23 ], [ -1, %12 ]
ret i32 %28
}