#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct servent {
  int /*<<< orphan*/ s_port;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char const *);
struct servent *getservbyname(char const *, char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char const *, TYPE_1__ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sscanf(char const *, char *, int *);
int /*<<< orphan*/ stderr;
int getsockfd(const char *host, const char *port) {
  struct hostent *hostent;
  struct servent *servent;
  struct sockaddr_in address;
  int numero, sock;
  memset(&address, 0, sizeof(struct sockaddr_in));
  if (inet_aton(host, &(address.sin_addr)) == 0) {
    if ((hostent = gethostbyname(host)) == NULL) {
      fprintf(stderr, "unknow host\n");
      return -1;
    }
    address.sin_addr.s_addr = ((struct in_addr *)(hostent->h_addr))->s_addr;
  }
  if (sscanf(port, "%d", &numero) == 1) {
    address.sin_port = htons(numero);
  } else if ((servent = getservbyname(port, "tcp")) == NULL) {
    fprintf(stderr, "unknow service %s\n", port);
    return -1;
  } else {
    address.sin_port = servent->s_port;
  }
  address.sin_family = AF_INET;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return -1;
  }
  if (connect(sock, (struct sockaddr *)&address, sizeof(struct sockaddr_in)) <
      0) {
    perror("connect");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<getsockfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
add    $0x8,%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%rax
jne    1221 <getsockfd+0x81>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1215 <getsockfd+0x75>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <getsockfd+0x183>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xde1(%rip),%rsi
lea    -0x3c(%rbp),%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    124e <getsockfd+0xae>
mov    -0x3c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x34(%rbp)
jmp    12a1 <getsockfd+0x101>
mov    -0x18(%rbp),%rdi
lea    0xdb7(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1293 <getsockfd+0xf3>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd94(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <getsockfd+0x183>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    12a1 <getsockfd+0x101>
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jge    12ea <getsockfd+0x14a>
lea    0xd4e(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <getsockfd+0x183>
mov    -0x40(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    131d <getsockfd+0x17d>
lea    0xd22(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <getsockfd+0x183>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsockfd>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <inet_aton@plt>
test   %rax,%rax
jne    11ea <getsockfd+0x4a>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1286 <getsockfd+0xe6>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0xe1c(%rip),%rsi
lea    0x14(%rsp),%rdx
mov    %r14,%rdi
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    120e <getsockfd+0x6e>
mov    0x14(%rsp),%edi
call   1030 <htons@plt>
jmp    1224 <getsockfd+0x84>
lea    0xdfb(%rip),%rsi
mov    %r14,%rdi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     126a <getsockfd+0xca>
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     129f <getsockfd+0xff>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
jns    12b0 <getsockfd+0x110>
lea    0xdc6(%rip),%rdi
jmp    12a6 <getsockfd+0x106>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12ab <getsockfd+0x10b>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12ab <getsockfd+0x10b>
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.servent = type { i32 }
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [13 x i8] c"unknow host\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [4 x i8] c"tcp\00"
@.str.3 = [19 x i8] c"unknow service %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@.str.5 = [8 x i8] c"connect\00"
define i32 @getsockfd(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.hostent*
%7 = alloca %struct.servent*
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%12 = load i8*, i8** %4
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%14 = call i64 @inet_aton(i8* %12, %struct.TYPE_2__* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %6
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
23: ; preds = %16
%24 = load %struct.hostent*, %struct.hostent** %6
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to %struct.in_addr*
%28 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
br label %32
32: ; preds = %23, %2
%33 = load i8*, i8** %5
%34 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %9)
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i32, i32* %9
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %38, i32* %39
br label %54
40: ; preds = %32
%41 = load i8*, i8** %5
%42 = call %struct.servent* @getservbyname(i8* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store %struct.servent* %42, %struct.servent** %7
%43 = icmp eq %struct.servent* %42, null
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %5
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %46)
store i32 -1, i32* %3
br label %72
48: ; preds = %40
%49 = load %struct.servent*, %struct.servent** %7
%50 = getelementptr inbounds %struct.servent, %struct.servent* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %51, i32* %52
br label %53
53: ; preds = %48
br label %54
54: ; preds = %53, %36
%55 = load i32, i32* @AF_INET
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %55, i32* %56
%57 = load i32, i32* @AF_INET
%58 = load i32, i32* @SOCK_STREAM
%59 = call i32 @socket(i32 %57, i32 %58, i32 0)
store i32 %59, i32* %10
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %54
%62 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
63: ; preds = %54
%64 = load i32, i32* %10
%65 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%66 = call i64 @connect(i32 %64, %struct.sockaddr* %65, i32 12)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
70: ; preds = %63
%71 = load i32, i32* %10
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %68, %61, %44, %20
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.servent = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [13 x i8] c"unknow host\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [4 x i8] c"tcp\00"
@.str.3 = [19 x i8] c"unknow service %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@.str.5 = [8 x i8] c"connect\00"
define i32 @getsockfd(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%9 = call i64 @inet_aton(i8* %0, %struct.TYPE_2__* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %2
%12 = call %struct.hostent* @gethostbyname(i8* %0)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %55
17: ; preds = %11
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to %struct.in_addr*
%21 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %20, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i64 0, i32 0
store i32 %22, i32* %23
br label %24
24: ; preds = %17, %2
%25 = call i32 @sscanf(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %4
%29 = call i32 @htons(i32 %28)
br label %39
30: ; preds = %24
%31 = call %struct.servent* @getservbyname(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq %struct.servent* %31, null
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %55
36: ; preds = %30
%37 = getelementptr inbounds %struct.servent, %struct.servent* %31, i64 0, i32 0
%38 = load i32, i32* %37
br label %39
39: ; preds = %36, %27
%40 = phi i32 [ %38, %36 ], [ %29, %27 ]
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %40, i32* %41
%42 = load i32, i32* @AF_INET
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* @SOCK_STREAM
%45 = call i32 @socket(i32 %42, i32 %44, i32 0)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %55
49: ; preds = %39
%50 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%51 = call i64 @connect(i32 %45, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %49, %53, %47, %33, %14
%56 = phi i32 [ -1, %14 ], [ -1, %47 ], [ -1, %53 ], [ -1, %33 ], [ %45, %49 ]
ret i32 %56
}