#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;
};
typedef int /*<<< orphan*/ server_info;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket(char *host, int port) {
  int fd;
  struct sockaddr_in server_info;
  struct hostent *host_ptr;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    return -1;
  bzero((char *)&server_info, sizeof(struct sockaddr_in));
  server_info.sin_family = AF_INET;
  server_info.sin_port = htons(port);
  if ((host_ptr = gethostbyname(host)) == NULL)
    return -1;
  bcopy(host_ptr->h_addr, (char *)&server_info.sin_addr, host_ptr->h_length);
  if (connect(fd, (struct sockaddr *)&server_info, sizeof(server_info)) == -1)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11a0 <create_socket+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <create_socket+0xca>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e33(%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   1040 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11e8 <create_socket+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <create_socket+0xca>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1050 <bcopy@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1224 <create_socket+0xc4>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <create_socket+0xca>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e6f(%rip),%r12
mov    (%r12),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11ee <create_socket+0x8e>
mov    %eax,%ebx
mov    %rsp,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11ee <create_socket+0x8e>
mov    (%rax),%edx
mov    0x4(%rax),%edi
mov    %rsp,%rbp
mov    %rbp,%rsi
call   1050 <bcopy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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
@SOCK_STREAM = global i32 0
define i32 @create_socket(i8* %0, i32 %1) {
%3 = alloca i32
%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 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %43
14: ; preds = %2
%15 = bitcast %struct.sockaddr_in* %7 to i8*
%16 = call i32 @bzero(i8* %15, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* %5
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i8*, i8** %4
%23 = call %struct.hostent* @gethostbyname(i8* %22)
store %struct.hostent* %23, %struct.hostent** %8
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %26
25: ; preds = %14
store i32 -1, i32* %3
br label %43
26: ; preds = %14
%27 = load %struct.hostent*, %struct.hostent** %8
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%31 = bitcast i32* %30 to i8*
%32 = load %struct.hostent*, %struct.hostent** %8
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @bcopy(i32 %29, i8* %31, i32 %34)
%36 = load i32, i32* %6
%37 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%38 = call i32 @connect(i32 %36, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %26
store i32 -1, i32* %3
br label %43
41: ; preds = %26
%42 = load i32, i32* %6
store i32 %42, i32* %3
br label %43
43: ; preds = %41, %40, %25, %13
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @create_socket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %27, label %9
9: ; preds = %2
%10 = call i32 @bzero(i8* %4, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 %1)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = tail call %struct.hostent* @gethostbyname(i8* %0)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %27, label %17
17: ; preds = %9
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @bcopy(i32 %19, i8* %4, i32 %21)
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i32 @connect(i32 %7, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
%26 = select i1 %25, i32 -1, i32 %7
br label %27
27: ; preds = %17, %9, %2
%28 = phi i32 [ -1, %2 ], [ -1, %9 ], [ %26, %17 ]
ret i32 %28
}