#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*/ sin;
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ bcopy(void *, void *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(void *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_pm(char *hostname, int portnum) {
  struct hostent *hp;
  struct sockaddr_in sin;
  int sfd, optval = 1;
  hp = gethostbyname(hostname);
  if (!hp) {
    printf("gethostbyname failed\n");
    exit(-1);
  }
  bzero((void *)&sin, sizeof(sin));
  bcopy((void *)hp->h_addr, (void *)&sin.sin_addr, hp->h_length);
  sin.sin_family = hp->h_addrtype;
  sin.sin_port = htons((unsigned short)portnum);
  sfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sfd < 0) {
    printf("socket failed\n");
    exit(-1);
  }
  if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&optval,
                 sizeof(optval))) {
    perror("Error calling setsockopt:");
  }
  if (connect(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    printf("connect failed\n");
    exit(-1);
  }
  return sfd;
}


x86 assembly (clang, -O0):
<connect_to_pm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e4 <connect_to_pm+0x44>
lea    0xe2b(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <bcopy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1261 <connect_to_pm+0xc1>
lea    0xdc4(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     129b <connect_to_pm+0xfb>
lea    0xd8f(%rip),%rdi
call   1080 <perror@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    12cc <connect_to_pm+0x12c>
lea    0xd82(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_pm>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
movl   $0x1,0x4(%rsp)
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1268 <connect_to_pm+0xc8>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x8(%rbx),%rsi
lea    0x10(%rsp),%rdi
mov    0x4(%rbx),%edx
call   1070 <memmove@plt>
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
movzwl %bp,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1271 <connect_to_pm+0xd1>
mov    %eax,%ebx
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jne    125a <connect_to_pm+0xba>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     127a <connect_to_pm+0xda>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
call   1080 <perror@plt>
jmp    123b <connect_to_pm+0x9b>
lea    0xdab(%rip),%rdi
jmp    1281 <connect_to_pm+0xe1>
lea    0xdc6(%rip),%rdi
jmp    1281 <connect_to_pm+0xe1>
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32, i32, i64 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"gethostbyname failed\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"socket failed\0A\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.2 = [26 x i8] c"Error calling setsockopt:\00"
@.str.3 = [16 x i8] c"connect failed\0A\00"
define i32 @connect_to_pm(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.hostent*
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 1, i32* %8
%9 = load i8*, i8** %3
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %5
%11 = load %struct.hostent*, %struct.hostent** %5
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = bitcast %struct.sockaddr_in* %6 to i8*
%18 = call i32 @bzero(i8* %17, i32 12)
%19 = load %struct.hostent*, %struct.hostent** %5
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 2
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i8*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%24 = bitcast i32* %23 to i8*
%25 = load %struct.hostent*, %struct.hostent** %5
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i32 @bcopy(i8* %22, i8* %24, i32 %27)
%29 = load %struct.hostent*, %struct.hostent** %5
%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* %6, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* %4
%34 = trunc i32 %33 to i16
%35 = call i32 @htons(i16 zeroext %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, 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* %7
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %16
%43 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %16
%46 = load i32, i32* %7
%47 = load i32, i32* @IPPROTO_TCP
%48 = load i32, i32* @TCP_NODELAY
%49 = bitcast i32* %8 to i8*
%50 = call i64 @setsockopt(i32 %46, i32 %47, i32 %48, i8* %49, i32 4)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %52, %45
%55 = load i32, i32* %7
%56 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%57 = call i64 @connect(i32 %55, %struct.sockaddr* %56, i32 12)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 -1)
unreachable
62: ; preds = %54
%63 = load i32, i32* %7
ret i32 %63
}

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.2 = [26 x i8] c"Error calling setsockopt:\00"
@str = [21 x i8] c"gethostbyname failed\00"
@str.4 = [15 x i8] c"connect failed\00"
@str.5 = [14 x i8] c"socket failed\00"
define i32 @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 %9, label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = call i32 @bzero(i8* %5, i32 12)
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 2
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i8*
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%18 = bitcast i32* %17 to i8*
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = trunc i32 %1 to i16
%25 = tail call i32 @htons(i16 zeroext %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = tail call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %12
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
%33 = tail call i32 @exit(i32 -1)
unreachable
34: ; preds = %12
%35 = load i32, i32* @IPPROTO_TCP
%36 = load i32, i32* @TCP_NODELAY
%37 = call i64 @setsockopt(i32 %29, i32 %35, i32 %36, i8* %6, i32 4)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i64 @connect(i32 %29, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %41
ret i32 %29
}