#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_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
  int /*<<< orphan*/ h_addrtype;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, void *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_socket(char *host, int port) {
  int fd;
  struct sockaddr_in saddr;
  struct hostent *hent;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    perror("Cannot create socket");
    exit(1);
  }
  if ((hent = gethostbyname(host)) == NULL) {
    perror("Cannot lookup host");
    exit(1);
  }
  bzero(&saddr, sizeof(saddr));
  saddr.sin_family = hent->h_addrtype;
  bcopy(hent->h_addr, &saddr.sin_addr, hent->h_length);
  saddr.sin_port = htons(port);
  if (connect(fd, (void *)&saddr, sizeof(saddr)) == -1) {
    perror("Connection to galfa failed");
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_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   $0xffffffff,-0x10(%rbp)
jne    11cb <open_socket+0x4b>
lea    0xe44(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11f8 <open_socket+0x78>
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1050 <bcopy@plt>
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1265 <open_socket+0xe5>
lea    0xdd2(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1216 <open_socket+0x96>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     121f <open_socket+0x9f>
mov    %rax,%rbp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x8(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%edi
lea    0xc(%rsp),%rsi
call   1050 <bcopy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1228 <open_socket+0xa8>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde3(%rip),%rdi
jmp    122f <open_socket+0xaf>
lea    0xdef(%rip),%rdi
jmp    122f <open_socket+0xaf>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Cannot create socket\00"
@.str.1 = [19 x i8] c"Cannot lookup host\00"
@.str.2 = [27 x i8] c"Connection to galfa failed\00"
define i32 @open_socket(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
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* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %7
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%25 = load %struct.hostent*, %struct.hostent** %7
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %27, i32* %28
%29 = load %struct.hostent*, %struct.hostent** %7
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%33 = load %struct.hostent*, %struct.hostent** %7
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @bcopy(i32 %31, i32* %32, i32 %35)
%37 = load i32, i32* %4
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %5
%41 = bitcast %struct.sockaddr_in* %6 to i8*
%42 = call i32 @connect(i32 %40, i8* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %23
%45 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %23
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Cannot create socket\00"
@.str.1 = [19 x i8] c"Cannot lookup host\00"
@.str.2 = [27 x i8] c"Connection to galfa failed\00"
define i32 @open_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 %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* 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 = tail call %struct.hostent* @gethostbyname(i8* %0)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 2
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @bcopy(i32 %24, i32* %25, i32 %27)
%29 = call i32 @htons(i32 %1)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = call i32 @connect(i32 %7, i8* %4, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %18
%34 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %18
ret i32 %7
}