#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*/ dest_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char const *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int clientsock(int UDPorTCP, const char *destination, int portN) {
  struct hostent *phe;
  struct sockaddr_in dest_addr;
  int sock;
  bzero((char *)&dest_addr, sizeof(dest_addr));
  dest_addr.sin_family = AF_INET;
  dest_addr.sin_port = htons(portN);
  if ((phe = gethostbyname(destination)) != 0)
    bcopy(phe->h_addr, (char *)&dest_addr.sin_addr, phe->h_length);
  else if (inet_aton(destination, &(dest_addr.sin_addr)) == 0)
    return -2;
  sock = socket(PF_INET, UDPorTCP, 0);
  if (sock < 0)
    return -3;
  if (connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0)
    return -4;
  return sock;
}


x86 assembly (clang, -O0):
<clientsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11d9 <clientsock+0x69>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <bcopy@plt>
jmp    1201 <clientsock+0x91>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <inet_aton@plt>
cmp    $0x0,%rax
jne    11fc <clientsock+0x8c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    125a <clientsock+0xea>
jmp    1201 <clientsock+0x91>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    122d <clientsock+0xbd>
movl   $0xfffffffd,-0x4(%rbp)
jmp    125a <clientsock+0xea>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1254 <clientsock+0xe4>
movl   $0xfffffffc,-0x4(%rbp)
jmp    125a <clientsock+0xea>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clientsock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsp,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11c1 <clientsock+0x51>
mov    (%rax),%edx
mov    0x4(%rax),%edi
mov    %rsp,%rsi
call   1060 <bcopy@plt>
jmp    11d1 <clientsock+0x61>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <inet_aton@plt>
test   %rax,%rax
je     120d <clientsock+0x9d>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    %r14d,%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1206 <clientsock+0x96>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
mov    $0xfffffffc,%eax
cmovns %ebx,%eax
jmp    1212 <clientsock+0xa2>
mov    $0xfffffffd,%eax
jmp    1212 <clientsock+0xa2>
mov    $0xfffffffe,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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