#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);
int /*<<< orphan*/ sock_init_sockaddr(struct sockaddr_in *, char const *,
                                      unsigned short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int sock_connect(const 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\n");
    return sock;
  }
  sock_init_sockaddr(&servername, host, port);
  err = connect(sock, (struct sockaddr *)&servername, sizeof(servername));
  if (err < 0) {
    fprintf(stderr, "sock_connect: connect failed\n");
    shutdown(sock, 2);
    return 0;
  }
  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    123d <sock_connect+0xdd>
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdi
movzwl -0x12(%rbp),%edx
call   1070 <sock_init_sockaddr@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    121d <sock_connect+0xbd>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
movl   $0x0,-0x4(%rbp)
jmp    123d <sock_connect+0xdd>
mov    -0x1c(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%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     11cd <sock_connect+0x6d>
movzwl %bp,%edx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <sock_init_sockaddr@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %eax,%eax
js     11e4 <sock_connect+0x84>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
jmp    1207 <sock_connect+0xa7>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1207 <sock_connect+0xa7>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
xor    %ebx,%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 = [37 x i8] c"sock_connect: Error creating socket\0A\00"
@.str.1 = [30 x i8] c"sock_connect: connect failed\0A\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 ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %38
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = load i16, i16* %5
%21 = call i32 @sock_init_sockaddr(%struct.sockaddr_in* %6, i8* %19, i16 zeroext %20)
%22 = load i32, i32* %7
%23 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 4)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %7
%31 = call i32 @shutdown(i32 %30, i32 2)
store i32 0, i32* %3
br label %38
32: ; preds = %18
%33 = load i32, i32* %7
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* @O_NONBLOCK
%36 = call i32 @fcntl(i32 %33, i32 %34, i32 %35)
%37 = load i32, i32* %7
store i32 %37, i32* %3
br label %38
38: ; preds = %32, %27, %14
%39 = load i32, i32* %3
ret i32 %39
}

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 = [37 x i8] c"sock_connect: Error creating socket\0A\00"
@.str.1 = [30 x i8] c"sock_connect: connect failed\0A\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 ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %25
12: ; preds = %2
%13 = call i32 @sock_init_sockaddr(%struct.sockaddr_in* %3, i8* %0, i16 zeroext %1)
%14 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%15 = call i32 @connect(i32 %7, %struct.sockaddr* %14, i32 4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @shutdown(i32 %7, i32 2)
br label %25
21: ; preds = %12
%22 = load i32, i32* @F_SETFL
%23 = load i32, i32* @O_NONBLOCK
%24 = call i32 @fcntl(i32 %7, i32 %22, i32 %23)
br label %25
25: ; preds = %21, %17, %9
%26 = phi i32 [ %7, %9 ], [ 0, %17 ], [ %7, %21 ]
ret i32 %26
}