#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_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ addr_remote;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ display_perror(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *working(int);
char *socket_rtinfo(char *server, int port) {
  int sockfd;
  struct sockaddr_in addr_remote;
  struct hostent *hent;
  addr_remote.sin_family = AF_INET;
  addr_remote.sin_port = htons(port);
  if ((hent = gethostbyname(server)) == NULL) {
    display_perror("gethostbyname");
    return NULL;
  }
  memcpy(&addr_remote.sin_addr, hent->h_addr_list[0], hent->h_length);
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    display_perror("socket");
    return NULL;
  }
  if (connect(sockfd, (const struct sockaddr *)&addr_remote,
              sizeof(addr_remote)) < 0) {
    display_perror("connect");
    return NULL;
  }
  return working(sockfd);
}


x86 assembly (clang, -O0):
<socket_rtinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11c6 <socket_rtinfo+0x56>
lea    0xe4c(%rip),%rdi
call   1040 <display_perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    125d <socket_rtinfo+0xed>
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    121d <socket_rtinfo+0xad>
lea    0xe03(%rip),%rdi
call   1040 <display_perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    125d <socket_rtinfo+0xed>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1251 <socket_rtinfo+0xe1>
lea    0xdd6(%rip),%rdi
call   1040 <display_perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    125d <socket_rtinfo+0xed>
mov    -0x18(%rbp),%edi
call   1070 <working@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_rtinfo>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e67(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11eb <socket_rtinfo+0x7b>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
call   1060 <memcpy@plt>
mov    (%r14),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11f4 <socket_rtinfo+0x84>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     11fd <socket_rtinfo+0x8d>
mov    %ebx,%edi
call   1070 <working@plt>
jmp    120b <socket_rtinfo+0x9b>
lea    0xe0e(%rip),%rdi
jmp    1204 <socket_rtinfo+0x94>
lea    0xe13(%rip),%rdi
jmp    1204 <socket_rtinfo+0x94>
lea    0xe11(%rip),%rdi
call   1040 <display_perror@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [14 x i8] c"gethostbyname\00"
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
define i8* @socket_rtinfo(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @htons(i32 %1)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %7, i32* %8
%9 = tail call %struct.hostent* @gethostbyname(i8* %0)
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @display_perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %35
13: ; preds = %2
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 1
%16 = load i32*, i32** %15
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i32* %14, i32 %17, i32 %19)
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = call i32 @display_perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %35
27: ; preds = %13
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @connect(i32 %23, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @display_perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %35
33: ; preds = %27
%34 = call i8* @working(i32 %23)
br label %35
35: ; preds = %33, %31, %25, %11
%36 = phi i8* [ null, %11 ], [ null, %25 ], [ null, %31 ], [ %34, %33 ]
ret i8* %36
}