#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_family;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_connect(char *host, in_port_t port) {
  struct hostent *hp;
  struct sockaddr_in addr;
  int on = 1, sock;
  if ((hp = gethostbyname(host)) == NULL) {
    herror("gethostbyname");
    exit(1);
  }
  bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
  addr.sin_port = htons(port);
  addr.sin_family = AF_INET;
  sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int));
  if (sock == -1) {
    perror("setsockopt");
    exit(1);
  }
  if (connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) ==
      -1) {
    perror("connect");
    exit(1);
  }
  return sock;
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d3 <socket_connect+0x43>
lea    0xe3c(%rip),%rdi
call   1060 <herror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1070 <bcopy@plt>
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmpl   $0xffffffff,-0x30(%rbp)
jne    126b <socket_connect+0xdb>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    129b <socket_connect+0x10b>
lea    0xd8d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
movl   $0x1,0x4(%rsp)
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     123c <socket_connect+0xac>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    0x10(%rsp),%rsi
call   1070 <bcopy@plt>
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%r14
mov    (%r14),%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
mov    (%r14),%esi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%ebx
je     1252 <socket_connect+0xc2>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     125b <socket_connect+0xcb>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdbd(%rip),%rdi
call   1060 <herror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdb5(%rip),%rdi
jmp    1262 <socket_connect+0xd2>
lea    0xdb7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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