#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int in_addr_t;
typedef int /*<<< orphan*/ host;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ IPPROTO_TCP;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int htonl(int);
int /*<<< orphan*/ htons(int);
int inet_addr(char *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int connect_to_host(char *name, int port) {
  int address;
  struct hostent *host_entity;
  int res, desc;
  int opts;
  struct sockaddr_in host;
  if (!strcmp(name, "localhost"))
    address = htonl(2130706433);
  else {
    address = inet_addr(name);
    if (address == (in_addr_t)-1) {
      host_entity = gethostbyname(name);
      if (!host_entity) {
        fprintf(stderr, "EAGI proxy: Wrong address!\n");
        return -1;
      }
      address = *((int *)host_entity->h_addr);
    }
  }
  desc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (desc < 0) {
    fprintf(stderr, "EAGI proxy: Cannot create socket!\n");
    return -1;
  }
  memset((void *)&host, 0, sizeof(struct sockaddr_in));
  host.sin_family = AF_INET;
  host.sin_port = htons(port);
  host.sin_addr.s_addr = address;
  res = connect(desc, (struct sockaddr *)&host, sizeof(host));
  if (res < 0) {
    fprintf(stderr, "EAGI proxy: Cannot connect!\n");
    return -1;
  }
  opts = fcntl(desc, F_GETFL);
  if (opts < 0) {
    perror("fcntl(F_GETFL)");
    exit(EXIT_FAILURE);
  }
  opts = (opts | O_NONBLOCK);
  if (fcntl(desc, F_SETFL, opts) < 0) {
    perror("fcntl(F_SETFL)");
    exit(EXIT_FAILURE);
  }
  return desc;
}


x86 assembly (clang, -O0):
<connect_to_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    11fa <connect_to_host+0x3a>
mov    $0x7f000001,%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
jmp    125a <connect_to_host+0x9a>
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1255 <connect_to_host+0x95>
mov    -0x10(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1249 <connect_to_host+0x89>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139d <connect_to_host+0x1dd>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    125a <connect_to_host+0x9a>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12a8 <connect_to_host+0xe8>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139d <connect_to_host+0x1dd>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1314 <connect_to_host+0x154>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    0xd46(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139d <connect_to_host+0x1dd>
mov    -0x28(%rbp),%edi
mov    0x2c92(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    134e <connect_to_host+0x18e>
lea    0xd2b(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x2c(%rbp),%eax
mov    0x2c88(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2c59(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1397 <connect_to_host+0x1d7>
lea    0xcf1(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c18(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_host>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xe27(%rip),%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1209 <connect_to_host+0x49>
mov    %rbx,%rdi
call   1070 <inet_addr@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1215 <connect_to_host+0x55>
mov    %rbx,%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     12de <connect_to_host+0x11e>
mov    (%rax),%rax
mov    (%rax),%ebp
jmp    1215 <connect_to_host+0x55>
mov    $0x7f000001,%edi
call   1050 <htonl@plt>
mov    %eax,%ebp
mov    0x2dcc(%rip),%r12
mov    (%r12),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12ba <connect_to_host+0xfa>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %eax,%eax
js     12cc <connect_to_host+0x10c>
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1307 <connect_to_host+0x147>
mov    0x2d4a(%rip),%rcx
or     (%rcx),%eax
mov    0x2d21(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
jns    12f8 <connect_to_host+0x138>
lea    0xdbd(%rip),%rdi
jmp    130e <connect_to_host+0x14e>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
jmp    12ee <connect_to_host+0x12e>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
jmp    12ee <connect_to_host+0x12e>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    0xd1c(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd58(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"EAGI proxy: Wrong address!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [35 x i8] c"EAGI proxy: Cannot create socket!\0A\00"
@.str.3 = [29 x i8] c"EAGI proxy: Cannot connect!\0A\00"
@F_GETFL = global i32 0
@.str.4 = [15 x i8] c"fcntl(F_GETFL)\00"
@EXIT_FAILURE = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.5 = [15 x i8] c"fcntl(F_SETFL)\00"
define i32 @connect_to_host(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%12 = load i8*, i8** %4
%13 = call i32 @strcmp(i8* %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @htonl(i32 2130706433)
store i32 %16, i32* %6
br label %37
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 @inet_addr(i8* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = call %struct.hostent* @gethostbyname(i8* %23)
store %struct.hostent* %24, %struct.hostent** %7
%25 = load %struct.hostent*, %struct.hostent** %7
%26 = icmp ne %struct.hostent* %25, null
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %92
30: ; preds = %22
%31 = load %struct.hostent*, %struct.hostent** %7
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to i32*
%35 = load i32, i32* %34
store i32 %35, i32* %6
br label %36
36: ; preds = %30, %17
br label %37
37: ; preds = %36, %15
%38 = load i32, i32* @AF_INET
%39 = load i32, i32* @SOCK_STREAM
%40 = load i32, i32* @IPPROTO_TCP
%41 = call i32 @socket(i32 %38, i32 %39, i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %92
47: ; preds = %37
%48 = bitcast %struct.sockaddr_in* %11 to i8*
%49 = call i32 @memset(i8* %48, i32 0, i32 12)
%50 = load i32, i32* @AF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %50, i32* %51
%52 = load i32, i32* %5
%53 = call i32 @htons(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %53, i32* %54
%55 = load i32, i32* %6
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
store i32 %55, i32* %57
%58 = load i32, i32* %9
%59 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%60 = call i32 @connect(i32 %58, %struct.sockaddr* %59, i32 12)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %47
%64 = load i32, i32* @stderr
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %92
66: ; preds = %47
%67 = load i32, i32* %9
%68 = load i32, i32* @F_GETFL
%69 = call i64 (i32, i32, ...) @fcntl(i32 %67, i32 %68)
%70 = trunc i64 %69 to i32
store i32 %70, i32* %10
%71 = load i32, i32* %10
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %66
%74 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%75 = load i32, i32* @EXIT_FAILURE
%76 = call i32 @exit(i32 %75)
unreachable
77: ; preds = %66
%78 = load i32, i32* %10
%79 = load i32, i32* @O_NONBLOCK
%80 = or i32 %78, %79
store i32 %80, i32* %10
%81 = load i32, i32* %9
%82 = load i32, i32* @F_SETFL
%83 = load i32, i32* %10
%84 = call i64 (i32, i32, ...) @fcntl(i32 %81, i32 %82, i32 %83)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %77
%87 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %77
%91 = load i32, i32* %9
store i32 %91, i32* %3
br label %92
92: ; preds = %90, %63, %44, %27
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"EAGI proxy: Wrong address!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [35 x i8] c"EAGI proxy: Cannot create socket!\0A\00"
@.str.3 = [29 x i8] c"EAGI proxy: Cannot connect!\0A\00"
@F_GETFL = global i32 0
@.str.4 = [15 x i8] c"fcntl(F_GETFL)\00"
@EXIT_FAILURE = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.5 = [15 x i8] c"fcntl(F_SETFL)\00"
define i32 @connect_to_host(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @htonl(i32 2130706433)
br label %23
9: ; preds = %2
%10 = tail call i32 @inet_addr(i8* %0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %23
12: ; preds = %9
%13 = tail call %struct.hostent* @gethostbyname(i8* %0)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %65
18: ; preds = %12
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to i32*
%22 = load i32, i32* %21
br label %23
23: ; preds = %9, %18, %7
%24 = phi i32 [ %22, %18 ], [ %10, %9 ], [ %8, %7 ]
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = load i32, i32* @IPPROTO_TCP
%28 = tail call i32 @socket(i32 %25, i32 %26, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %65
33: ; preds = %23
%34 = call i32 @memset(i8* %4, i32 0, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %35, i32* %36
%37 = call i32 @htons(i32 %1)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %24, i32* %39
%40 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%41 = call i32 @connect(i32 %28, %struct.sockaddr* %40, i32 12)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %65
46: ; preds = %33
%47 = load i32, i32* @F_GETFL
%48 = call i64 (i32, i32, ...) @fcntl(i32 %28, i32 %47)
%49 = trunc i64 %48 to i32
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %46
%56 = load i32, i32* @O_NONBLOCK
%57 = or i32 %56, %49
%58 = load i32, i32* @F_SETFL
%59 = call i64 (i32, i32, ...) @fcntl(i32 %28, i32 %58, i32 %57)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %55, %43, %30, %15
%66 = phi i32 [ -1, %30 ], [ -1, %43 ], [ -1, %15 ], [ %28, %55 ]
ret i32 %66
}