#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*/ dont_report(int, char *, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ report(int, 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 sock_connect(char *host, unsigned short int port) {
  struct sockaddr_in servername;
  int sock;
  int err = 0;
  report(5, "sock_connect: Creating socket");
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    report(1, "sock_connect: Error creating socket");
    return sock;
  }
  dont_report(5, "sock_connect: Created socket (%i)", sock);
  if (sock_init_sockaddr(&servername, host, port) < 0)
    return -1;
  err = connect(sock, (struct sockaddr *)&servername, sizeof(servername));
  if (err < 0) {
    report(1, "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    $0x5,%edi
lea    0xe6a(%rip),%rsi
call   1070 <report@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11dd <sock_connect+0x6d>
mov    $0x1,%edi
lea    0xe51(%rip),%rsi
call   1070 <report@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1280 <sock_connect+0x110>
mov    -0x1c(%rbp),%edx
mov    $0x5,%edi
lea    0xe56(%rip),%rsi
call   1050 <dont_report@plt>
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdi
movzwl -0x12(%rbp),%edx
call   1080 <sock_init_sockaddr@plt>
cmp    $0x0,%rax
jge    1218 <sock_connect+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <sock_connect+0x110>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1260 <sock_connect+0xf0>
mov    $0x1,%edi
lea    0xe22(%rip),%rsi
call   1070 <report@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <sock_connect+0x110>
mov    -0x1c(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
mov    0x2d75(%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
lea    0xe7c(%rip),%rsi
mov    $0x5,%edi
call   1070 <report@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1205 <sock_connect+0x95>
lea    0xe8e(%rip),%rsi
mov    $0x5,%edi
mov    %ebx,%edx
call   1050 <dont_report@plt>
movzwl %bp,%edx
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1080 <sock_init_sockaddr@plt>
test   %rax,%rax
js     1235 <sock_connect+0xc5>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %eax,%eax
js     1218 <sock_connect+0xa8>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
jmp    123a <sock_connect+0xca>
lea    0xe12(%rip),%rsi
mov    $0x1,%edi
call   1070 <report@plt>
jmp    123a <sock_connect+0xca>
lea    0xe45(%rip),%rsi
mov    $0x1,%edi
call   1070 <report@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 }
@.str = [30 x i8] c"sock_connect: Creating socket\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [36 x i8] c"sock_connect: Error creating socket\00"
@.str.2 = [34 x i8] c"sock_connect: Created socket (%i)\00"
@.str.3 = [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 = call i32 @report(i32 5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @report(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %42
18: ; preds = %2
%19 = load i32, i32* %7
%20 = call i32 @dont_report(i32 5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i8*, i8** %4
%22 = load i16, i16* %5
%23 = call i64 @sock_init_sockaddr(%struct.sockaddr_in* %6, i8* %21, i16 zeroext %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
store i32 -1, i32* %3
br label %42
26: ; preds = %18
%27 = load i32, i32* %7
%28 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%29 = call i32 @connect(i32 %27, %struct.sockaddr* %28, i32 4)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = call i32 @report(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* %7
%35 = call i32 @shutdown(i32 %34, i32 2)
store i32 -1, i32* %3
br label %42
36: ; preds = %26
%37 = load i32, i32* %7
%38 = load i32, i32* @F_SETFL
%39 = load i32, i32* @O_NONBLOCK
%40 = call i32 @fcntl(i32 %37, i32 %38, i32 %39)
%41 = load i32, i32* %7
store i32 %41, i32* %3
br label %42
42: ; preds = %36, %32, %25, %15
%43 = load i32, i32* %3
ret i32 %43
}

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