#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 s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int inet_addr(char const *);
int /*<<< orphan*/ memmove(TYPE_1__ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int festival_socket_open(const char *host, int port) {
  struct sockaddr_in serv_addr;
  struct hostent *serverhost;
  int fd;
  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd < 0) {
    fprintf(stderr, "festival_client: can't get socket\n");
    return -1;
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  if ((serv_addr.sin_addr.s_addr = inet_addr(host)) == -1) {
    serverhost = gethostbyname(host);
    if (serverhost == (struct hostent *)0) {
      fprintf(stderr, "festival_client: gethostbyname failed\n");
      return -1;
    }
    memmove(&serv_addr.sin_addr, serverhost->h_addr, serverhost->h_length);
  }
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) {
    fprintf(stderr, "festival_client: connect to server failed\n");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<festival_socket_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11dd <festival_socket_open+0x5d>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <festival_socket_open+0x12e>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1255 <festival_socket_open+0xd5>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123b <festival_socket_open+0xbb>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <festival_socket_open+0x12e>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1080 <memmove@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     12a8 <festival_socket_open+0x128>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <festival_socket_open+0x12e>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<festival_socket_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e51(%rip),%r15
mov    (%r15),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1230 <festival_socket_open+0xb0>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    %rbp,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x10(%rsp)
cmp    $0xffffffff,%eax
jne    11f5 <festival_socket_open+0x75>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1242 <festival_socket_open+0xc2>
lea    0x10(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1080 <memmove@plt>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
je     125c <festival_socket_open+0xdc>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
jmp    1252 <festival_socket_open+0xd2>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
jmp    1252 <festival_socket_open+0xd2>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"festival_client: can't get socket\0A\00"
@.str.1 = [39 x i8] c"festival_client: gethostbyname failed\0A\00"
@.str.2 = [43 x i8] c"festival_client: connect to server failed\0A\00"
define i32 @festival_socket_open(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %42
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = call i32 @inet_addr(i8* %0)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i64 0, i32 0
store i32 %15, i32* %17
%18 = icmp eq i32 %15, -1
br i1 %18, label %19, label %31
19: ; preds = %13
%20 = call %struct.hostent* @gethostbyname(i8* %0)
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %42
25: ; preds = %19
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @memmove(%struct.TYPE_2__* %16, i32 %27, i32 %29)
br label %31
31: ; preds = %25, %13
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %32, i32* %33
%34 = call i32 @htons(i32 %1)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %34, i32* %35
%36 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%37 = call i64 @connect(i32 %8, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %31
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %31, %39, %22, %10
%43 = phi i32 [ -1, %10 ], [ -1, %22 ], [ -1, %39 ], [ %8, %31 ]
ret i32 %43
}