#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 */
typedef int /*<<< orphan*/ u_short;
struct sockaddr_un {
  int sin_family;
  int sun_family;
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr_in {
  int sin_family;
  int sun_family;
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int h_addrtype;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int AF_INET;
int AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int parseSocketVariable(char *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int connectToRtprof(void) {
  int socketType;
  char hostBuffer[160];
  int s;
  socketType = parseSocketVariable(hostBuffer, 160);
  if (socketType == AF_INET) {
    struct sockaddr_in sa;
    struct hostent *hp;
    if ((hp = gethostbyname(hostBuffer)) == NULL)
      return -1;
    memset(&sa, 0, sizeof(sa));
    memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length);
    sa.sin_family = hp->h_addrtype;
    sa.sin_port = htons((u_short)4004);
    if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
      return -1;
    if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
      close(s);
      return -1;
    }
    return s;
  } else if (socketType == AF_UNIX) {
    struct sockaddr_un sa;
    memset(&sa, 0, sizeof(sa));
    strcpy(sa.sun_path, hostBuffer);
    sa.sun_family = AF_UNIX;
    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
      return -1;
    if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
      close(s);
      return -1;
    }
    return s;
  } else if (socketType < 0) {
    fprintf(stderr, "WARNING: librtprof cannot parse environment"
                    "variable RTPROF_SKT\n");
    return -1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<connectToRtprof>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
lea    -0xb0(%rbp),%rdi
mov    $0xa0,%esi
call   10a0 <parseSocketVariable@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%eax
jne    12ca <connectToRtprof+0x12a>
lea    -0xb0(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0xd0(%rbp)
cmp    $0x0,%rax
jne    11fa <connectToRtprof+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c6 <connectToRtprof+0x226>
lea    -0xc8(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
lea    -0xc8(%rbp),%rdi
add    $0x10,%rdi
mov    -0xd0(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0xd0(%rbp),%rax
mov    0x4(%rax),%edx
call   1090 <memcpy@plt>
mov    -0xd0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc8(%rbp)
mov    $0xfa4,%edi
call   1040 <htons@plt>
mov    %eax,-0xbc(%rbp)
mov    -0xd0(%rbp),%rax
mov    (%rax),%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xb4(%rbp)
cmp    $0x0,%eax
jge    1284 <connectToRtprof+0xe4>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c6 <connectToRtprof+0x226>
mov    -0xb4(%rbp),%edi
lea    -0xc8(%rbp),%rsi
mov    $0x14,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    12bc <connectToRtprof+0x11c>
mov    -0xb4(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c6 <connectToRtprof+0x226>
mov    -0xb4(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c6 <connectToRtprof+0x226>
mov    -0x8(%rbp),%eax
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%eax
jne    138a <connectToRtprof+0x1ea>
lea    -0xe8(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    -0xe0(%rbp),%edi
lea    -0xb0(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xe4(%rbp)
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
mov    0x2ca8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xb4(%rbp)
cmp    $0x0,%eax
jge    1344 <connectToRtprof+0x1a4>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c6 <connectToRtprof+0x226>
mov    -0xb4(%rbp),%edi
lea    -0xe8(%rbp),%rsi
mov    $0x14,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    137c <connectToRtprof+0x1dc>
mov    -0xb4(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c6 <connectToRtprof+0x226>
mov    -0xb4(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c6 <connectToRtprof+0x226>
cmpl   $0x0,-0x8(%rbp)
jge    13b5 <connectToRtprof+0x215>
mov    0x2c35(%rip),%rax
mov    (%rax),%edi
lea    0xc5c(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c6 <connectToRtprof+0x226>
jmp    13ba <connectToRtprof+0x21a>
jmp    13bf <connectToRtprof+0x21f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectToRtprof>:
push   %rbp
push   %rbx
sub    $0xc8,%rsp
lea    0x20(%rsp),%rdi
mov    $0xa0,%esi
call   10a0 <parseSocketVariable@plt>
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%eax
jne    122e <connectToRtprof+0x8e>
lea    0x20(%rsp),%rdi
call   1070 <gethostbyname@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     12ba <connectToRtprof+0x11a>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
lea    0x18(%rsp),%rdi
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
call   1090 <memcpy@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0xfa4,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%rbx),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
jns    1277 <connectToRtprof+0xd7>
jmp    12ba <connectToRtprof+0x11a>
mov    0x2dab(%rip),%rbx
cmp    (%rbx),%eax
jne    1293 <connectToRtprof+0xf3>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    0x10(%rsp),%edi
lea    0x20(%rsp),%rsi
call   1030 <strcpy@plt>
mov    (%rbx),%edi
mov    %edi,0xc(%rsp)
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     12ba <connectToRtprof+0x11a>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     12b3 <connectToRtprof+0x113>
mov    %ebx,%ebp
jmp    12ba <connectToRtprof+0x11a>
mov    $0xffffffff,%ebp
test   %eax,%eax
jns    12ba <connectToRtprof+0x11a>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12ba <connectToRtprof+0x11a>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32, i32, i32 }
%struct.hostent = type { i32, i32, i32 }
%struct.sockaddr_un = type { i32, i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@stderr = global i32 0
@.str = [64 x i8] c"WARNING: librtprof cannot parse environmentvariable RTPROF_SKT\0A\00"
define i32 @connectToRtprof() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [160 x i8]
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_un
%8 = getelementptr inbounds [160 x i8], [160 x i8]* %3, i64 0, i64 0
%9 = call i32 @parseSocketVariable(i8* %8, i32 160)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = load i32, i32* @AF_INET
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %53
13: ; preds = %0
%14 = getelementptr inbounds [160 x i8], [160 x i8]* %3, i64 0, i64 0
%15 = call %struct.hostent* @gethostbyname(i8* %14)
store %struct.hostent* %15, %struct.hostent** %6
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %1
br label %89
18: ; preds = %13
%19 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr_un*
%20 = call i32 @memset(%struct.sockaddr_un* %19, i32 0, i32 20)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 4
%22 = bitcast i32* %21 to i8*
%23 = load %struct.hostent*, %struct.hostent** %6
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 2
%25 = load i32, i32* %24
%26 = load %struct.hostent*, %struct.hostent** %6
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i8* %22, i32 %25, i32 %28)
%30 = load %struct.hostent*, %struct.hostent** %6
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %32, i32* %33
%34 = call i32 @htons(i32 4004)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 3
store i32 %34, i32* %35
%36 = load %struct.hostent*, %struct.hostent** %6
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = load i32, i32* @SOCK_STREAM
%40 = call i32 @socket(i32 %38, i32 %39, i32 0)
store i32 %40, i32* %4
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %18
store i32 -1, i32* %1
br label %89
43: ; preds = %18
%44 = load i32, i32* %4
%45 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%46 = call i64 @connect(i32 %44, %struct.sockaddr* %45, i32 20)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %1
br label %89
51: ; preds = %43
%52 = load i32, i32* %4
store i32 %52, i32* %1
br label %89
53: ; preds = %0
%54 = load i32, i32* %2
%55 = load i32, i32* @AF_UNIX
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %80
57: ; preds = %53
%58 = call i32 @memset(%struct.sockaddr_un* %7, i32 0, i32 20)
%59 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 2
%60 = load i32, i32* %59
%61 = getelementptr inbounds [160 x i8], [160 x i8]* %3, i64 0, i64 0
%62 = call i32 @strcpy(i32 %60, i8* %61)
%63 = load i32, i32* @AF_UNIX
%64 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %63, i32* %64
%65 = load i32, i32* @AF_UNIX
%66 = load i32, i32* @SOCK_STREAM
%67 = call i32 @socket(i32 %65, i32 %66, i32 0)
store i32 %67, i32* %4
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %57
store i32 -1, i32* %1
br label %89
70: ; preds = %57
%71 = load i32, i32* %4
%72 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%73 = call i64 @connect(i32 %71, %struct.sockaddr* %72, i32 20)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %70
%76 = load i32, i32* %4
%77 = call i32 @close(i32 %76)
store i32 -1, i32* %1
br label %89
78: ; preds = %70
%79 = load i32, i32* %4
store i32 %79, i32* %1
br label %89
80: ; preds = %53
%81 = load i32, i32* %2
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i32, i32* @stderr
%85 = call i32 @fprintf(i32 %84, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %89
86: ; preds = %80
br label %87
87: ; preds = %86
br label %88
88: ; preds = %87
store i32 -1, i32* %1
br label %89
89: ; preds = %88, %83, %78, %75, %69, %51, %48, %42, %17
%90 = load i32, i32* %1
ret i32 %90
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_un = type { i32, i32, i32, i32, i32 }
%struct.hostent = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@stderr = global i32 0
@.str = [64 x i8] c"WARNING: librtprof cannot parse environmentvariable RTPROF_SKT\0A\00"
define i32 @connectToRtprof() {
%1 = alloca [160 x i8]
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_un
%4 = getelementptr inbounds [160 x i8], [160 x i8]* %1, i64 0, i64 0
%5 = call i32 @parseSocketVariable(i8* %4, i32 160)
%6 = load i32, i32* @AF_INET
%7 = icmp eq i32 %5, %6
br i1 %7, label %8, label %39
8: ; preds = %0
%9 = bitcast %struct.sockaddr_in* %2 to i8*
%10 = call %struct.hostent* @gethostbyname(i8* %4)
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %37, label %12
12: ; preds = %8
%13 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr_un*
%14 = call i32 @memset(%struct.sockaddr_un* %13, i32 0, i32 20)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 4
%16 = bitcast i32* %15 to i8*
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 2
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @memcpy(i8* %16, i32 %18, i32 %20)
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %23, i32* %24
%25 = call i32 @htons(i32 4004)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %25, i32* %26
%27 = load i32, i32* %22
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = icmp slt i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %12
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i64 @connect(i32 %29, %struct.sockaddr* %32, i32 20)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @close(i32 %29)
br label %37
37: ; preds = %31, %12, %8, %35
%38 = phi i32 [ -1, %35 ], [ -1, %8 ], [ -1, %12 ], [ %29, %31 ]
br label %66
39: ; preds = %0
%40 = load i32, i32* @AF_UNIX
%41 = icmp eq i32 %5, %40
br i1 %41, label %42, label %61
42: ; preds = %39
%43 = bitcast %struct.sockaddr_un* %3 to i8*
%44 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 20)
%45 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 2
%46 = load i32, i32* %45
%47 = call i32 @strcpy(i32 %46, i8* %4)
%48 = load i32, i32* @AF_UNIX
%49 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %48, i32* %49
%50 = load i32, i32* @SOCK_STREAM
%51 = call i32 @socket(i32 %48, i32 %50, i32 0)
%52 = icmp slt i32 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %42
%54 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%55 = call i64 @connect(i32 %51, %struct.sockaddr* %54, i32 20)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @close(i32 %51)
br label %59
59: ; preds = %53, %42, %57
%60 = phi i32 [ -1, %57 ], [ -1, %42 ], [ %51, %53 ]
br label %66
61: ; preds = %39
%62 = icmp slt i32 %5, 0
br i1 %62, label %63, label %66
63: ; preds = %61
%64 = load i32, i32* @stderr
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0))
br label %66
66: ; preds = %61, %63, %59, %37
%67 = phi i32 [ %38, %37 ], [ %60, %59 ], [ -1, %63 ], [ -1, %61 ]
ret i32 %67
}