#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  char *h_name;
  char *h_addr;
  int /*<<< orphan*/ h_length;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, struct in_addr *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int graphite_connect(char *graphite_addr) {
  int sockfd, n;
  int portno = 2003;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  struct in_addr ipv4addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("Failed to open sock\n");
    return -1;
  }
  inet_pton(AF_INET, graphite_addr, &ipv4addr);
  server = gethostbyname(graphite_addr);
  if (!server) {
    printf("Failed to resolve host\n");
    return -2;
  }
  printf("h_name: %s\nh_addr: %s\n", server->h_name, server->h_addr);
  serv_addr.sin_family = AF_INET;
  bcopy(server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
  serv_addr.sin_port = htons(portno);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) <
      0) {
    printf("failed to connec to socket\n");
    return -3;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<graphite_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x7d3,-0x1c(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c3 <graphite_connect+0x53>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <graphite_connect+0x126>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <inet_pton@plt>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120b <graphite_connect+0x9b>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1296 <graphite_connect+0x126>
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edx
call   1070 <bcopy@plt>
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1290 <graphite_connect+0x120>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1296 <graphite_connect+0x126>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<graphite_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
mov    0x2e56(%rip),%r14
mov    (%r14),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1233 <graphite_connect+0xb3>
mov    %eax,%ebp
mov    (%r14),%edi
lea    0x18(%rsp),%rdx
mov    %rbx,%rsi
call   1070 <inet_pton@plt>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1246 <graphite_connect+0xc6>
mov    %rax,%rbx
mov    (%rax),%rsi
mov    0x8(%rax),%rdx
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rbx),%rsi
lea    0xc(%rsp),%rdi
mov    0x10(%rbx),%edx
call   1080 <memmove@plt>
mov    $0x7d3,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1090 <connect@plt>
test   %rax,%rax
jns    1257 <graphite_connect+0xd7>
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%ebp
jmp    1257 <graphite_connect+0xd7>
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1257 <graphite_connect+0xd7>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i8*, i8*, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Failed to open sock\0A\00"
@.str.1 = [24 x i8] c"Failed to resolve host\0A\00"
@.str.2 = [23 x i8] c"h_name: %s\0Ah_addr: %s\0A\00"
@.str.3 = [28 x i8] c"failed to connec to socket\0A\00"
define i32 @graphite_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
%9 = alloca %struct.in_addr
store i8* %0, i8** %3
store i32 2003, i32* %6
%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* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %58
17: ; preds = %1
%18 = load i32, i32* @AF_INET
%19 = load i8*, i8** %3
%20 = call i32 @inet_pton(i32 %18, i8* %19, %struct.in_addr* %9)
%21 = load i8*, i8** %3
%22 = call %struct.hostent* @gethostbyname(i8* %21)
store %struct.hostent* %22, %struct.hostent** %8
%23 = load %struct.hostent*, %struct.hostent** %8
%24 = icmp ne %struct.hostent* %23, null
br i1 %24, label %27, label %25
25: ; preds = %17
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %2
br label %58
27: ; preds = %17
%28 = load %struct.hostent*, %struct.hostent** %8
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = load %struct.hostent*, %struct.hostent** %8
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 1
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %30, i8* %33)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %35, i32* %36
%37 = load %struct.hostent*, %struct.hostent** %8
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 1
%39 = load i8*, i8** %38
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
%42 = bitcast i32* %41 to i8*
%43 = load %struct.hostent*, %struct.hostent** %8
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = call i32 @bcopy(i8* %39, i8* %42, i32 %45)
%47 = load i32, i32* %6
%48 = call i32 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %48, i32* %49
%50 = load i32, i32* %4
%51 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%52 = call i64 @connect(i32 %50, %struct.sockaddr* %51, i32 4)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %27
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
store i32 -3, i32* %2
br label %58
56: ; preds = %27
%57 = load i32, i32* %4
store i32 %57, i32* %2
br label %58
58: ; preds = %56, %54, %25, %15
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i8*, i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [23 x i8] c"h_name: %s\0Ah_addr: %s\0A\00"
@str = [23 x i8] c"Failed to resolve host\00"
@str.4 = [27 x i8] c"failed to connec to socket\00"
@str.5 = [20 x i8] c"Failed to open sock\00"
define i32 @graphite_connect(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.in_addr
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast %struct.in_addr* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
br label %39
12: ; preds = %1
%13 = load i32, i32* @AF_INET
%14 = call i32 @inet_pton(i32 %13, i8* %0, %struct.in_addr* %3)
%15 = call %struct.hostent* @gethostbyname(i8* %0)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %39
19: ; preds = %12
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 1
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %23)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %25, i32* %26
%27 = load i8*, i8** %22
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%29 = bitcast i32* %28 to i8*
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @htons(i32 2003)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%35 = call i64 @connect(i32 %8, %struct.sockaddr* %34, i32 4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %19
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.4, i64 0, i64 0))
br label %39
39: ; preds = %19, %37, %17, %10
%40 = phi i32 [ -1, %10 ], [ -3, %37 ], [ -2, %17 ], [ %8, %19 ]
ret i32 %40
}