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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Error(char *);
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int Connect(char *hostname, int port) {
  int sock;
  struct hostent *host;
  struct sockaddr_in host_addr;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    Error("socket()");
  }
  host = gethostbyname(hostname);
  if (host == NULL) {
    Error("gethostbyname()");
  }
  host_addr.sin_family = AF_INET;
  host_addr.sin_port = htons(port);
  host_addr.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
  memset(&host_addr.sin_zero, 0, 8);
  int status =
      connect(sock, (struct sockaddr *)&host_addr, sizeof(struct sockaddr));
  if (status == 0) {
    return sock;
  }
  return 0;
}


x86 assembly (clang, -O0):
<Connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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)
cmpl   $0x0,-0x18(%rbp)
jge    11a1 <Connect+0x41>
lea    0xe64(%rip),%rdi
call   1030 <Error@plt>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c5 <Connect+0x65>
lea    0xe49(%rip),%rdi
call   1030 <Error@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1224 <Connect+0xc4>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122b <Connect+0xcb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e71(%rip),%r15
mov    (%r15),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119c <Connect+0x3c>
lea    0xe69(%rip),%rdi
call   1030 <Error@plt>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11b8 <Connect+0x58>
lea    0xe56(%rip),%rdi
call   1030 <Error@plt>
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %eax,%eax
cmovne %r14d,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [16 x i8] c"gethostbyname()\00"
define i32 @Connect(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 %11
9: ; preds = %2
%10 = tail call i32 @Error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = tail call %struct.hostent* @gethostbyname(i8* %0)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @Error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%22 = load i64*, i64** %21
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i32*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%26 = load i32, i32* %24
store i32 %26, i32* %25
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%28 = call i32 @memset(i32* %27, i32 0, i32 8)
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @connect(i32 %7, %struct.sockaddr* %29, i32 4)
%31 = icmp eq i32 %30, 0
%32 = select i1 %31, i32 %7, i32 0
ret i32 %32
}