#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct addrinfo {
  scalar_t__ ai_family;
  TYPE_1__ *ai_addr;
  int /*<<< orphan*/ ai_socktype;
};
struct TYPE_2__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
char *btcalloc(int, int);
int /*<<< orphan*/ btfree(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo const *,
                struct addrinfo **);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, int);
char *strchr(char *, char);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int parse(char *locator, struct sockaddr_in *psa) {
  int len;
  int port;
  char *host;
  char *hostname;
  char *sep;
  int err = 0;
  const struct addrinfo ai_template = {.ai_family = AF_INET,
                                       .ai_socktype = SOCK_DGRAM};
  struct addrinfo *ai = NULL;
  if (strncmp(locator, "udp://", 6) != 0) {
    return -1;
  }
  host = locator + 6;
  sep = strchr(host, ':');
  if (sep == NULL) {
    return -2;
  }
  len = sep - host;
  hostname = btcalloc(1, len + 1);
  strncpy(hostname, host, len);
  port = atoi(sep + 1);
  err = getaddrinfo(hostname, NULL, &ai_template, &ai);
  if (err == 0) {
    if (ai->ai_addr->sa_family == AF_INET) {
      *psa = *(struct sockaddr_in *)ai->ai_addr;
      psa->sin_port = htons(port);
    } else {
      printf("Tracker lookup returned unsupported address type %d. \n",
             ai->ai_addr->sa_family);
      err = -3;
    }
  } else {
    printf("getaddrinfo: error %d\n", err);
  }
  if (ai) {
    freeaddrinfo(ai);
  }
  btfree(hostname);
  return err;
}


