#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;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int opensocket(char *hostname, int port) {
  struct sockaddr_in serv_addr;
  struct hostent *server;
  int s;
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0) {
    fprintf(stderr, "ERROR opening socket\n");
    exit(1);
  }
  server = gethostbyname(hostname);
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(1);
  }
  memset((char *)&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(port);
  if (connect(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    fprintf(stderr, "ERROR connecting\n");
    exit(1);
  }
  return s;
}


x86 assembly (clang, -O0):
<opensocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d4 <opensocket+0x54>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120b <opensocket+0x8b>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <bcopy@plt>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1287 <opensocket+0x107>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opensocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e4f(%rip),%r12
mov    (%r12),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     121c <opensocket+0x9c>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     122e <opensocket+0xae>
mov    %rax,%rbp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbp),%rsi
lea    0x4(%rsp),%rdi
mov    0x0(%rbp),%edx
call   1070 <memmove@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1240 <opensocket+0xc0>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
jmp    1250 <opensocket+0xd0>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
jmp    1250 <opensocket+0xd0>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"ERROR opening socket\0A\00"
@.str.1 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.2 = [18 x i8] c"ERROR connecting\0A\00"
define i32 @opensocket(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.hostent*
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 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 eq %struct.hostent* %20, null
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = bitcast %struct.sockaddr_in* %5 to i8*
%28 = call i32 @memset(i8* %27, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %29, i32* %30
%31 = load %struct.hostent*, %struct.hostent** %6
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 1
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to i8*
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = bitcast i32* %36 to i8*
%38 = load %struct.hostent*, %struct.hostent** %6
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @bcopy(i8* %34, i8* %37, i32 %40)
%42 = load i32, i32* %4
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* %7
%46 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%47 = call i64 @connect(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %26
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %26
%54 = load i32, i32* %7
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"ERROR opening socket\0A\00"
@.str.1 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.2 = [18 x i8] c"ERROR connecting\0A\00"
define i32 @opensocket(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 slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call %struct.hostent* @gethostbyname(i8* %0)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = call i32 @memset(i8* %4, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to i8*
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%28 = bitcast i32* %27 to i8*
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @htons(i32 %1)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %31, i32* %32
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @connect(i32 %7, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %20
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %20
ret i32 %7
}