#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;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strchr(char *, char);
int strtol(char *, int /*<<< orphan*/ *, int);
int open_net_fd(char *spec) {
  char *p;
  int fd, port;
  struct sockaddr_in sa;
  struct hostent *he;
  p = strchr(spec, ':');
  if (!p)
    return -1;
  *p++ = '\0';
  port = strtol(p, NULL, 10);
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  he = gethostbyname(spec);
  if (!he) {
    herror("open_net_fd");
    return -1;
  }
  memcpy(&sa.sin_addr, he->h_addr, he->h_length);
  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd < 0)
    return fd;
  if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<open_net_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b5 <open_net_fd+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <open_net_fd+0x11e>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1223 <open_net_fd+0xa3>
lea    0xdee(%rip),%rdi
call   1080 <herror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <open_net_fd+0x11e>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1271 <open_net_fd+0xf1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129e <open_net_fd+0x11e>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1298 <open_net_fd+0x118>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <open_net_fd+0x11e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_net_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1234 <open_net_fd+0xb4>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    0x2e28(%rip),%r14
mov    (%r14),%ecx
mov    %ecx,0x8(%rsp)
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1228 <open_net_fd+0xa8>
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %rsp,%rdi
call   1070 <memcpy@plt>
mov    (%r14),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1234 <open_net_fd+0xb4>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebp
jmp    1234 <open_net_fd+0xb4>
lea    0xdd1(%rip),%rdi
call   1080 <herror@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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