x86 assembly (clang, -O0):
<parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe14(%rip),%rsi
mov    $0x6,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     120c <parse+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    133c <parse+0x19c>
mov    -0x10(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1241 <parse+0xa1>
movl   $0xfffffffe,-0x4(%rbp)
jmp    133c <parse+0x19c>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1040 <btcalloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    130a <parse+0x16a>
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x2d31(%rip),%rcx
cmp    (%rcx),%rax
jne    12e5 <parse+0x145>
mov    -0x18(%rbp),%rax
mov    -0x60(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1305 <parse+0x165>
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    %eax,%esi
lea    0xd0e(%rip),%rdi
call   1080 <printf@plt>
movl   $0xfffffffd,-0x3c(%rbp)
jmp    1319 <parse+0x179>
mov    -0x3c(%rbp),%esi
lea    0xd2a(%rip),%rdi
call   1080 <printf@plt>
cmpq   $0x0,-0x60(%rbp)
je     132d <parse+0x18d>
mov    -0x60(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <btfree@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e2d(%rip),%r13
mov    0x0(%r13),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe16(%rip),%rsi
mov    $0x6,%edx
call   1050 <strncmp@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    12cb <parse+0x12b>
add    $0x6,%rbx
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1279 <parse+0xd9>
mov    %rax,%rbp
mov    %ebp,%r12d
sub    %ebx,%r12d
lea    0x1(%r12),%esi
mov    $0x1,%edi
call   1040 <btcalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
add    $0x1,%rbp
mov    %rbp,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
xor    %esi,%esi
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1280 <parse+0xe0>
mov    %eax,%ebp
lea    0xdce(%rip),%rdi
mov    %eax,%esi
call   1080 <printf@plt>
jmp    12b4 <parse+0x114>
mov    $0xfffffffe,%ebp
jmp    12cb <parse+0x12b>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rsi
cmp    0x0(%r13),%rsi
jne    12a3 <parse+0x103>
mov    %esi,(%r15)
mov    %ebx,%edi
call   1060 <htons@plt>
mov    %eax,(%r15)
xor    %ebp,%ebp
jmp    12b4 <parse+0x114>
lea    0xd5d(%rip),%rdi
call   1080 <printf@plt>
mov    $0xfffffffd,%ebp
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     12c3 <parse+0x123>
call   10c0 <freeaddrinfo@plt>
mov    %r14,%rdi
call   10a0 <btfree@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i64, %struct.TYPE_2__*, i32 }
%struct.TYPE_2__ = type { i64 }
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"udp://\00"
@.str.1 = [55 x i8] c"Tracker lookup returned unsupported address type %d. \0A\00"
@.str.2 = [23 x i8] c"getaddrinfo: error %d\0A\00"
define i32 @parse(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.addrinfo
%13 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
store i32 0, i32* %11
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 0
%15 = load i64, i64* @AF_INET
store i64 %15, i64* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 1
store %struct.TYPE_2__* null, %struct.TYPE_2__** %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 2
%18 = load i32, i32* @SOCK_DGRAM
store i32 %18, i32* %17
store %struct.addrinfo* null, %struct.addrinfo** %13
%19 = load i8*, i8** %4
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %2
store i32 -1, i32* %3
br label %94
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i64 6
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = call i8* @strchr(i8* %26, i8 signext 58)
store i8* %27, i8** %10
%28 = load i8*, i8** %10
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %23
store i32 -2, i32* %3
br label %94
31: ; preds = %23
%32 = load i8*, i8** %10
%33 = load i8*, i8** %8
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
%40 = call i8* @btcalloc(i32 1, i32 %39)
store i8* %40, i8** %9
%41 = load i8*, i8** %9
%42 = load i8*, i8** %8
%43 = load i32, i32* %6
%44 = call i32 @strncpy(i8* %41, i8* %42, i32 %43)
%45 = load i8*, i8** %10
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = call i32 @atoi(i8* %46)
store i32 %47, i32* %7
%48 = load i8*, i8** %9
%49 = call i32 @getaddrinfo(i8* %48, i32* null, %struct.addrinfo* %12, %struct.addrinfo** %13)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %81
52: ; preds = %31
%53 = load %struct.addrinfo*, %struct.addrinfo** %13
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load %struct.TYPE_2__*, %struct.TYPE_2__** %54
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i64, i64* %56
%58 = load i64, i64* @AF_INET
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %72
60: ; preds = %52
%61 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%62 = load %struct.addrinfo*, %struct.addrinfo** %13
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 1
%64 = load %struct.TYPE_2__*, %struct.TYPE_2__** %63
%65 = bitcast %struct.TYPE_2__* %64 to %struct.sockaddr_in*
%66 = bitcast %struct.sockaddr_in* %61 to i8*
%67 = bitcast %struct.sockaddr_in* %65 to i8*
%68 = load i32, i32* %7
%69 = call i32 @htons(i32 %68)
%70 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %70, i32 0, i32 0
store i32 %69, i32* %71
br label %80
72: ; preds = %52
%73 = load %struct.addrinfo*, %struct.addrinfo** %13
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 1
%75 = load %struct.TYPE_2__*, %struct.TYPE_2__** %74
%76 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %75, i32 0, i32 0
%77 = load i64, i64* %76
%78 = trunc i64 %77 to i32
%79 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %78)
store i32 -3, i32* %11
br label %80
80: ; preds = %72, %60
br label %84
81: ; preds = %31
%82 = load i32, i32* %11
%83 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %82)
br label %84
84: ; preds = %81, %80
%85 = load %struct.addrinfo*, %struct.addrinfo** %13
%86 = icmp ne %struct.addrinfo* %85, null
br i1 %86, label %87, label %90
87: ; preds = %84
%88 = load %struct.addrinfo*, %struct.addrinfo** %13
%89 = call i32 @freeaddrinfo(%struct.addrinfo* %88)
br label %90
90: ; preds = %87, %84
%91 = load i8*, i8** %9
%92 = call i32 @btfree(i8* %91)
%93 = load i32, i32* %11
store i32 %93, i32* %3
br label %94
94: ; preds = %90, %30, %22
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i64, %struct.TYPE_2__*, i32 }
%struct.TYPE_2__ = type { i64 }
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"udp://\00"
@.str.1 = [55 x i8] c"Tracker lookup returned unsupported address type %d. \0A\00"
@.str.2 = [23 x i8] c"getaddrinfo: error %d\0A\00"
define i32 @parse(i8* %0, %struct.sockaddr_in* nocapture writeonly %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
%7 = load i64, i64* @AF_INET
store i64 %7, i64* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store %struct.TYPE_2__* null, %struct.TYPE_2__** %8
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
%10 = load i32, i32* @SOCK_DGRAM
store i32 %10, i32* %9
%11 = bitcast %struct.addrinfo** %4 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %4
%12 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %54
14: ; preds = %2
%15 = getelementptr inbounds i8, i8* %0, i64 6
%16 = tail call i8* @strchr(i8* %15, i8 signext 58)
%17 = icmp eq i8* %16, null
br i1 %17, label %54, label %18
18: ; preds = %14
%19 = ptrtoint i8* %16 to i64
%20 = ptrtoint i8* %15 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = add nsw i32 %22, 1
%24 = tail call i8* @btcalloc(i32 1, i32 %23)
%25 = tail call i32 @strncpy(i8* %24, i8* %15, i32 %22)
%26 = getelementptr inbounds i8, i8* %16, i64 1
%27 = tail call i32 @atoi(i8* %26)
%28 = call i32 @getaddrinfo(i8* %24, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %18
%31 = load %struct.addrinfo*, %struct.addrinfo** %4
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 1
%33 = load %struct.TYPE_2__*, %struct.TYPE_2__** %32
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i64 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* @AF_INET
%37 = icmp eq i64 %35, %36
%38 = trunc i64 %35 to i32
br i1 %37, label %39, label %42
39: ; preds = %30
%40 = getelementptr %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %38, i32* %40
%41 = call i32 @htons(i32 %27)
store i32 %41, i32* %40
br label %46
42: ; preds = %30
%43 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %38)
br label %46
44: ; preds = %18
%45 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %46
46: ; preds = %39, %42, %44
%47 = phi i32 [ 0, %39 ], [ -3, %42 ], [ %28, %44 ]
%48 = load %struct.addrinfo*, %struct.addrinfo** %4
%49 = icmp eq %struct.addrinfo* %48, null
br i1 %49, label %52, label %50
50: ; preds = %46
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
br label %52
52: ; preds = %50, %46
%53 = call i32 @btfree(i8* %24)
br label %54
54: ; preds = %14, %2, %52
%55 = phi i32 [ %47, %52 ], [ -1, %2 ], [ -2, %14 ]
ret i32 %55
}