#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_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int ENETUNREACH;
int /*<<< orphan*/ SOCK_STREAM;
int aprsis_login(int, char const *, double, double, double);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int aprsis_connect(const char *hostname, int port, const char *mycall,
                   double lat, double lon, double range) {
  int sock;
  struct sockaddr_in sa;
  struct hostent *he;
  int ret;
  he = gethostbyname(hostname);
  if (!he || (he->h_length < 1))
    return -ENETUNREACH;
  sa.sin_family = AF_INET;
  memcpy(&sa.sin_addr, he->h_addr_list[0], sizeof(sa.sin_addr));
  sa.sin_port = htons(port);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    return sock;
  ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa));
  if (ret < 0)
    goto out;
  ret = aprsis_login(sock, mycall, lat, lon, range);
  if (ret < 0)
    goto out;
  printf("Connected\n");
out:
  if (ret) {
    close(sock);
    return ret;
  }
  return sock;
}


x86 assembly (clang, -O0):
<aprsis_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movsd  %xmm0,-0x28(%rbp)
movsd  %xmm1,-0x30(%rbp)
movsd  %xmm2,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     11c7 <aprsis_connect+0x47>
mov    -0x50(%rbp),%rax
cmpl   $0x1,(%rax)
jge    11da <aprsis_connect+0x5a>
xor    %eax,%eax
mov    0x2e10(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12bd <aprsis_connect+0x13d>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    -0x48(%rbp),%rdi
add    $0x4,%rdi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    123e <aprsis_connect+0xbe>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12bd <aprsis_connect+0x13d>
mov    -0x3c(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1261 <aprsis_connect+0xe1>
jmp    129a <aprsis_connect+0x11a>
mov    -0x3c(%rbp),%edi
mov    -0x20(%rbp),%rsi
movsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm2
call   1070 <aprsis_login@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    128e <aprsis_connect+0x10e>
jmp    129a <aprsis_connect+0x11a>
lea    0xd6b(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x54(%rbp)
je     12b7 <aprsis_connect+0x137>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12bd <aprsis_connect+0x13d>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aprsis_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
movsd  %xmm2,0x28(%rsp)
movsd  %xmm1,0x20(%rsp)
movsd  %xmm0,0x18(%rsp)
mov    %rdx,%r14
mov    %esi,%ebp
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1244 <aprsis_connect+0xc4>
cmpl   $0x0,(%rax)
jle    1244 <aprsis_connect+0xc4>
mov    0x2e2b(%rip),%rbx
mov    (%rbx),%ecx
mov    %ecx,0x10(%rsp)
lea    0xc(%rsp),%rdi
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     125c <aprsis_connect+0xdc>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %eax,%eax
js     1251 <aprsis_connect+0xd1>
mov    %ebp,%edi
mov    %r14,%rsi
movsd  0x18(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
movsd  0x28(%rsp),%xmm2
call   1070 <aprsis_login@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1253 <aprsis_connect+0xd3>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
jne    1253 <aprsis_connect+0xd3>
jmp    125c <aprsis_connect+0xdc>
mov    0x2d95(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    125c <aprsis_connect+0xdc>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@ENETUNREACH = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"Connected\0A\00"
define i32 @aprsis_connect(i8* %0, i32 %1, i8* %2, double %3, double %4, double %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca i32
%15 = alloca %struct.sockaddr_in
%16 = alloca %struct.hostent*
%17 = alloca i32
store i8* %0, i8** %8
store i32 %1, i32* %9
store i8* %2, i8** %10
store double %3, double* %11
store double %4, double* %12
store double %5, double* %13
%18 = load i8*, i8** %8
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %16
%20 = load %struct.hostent*, %struct.hostent** %16
%21 = icmp ne %struct.hostent* %20, null
br i1 %21, label %22, label %27
22: ; preds = %6
%23 = load %struct.hostent*, %struct.hostent** %16
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %22, %6
%28 = load i32, i32* @ENETUNREACH
%29 = sub nsw i32 0, %28
store i32 %29, i32* %7
br label %78
30: ; preds = %22
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%34 = load %struct.hostent*, %struct.hostent** %16
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i32 0, i32 1
%36 = load i32*, i32** %35
%37 = getelementptr inbounds i32, i32* %36, i64 0
%38 = load i32, i32* %37
%39 = call i32 @memcpy(i32* %33, i32 %38, i32 4)
%40 = load i32, i32* %9
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i32 %41, i32* %42
%43 = load i32, i32* @AF_INET
%44 = load i32, i32* @SOCK_STREAM
%45 = call i32 @socket(i32 %43, i32 %44, i32 0)
store i32 %45, i32* %14
%46 = load i32, i32* %14
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %30
%49 = load i32, i32* %14
store i32 %49, i32* %7
br label %78
50: ; preds = %30
%51 = load i32, i32* %14
%52 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr*
%53 = call i32 @connect(i32 %51, %struct.sockaddr* %52, i32 12)
store i32 %53, i32* %17
%54 = load i32, i32* %17
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %50
br label %69
57: ; preds = %50
%58 = load i32, i32* %14
%59 = load i8*, i8** %10
%60 = load double, double* %11
%61 = load double, double* %12
%62 = load double, double* %13
%63 = call i32 @aprsis_login(i32 %58, i8* %59, double %60, double %61, double %62)
store i32 %63, i32* %17
%64 = load i32, i32* %17
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %57
br label %69
67: ; preds = %57
%68 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %69
69: ; preds = %67, %66, %56
%70 = load i32, i32* %17
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %69
%73 = load i32, i32* %14
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %17
store i32 %75, i32* %7
br label %78
76: ; preds = %69
%77 = load i32, i32* %14
store i32 %77, i32* %7
br label %78
78: ; preds = %76, %72, %48, %27
%79 = load i32, i32* %7
ret i32 %79
}

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