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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
unsigned long INADDR_NONE;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
unsigned long inet_addr(char const *);
int /*<<< orphan*/ memcpy(char *, unsigned long *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ message(int /*<<< orphan*/, char *, char const *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int socket_open(const char *name, int port) {
  struct sockaddr_in server;
  struct hostent *host_info;
  unsigned long addr;
  int sock;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    message(0, "[hddtemp] failed to create socket: %s", strerror(errno));
    return -1;
  }
  memset(&server, 0, sizeof(server));
  if ((addr = inet_addr(name)) != INADDR_NONE) {
    memcpy((char *)&server.sin_addr, &addr, sizeof(addr));
  } else {
    host_info = gethostbyname(name);
    if (NULL == host_info) {
      message(0, "[hddtemp] Unknown server: %s", name);
      return -1;
    }
    memcpy((char *)&server.sin_addr, host_info->h_addr, host_info->h_length);
  }
  server.sin_family = AF_INET;
  server.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
    message(0, "[hddtemp] can't connect to server %s: %s", name,
            strerror(errno));
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<socket_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11f2 <socket_open+0x62>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
xor    %edi,%edi
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1030 <message@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <socket_open+0x16e>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %rax,-0x30(%rbp)
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
je     123a <socket_open+0xaa>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
jmp    128e <socket_open+0xfe>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1273 <socket_open+0xe3>
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1030 <message@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <socket_open+0x16e>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <memcpy@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    12f8 <socket_open+0x168>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
xor    %edi,%edi
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1030 <message@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <socket_open+0x16e>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1205 <socket_open+0x75>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
mov    %rax,0x18(%rsp)
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <socket_open+0x91>
mov    %rbx,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1289 <socket_open+0xf9>
lea    0x10(%rsp),%rdi
mov    (%rax),%rsi
mov    0x8(%rax),%edx
jmp    1230 <socket_open+0xa0>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
lea    0xde3(%rip),%rsi
xor    %edi,%edi
jmp    1295 <socket_open+0x105>
lea    0x10(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
jns    12a1 <socket_open+0x111>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdca(%rip),%rsi
xor    %edi,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <message@plt>
jmp    129c <socket_open+0x10c>
lea    0xd96(%rip),%rsi
xor    %edi,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <message@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [38 x i8] c"[hddtemp] failed to create socket: %s\00"
@errno = global i32 0
@INADDR_NONE = global i64 0
@.str.1 = [29 x i8] c"[hddtemp] Unknown server: %s\00"
@AF_INET = global i32 0
@.str.2 = [41 x i8] c"[hddtemp] can't connect to server %s: %s\00"
define i32 @socket_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i8*
%20 = call i32 (i32, i8*, i8*, ...) @message(i32 0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 -1, i32* %3
br label %66
21: ; preds = %2
%22 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%23 = load i8*, i8** %4
%24 = call i64 @inet_addr(i8* %23)
store i64 %24, i64* %8
%25 = load i64, i64* @INADDR_NONE
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%29 = bitcast i32* %28 to i8*
%30 = call i32 @memcpy(i8* %29, i64* %8, i32 8)
br label %49
31: ; preds = %21
%32 = load i8*, i8** %4
%33 = call %struct.hostent* @gethostbyname(i8* %32)
store %struct.hostent* %33, %struct.hostent** %7
%34 = load %struct.hostent*, %struct.hostent** %7
%35 = icmp eq %struct.hostent* null, %34
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i8*, i8** %4
%38 = call i32 (i32, i8*, i8*, ...) @message(i32 0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %37)
store i32 -1, i32* %3
br label %66
39: ; preds = %31
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%41 = bitcast i32* %40 to i8*
%42 = load %struct.hostent*, %struct.hostent** %7
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i32 0, i32 0
%44 = load i64*, i64** %43
%45 = load %struct.hostent*, %struct.hostent** %7
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 @memcpy(i8* %41, i64* %44, i32 %47)
br label %49
49: ; preds = %39, %27
%50 = load i32, i32* @AF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %50, i32* %51
%52 = load i32, i32* %5
%53 = call i32 @htons(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %53, i32* %54
%55 = load i32, i32* %9
%56 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%57 = call i64 @connect(i32 %55, %struct.sockaddr* %56, i32 12)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %49
%60 = load i8*, i8** %4
%61 = load i32, i32* @errno
%62 = call i32 @strerror(i32 %61)
%63 = call i32 (i32, i8*, i8*, ...) @message(i32 0, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %60, i32 %62)
store i32 -1, i32* %3
br label %66
64: ; preds = %49
%65 = load i32, i32* %9
store i32 %65, i32* %3
br label %66
66: ; preds = %64, %59, %36, %15
%67 = load i32, i32* %3
ret i32 %67
}

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