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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_connect(int port, char *host) {
  int err = 0;
  struct sockaddr_in serv_addr;
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    return fd;
  err = inet_pton(AF_INET, host ? host : "127.0.0.1", &serv_addr.sin_addr);
  if (err < 0)
    return err;
  err = connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (err < 0)
    return err;
  return fd;
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11be <socket_connect+0x6e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <socket_connect+0xf9>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e2 <socket_connect+0x92>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11f2 <socket_connect+0xa2>
lea    0xe17(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11f2 <socket_connect+0xa2>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <inet_pton@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    121a <socket_connect+0xca>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <socket_connect+0xf9>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1243 <socket_connect+0xf3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <socket_connect+0xf9>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e74(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d4 <socket_connect+0x84>
mov    (%r15),%edi
test   %r14,%r14
lea    0xe54(%rip),%rsi
cmovne %r14,%rsi
mov    %rsp,%rdx
call   1050 <inet_pton@plt>
test   %eax,%eax
js     11d2 <socket_connect+0x82>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
test   %eax,%eax
cmovs  %eax,%ebx
jmp    11d4 <socket_connect+0x84>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @socket_connect(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%9 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* %4
%13 = call i32 @htons(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = load i32, i32* %8
store i32 %21, i32* %3
br label %47
22: ; preds = %2
%23 = load i32, i32* @AF_INET
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = load i8*, i8** %5
br label %29
28: ; preds = %22
br label %29
29: ; preds = %28, %26
%30 = phi i8* [ %27, %26 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %28 ]
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%32 = call i32 @inet_pton(i32 %23, i8* %30, i32* %31)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %47
37: ; preds = %29
%38 = load i32, i32* %8
%39 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%40 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = load i32, i32* %6
store i32 %44, i32* %3
br label %47
45: ; preds = %37
%46 = load i32, i32* %8
store i32 %46, i32* %3
br label %47
47: ; preds = %45, %43, %35, %20
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @socket_connect(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = call i32 @htons(i32 %0)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = icmp eq i8* %1, null
%17 = select i1 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %1
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%19 = call i32 @inet_pton(i32 %15, i8* %17, i32* %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %14
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i32 @connect(i32 %12, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i32 %23, 0
%25 = select i1 %24, i32 %23, i32 %12
br label %26
26: ; preds = %21, %14, %2
%27 = phi i32 [ %12, %2 ], [ %19, %14 ], [ %25, %21 ]
ret i32 %27
}