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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket(char *host, int port) {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("error opening socket");
    exit(1);
  };
  struct hostent *server = gethostbyname(host);
  if (!server) {
    perror("could not resolve hostname");
    exit(1);
  };
  in_addr_t in_addr =
      inet_addr(inet_ntoa(*(struct in_addr *)*(server->h_addr_list)));
  struct sockaddr_in serv_addr;
  serv_addr.sin_addr.s_addr = in_addr;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("error connecting to remote host");
    exit(1);
  };
  return sockfd;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11cb <create_socket+0x4b>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f9 <create_socket+0x79>
lea    0xe2b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%edi
call   1050 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1262 <create_socket+0xe2>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
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    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     120b <create_socket+0x8b>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1214 <create_socket+0x94>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%edi
call   1050 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     121d <create_socket+0x9d>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdee(%rip),%rdi
jmp    1224 <create_socket+0xa4>
lea    0xdfa(%rip),%rdi
jmp    1224 <create_socket+0xa4>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"error opening socket\00"
@.str.1 = [27 x i8] c"could not resolve hostname\00"
@.str.2 = [32 x i8] c"error connecting to remote host\00"
define i32 @create_socket(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %6
%20 = load %struct.hostent*, %struct.hostent** %6
%21 = icmp ne %struct.hostent* %20, null
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = load %struct.hostent*, %struct.hostent** %6
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i64*, i64** %27
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to %struct.in_addr*
%31 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @inet_ntoa(i32 %32)
%34 = call i32 @inet_addr(i32 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %38, i32* %39
%40 = load i32, i32* %4
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %41, i32* %42
%43 = load i32, i32* %5
%44 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%45 = call i64 @connect(i32 %43, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %25
%48 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %25
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"error opening socket\00"
@.str.1 = [27 x i8] c"could not resolve hostname\00"
@.str.2 = [32 x i8] c"error connecting to remote host\00"
define i32 @create_socket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call %struct.hostent* @gethostbyname(i8* %0)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%19 = load i64*, i64** %18
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to %struct.in_addr*
%22 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %21, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i32 @inet_ntoa(i32 %23)
%25 = tail call i32 @inet_addr(i32 %24)
%26 = bitcast %struct.sockaddr_in* %3 to i8*
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %28, i32* %29
%30 = tail call i32 @htons(i32 %1)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i64 @connect(i32 %6, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %17
%36 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %17
ret i32 %6
}