#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 {
  scalar_t__ sin_family;
  void *sin_port;
  TYPE_1__ sin_addr;
};
struct hostent {
  scalar_t__ h_addrtype;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ INADDR_NONE;
struct hostent *gethostbyname(char *);
void *htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memcpy(TYPE_1__ *, int /*<<< orphan*/, int /*<<< orphan*/);
int scan_int(char *);
char *strtok_r(char *, char *, char **);
int scan_tcp_port(char *str, struct sockaddr_in *addr) {
  char *strtok_data;
  char *ip;
  char *port;
  int port_num;
  ip = strtok_r(str, ",", &strtok_data);
  port = strtok_r(NULL, "", &strtok_data);
  if (port == NULL) {
    port = ip;
    ip = NULL;
    addr->sin_addr.s_addr = INADDR_ANY;
    port_num = scan_int(port);
    if (port_num == -1) {
      return -1;
    }
    addr->sin_port = htons(port_num);
  } else {
    addr->sin_addr.s_addr = inet_addr(ip);
    if (addr->sin_addr.s_addr == INADDR_NONE) {
      struct hostent *hp;
      hp = gethostbyname(ip);
      if (hp == NULL) {
        return -1;
      }
      if (hp->h_addrtype != AF_INET) {
        return -1;
      }
      memcpy(&addr->sin_addr, hp->h_addr_list[0], hp->h_length);
    }
    port_num = scan_int(port);
    if (port_num == -1) {
      return -1;
    }
    addr->sin_port = htons(port_num);
  }
  addr->sin_family = AF_INET;
  return 0;
}


x86 assembly (clang, -O0):
<scan_tcp_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe6e(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1205 <scan_tcp_port+0xa5>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x30(%rbp),%rdi
call   1080 <scan_int@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11ed <scan_tcp_port+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <scan_tcp_port+0x17f>
mov    -0x34(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    12c7 <scan_tcp_port+0x167>
mov    -0x28(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%eax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%eax
jne    1292 <scan_tcp_port+0x132>
mov    -0x28(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1251 <scan_tcp_port+0xf1>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <scan_tcp_port+0x17f>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
je     1274 <scan_tcp_port+0x114>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <scan_tcp_port+0x17f>
mov    -0x18(%rbp),%rdi
add    $0x10,%rdi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1080 <scan_int@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12b4 <scan_tcp_port+0x154>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <scan_tcp_port+0x17f>
mov    -0x34(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    0x2d1a(%rip),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_tcp_port>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
lea    0xe8d(%rip),%rsi
lea    0x8(%rsp),%r15
mov    %r15,%rdx
call   1040 <strtok_r@plt>
mov    %rax,%rbx
lea    0xe77(%rip),%rsi
xor    %edi,%edi
mov    %r15,%rdx
call   1040 <strtok_r@plt>
test   %rax,%rax
je     11ee <scan_tcp_port+0x8e>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x10(%r14)
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jne    11e9 <scan_tcp_port+0x89>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1229 <scan_tcp_port+0xc9>
mov    (%rcx),%rdx
mov    0x2e16(%rip),%rsi
cmp    (%rsi),%rdx
jne    1229 <scan_tcp_port+0xc9>
lea    0x10(%r14),%rdi
mov    0x10(%rcx),%rax
mov    (%rax),%esi
mov    0x8(%rcx),%edx
call   1070 <memcpy@plt>
mov    %r15,%rdi
jmp    11fe <scan_tcp_port+0x9e>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%r14)
mov    %rbx,%rdi
call   1080 <scan_int@plt>
cmp    $0xffffffff,%eax
je     1224 <scan_tcp_port+0xc4>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%r14)
mov    0x2dce(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
xor    %eax,%eax
jmp    1229 <scan_tcp_port+0xc9>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64, i32, i32* }
@.str = [2 x i8] c",\00"
@.str.1 = [1 x i8] zeroinitializer
@INADDR_ANY = global i32 0
@INADDR_NONE = global i32 0
@AF_INET = global i64 0
define i32 @scan_tcp_port(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i8* @strtok_r(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%6 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @INADDR_ANY
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2, i32 0
store i32 %9, i32* %10
%11 = call i32 @scan_int(i8* %5)
%12 = icmp eq i32 %11, -1
br i1 %12, label %43, label %37
13: ; preds = %2
%14 = call i32 @inet_addr(i8* %5)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i64 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* @INADDR_NONE
%18 = icmp eq i32 %14, %17
br i1 %18, label %19, label %34
19: ; preds = %13
%20 = call %struct.hostent* @gethostbyname(i8* %5)
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %43, label %22
22: ; preds = %19
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @AF_INET
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %43
27: ; preds = %22
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 2
%29 = load i32*, i32** %28
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @memcpy(%struct.TYPE_2__* %15, i32 %30, i32 %32)
br label %34
34: ; preds = %27, %13
%35 = call i32 @scan_int(i8* %6)
%36 = icmp eq i32 %35, -1
br i1 %36, label %43, label %37
37: ; preds = %34, %8
%38 = phi i32 [ %11, %8 ], [ %35, %34 ]
%39 = call i8* @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i8* %39, i8** %40
%41 = load i64, i64* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i64 %41, i64* %42
br label %43
43: ; preds = %34, %19, %22, %8, %37
%44 = phi i32 [ 0, %37 ], [ -1, %8 ], [ -1, %22 ], [ -1, %19 ], [ -1, %34 ]
ret i32 %44
}