#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
  int /*<<< orphan*/ h_addrtype;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
scalar_t__ init_sf_source(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_sf_source(const char *host, int port) {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  struct hostent *entry;
  struct sockaddr_in addr;
  if (fd < 0)
    return fd;
  entry = gethostbyname(host);
  if (!entry) {
    close(fd);
    return -1;
  }
  addr.sin_family = entry->h_addrtype;
  memcpy(&addr.sin_addr, entry->h_addr_list[0], entry->h_length);
  addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
    close(fd);
    return -1;
  }
  if (init_sf_source(fd) < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_sf_source>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b0 <open_sf_source+0x40>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1269 <open_sf_source+0xf9>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dc <open_sf_source+0x6c>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <open_sf_source+0xf9>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x4,%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    123d <open_sf_source+0xcd>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <open_sf_source+0xf9>
mov    -0x18(%rbp),%edi
call   1040 <init_sf_source@plt>
cmp    $0x0,%rax
jge    1263 <open_sf_source+0xf3>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <open_sf_source+0xf9>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_sf_source>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fa <open_sf_source+0x8a>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     11ee <open_sf_source+0x7e>
mov    0x10(%rax),%ecx
mov    %ecx,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     11ee <open_sf_source+0x7e>
mov    %ebx,%edi
call   1040 <init_sf_source@plt>
test   %rax,%rax
jns    11fa <open_sf_source+0x8a>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32*, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @open_sf_source(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %6
store i32 %15, i32* %3
br label %58
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %7
%19 = load %struct.hostent*, %struct.hostent** %7
%20 = icmp ne %struct.hostent* %19, null
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %3
br label %58
24: ; preds = %16
%25 = load %struct.hostent*, %struct.hostent** %7
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%30 = load %struct.hostent*, %struct.hostent** %7
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32*, i32** %31
%33 = getelementptr inbounds i32, i32* %32, i64 0
%34 = load i32, i32* %33
%35 = load %struct.hostent*, %struct.hostent** %7
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @memcpy(i32* %29, i32 %34, i32 %37)
%39 = load i32, i32* %5
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %6
%43 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%44 = call i64 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %24
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %3
br label %58
49: ; preds = %24
%50 = load i32, i32* %6
%51 = call i64 @init_sf_source(i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %3
br label %58
56: ; preds = %49
%57 = load i32, i32* %6
store i32 %57, i32* %3
br label %58
58: ; preds = %56, %53, %46, %21, %14
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @open_sf_source(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = icmp slt i32 %6, 0
br i1 %8, label %37, label %9
9: ; preds = %2
%10 = tail call %struct.hostent* @gethostbyname(i8* %0)
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @close(i32 %6)
br label %37
14: ; preds = %9
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 2
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%20 = load i32*, i32** %19
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @memcpy(i32* %18, i32 %21, i32 %23)
%25 = call i32 @htons(i32 %1)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i64 @connect(i32 %6, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %14
%31 = call i32 @close(i32 %6)
br label %37
32: ; preds = %14
%33 = call i64 @init_sf_source(i32 %6)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @close(i32 %6)
br label %37
37: ; preds = %32, %2, %35, %30, %12
%38 = phi i32 [ -1, %30 ], [ -1, %35 ], [ -1, %12 ], [ %6, %2 ], [ %6, %32 ]
ret i32 %38
}