#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_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addrtype;
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
#define ECONNREFUSED 131
#define EINPROGRESS 130
#define EISCONN 129
#define ETIMEDOUT 128
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int errno;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memcpy(void *, void *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int PMII_Connect_to_pm(char *hostname, int portnum) {
  struct hostent *hp;
  struct sockaddr_in sa;
  int fd;
  int optval = 1;
  int q_wait = 1;
  hp = gethostbyname(hostname);
  if (!hp) {
    ;
    return -1;
  }
  memset((void *)&sa, 0, sizeof(sa));
  memcpy((void *)&sa.sin_addr, (void *)hp->h_addr, hp->h_length);
  sa.sin_family = hp->h_addrtype;
  sa.sin_port = htons((unsigned short)portnum);
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    ;
    return -1;
  }
  if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&optval,
                 sizeof(optval))) {
    perror("Error calling setsockopt:");
  }
  if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    switch (errno) {
    case ECONNREFUSED:;
      if (q_wait)
        close(fd);
      return -1;
    case EINPROGRESS:
      break;
    case EISCONN:
      break;
    case ETIMEDOUT:;
      return -1;
    default:;
      return -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<PMII_Connect_to_pm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <PMII_Connect_to_pm+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <PMII_Connect_to_pm+0x185>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
call   1080 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1246 <PMII_Connect_to_pm+0xb6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <PMII_Connect_to_pm+0x185>
mov    -0x34(%rbp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1280 <PMII_Connect_to_pm+0xf0>
lea    0xd95(%rip),%rdi
call   1090 <perror@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    130f <PMII_Connect_to_pm+0x17f>
mov    0x2d1e(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
sub    $0x3,%eax
ja     12fe <PMII_Connect_to_pm+0x16e>
mov    -0x48(%rbp),%rax
lea    0xd3f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpl   $0x0,-0x3c(%rbp)
je     12dc <PMII_Connect_to_pm+0x14c>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <PMII_Connect_to_pm+0x185>
jmp    130a <PMII_Connect_to_pm+0x17a>
jmp    130a <PMII_Connect_to_pm+0x17a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <PMII_Connect_to_pm+0x185>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <PMII_Connect_to_pm+0x185>
jmp    130f <PMII_Connect_to_pm+0x17f>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PMII_Connect_to_pm>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
movl   $0x1,0x4(%rsp)
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1265 <PMII_Connect_to_pm+0xd5>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    0x10(%rsp),%rdi
mov    0x8(%rbx),%rsi
mov    0x4(%rbx),%edx
call   1080 <memcpy@plt>
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1265 <PMII_Connect_to_pm+0xd5>
mov    %eax,%ebx
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    1273 <PMII_Connect_to_pm+0xe3>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    126a <PMII_Connect_to_pm+0xda>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
lea    -0x81(%rax),%ecx
cmp    $0x2,%ecx
jb     126a <PMII_Connect_to_pm+0xda>
cmp    $0x83,%eax
jne    1265 <PMII_Connect_to_pm+0xd5>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
jmp    122d <PMII_Connect_to_pm+0x9d>

LLVM IR (-O0):
%struct.hostent = type { i32, i32, i64 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str = [26 x i8] c"Error calling setsockopt:\00"
@errno = global i32 0
define i32 @PMII_Connect_to_pm(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %9
store i32 1, i32* %10
%11 = load i8*, i8** %4
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %6
%13 = load %struct.hostent*, %struct.hostent** %6
%14 = icmp ne %struct.hostent* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 -1, i32* %3
br label %73
16: ; preds = %2
%17 = bitcast %struct.sockaddr_in* %7 to i8*
%18 = call i32 @memset(i8* %17, i32 0, i32 12)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%20 = bitcast i32* %19 to i8*
%21 = load %struct.hostent*, %struct.hostent** %6
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i32 0, i32 2
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i8*
%25 = load %struct.hostent*, %struct.hostent** %6
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i32 @memcpy(i8* %20, i8* %24, i32 %27)
%29 = load %struct.hostent*, %struct.hostent** %6
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* %5
%34 = trunc i32 %33 to i16
%35 = call i32 @htons(i16 zeroext %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* @AF_INET
%38 = load i32, i32* @SOCK_STREAM
%39 = call i32 @socket(i32 %37, i32 %38, i32 0)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %16
store i32 -1, i32* %3
br label %73
43: ; preds = %16
%44 = load i32, i32* %8
%45 = load i32, i32* @IPPROTO_TCP
%46 = load i32, i32* @TCP_NODELAY
%47 = bitcast i32* %9 to i8*
%48 = call i64 @setsockopt(i32 %44, i32 %45, i32 %46, i8* %47, i32 4)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %43
%51 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %52
52: ; preds = %50, %43
%53 = load i32, i32* %8
%54 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%55 = call i64 @connect(i32 %53, %struct.sockaddr* %54, i32 12)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %71
57: ; preds = %52
%58 = load i32, i32* @errno
switch i32 %58, label %69 [
i32 131, label %59
i32 130, label %66
i32 129, label %67
i32 128, label %68
]
59: ; preds = %57
%60 = load i32, i32* %10
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %59
store i32 -1, i32* %3
br label %73
66: ; preds = %57
br label %70
67: ; preds = %57
br label %70
68: ; preds = %57
store i32 -1, i32* %3
br label %73
69: ; preds = %57
store i32 -1, i32* %3
br label %73
70: ; preds = %67, %66
br label %71
71: ; preds = %70, %52
%72 = load i32, i32* %8
store i32 %72, i32* %3
br label %73
73: ; preds = %71, %69, %68, %65, %42, %15
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str = [26 x i8] c"Error calling setsockopt:\00"
@errno = global i32 0
define i32 @PMII_Connect_to_pm(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %45, label %9
9: ; preds = %2
%10 = call i32 @memset(i8* %5, i32 0, i32 12)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%12 = bitcast i32* %11 to i8*
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 2
%14 = load i64, i64* %13
%15 = inttoptr i64 %14 to i8*
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 @memcpy(i8* %12, i8* %15, i32 %17)
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = trunc i32 %1 to i16
%23 = call i32 @htons(i16 zeroext %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %45, label %29
29: ; preds = %9
%30 = load i32, i32* @IPPROTO_TCP
%31 = load i32, i32* @TCP_NODELAY
%32 = call i64 @setsockopt(i32 %27, i32 %30, i32 %31, i8* %6, i32 4)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i64 @connect(i32 %27, %struct.sockaddr* %37, i32 12)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i32, i32* @errno
switch i32 %41, label %45 [
i32 131, label %42
i32 130, label %44
i32 129, label %44
]
42: ; preds = %40
%43 = call i32 @close(i32 %27)
br label %45
44: ; preds = %40, %40, %36
br label %45
45: ; preds = %40, %9, %2, %44, %42
%46 = phi i32 [ %27, %44 ], [ -1, %42 ], [ -1, %2 ], [ -1, %9 ], [ -1, %40 ]
ret i32 %46
}