#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_NONE;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ inet_hostaddr(char *);
int /*<<< orphan*/ inet_service(char *, char const *);
char *strchr(char *, char);
char *strdup(char const *);
int inet_parseaddr(struct sockaddr_in *sa, const char *addr, const char *proto,
                   const char *def_service) {
  char *host = strdup(addr);
  char *serv = strchr(host, ':');
  int ok = 0;
  if (host) {
    sa->sin_family = AF_INET;
    if (serv)
      *serv++ = '\0';
    else
      serv = (char *)def_service;
    sa->sin_port = inet_service(serv, proto);
    sa->sin_addr.s_addr = inet_hostaddr(host);
    if (sa->sin_addr.s_addr != INADDR_NONE && sa->sin_port > 0)
      ok = 1;
  }
  free(host);
  return ok;
}


x86 assembly (clang, -O0):
<inet_parseaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1229 <inet_parseaddr+0xd9>
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
cmpq   $0x0,-0x30(%rbp)
je     11cb <inet_parseaddr+0x7b>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
jmp    11d3 <inet_parseaddr+0x83>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <inet_service@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <inet_hostaddr@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%eax
je     1224 <inet_parseaddr+0xd4>
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jle    1224 <inet_parseaddr+0xd4>
movl   $0x1,-0x34(%rbp)
jmp    1229 <inet_parseaddr+0xd9>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_parseaddr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
xor    %r12d,%r12d
test   %rbx,%rbx
je     11cd <inet_parseaddr+0x7d>
mov    0x2e60(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%r14)
test   %rax,%rax
je     119d <inet_parseaddr+0x4d>
mov    %rax,%rbp
add    $0x1,%rbp
movb   $0x0,(%rax)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <inet_service@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1040 <inet_hostaddr@plt>
mov    %eax,0x4(%r14)
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%eax
je     11cd <inet_parseaddr+0x7d>
xor    %r12d,%r12d
cmpl   $0x0,(%r14)
setg   %r12b
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i32 0
define i32 @inet_parseaddr(%struct.sockaddr_in* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca %struct.sockaddr_in*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%12 = load i8*, i8** %6
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = call i8* @strchr(i8* %14, i8 signext 58)
store i8* %15, i8** %10
store i32 0, i32* %11
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %53
18: ; preds = %4
%19 = load i32, i32* @AF_INET
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 2
store i32 %19, i32* %21
%22 = load i8*, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i8*, i8** %10
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %10
store i8 0, i8* %25
br label %29
27: ; preds = %18
%28 = load i8*, i8** %8
store i8* %28, i8** %10
br label %29
29: ; preds = %27, %24
%30 = load i8*, i8** %10
%31 = load i8*, i8** %7
%32 = call i32 @inet_service(i8* %30, i8* %31)
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i8*, i8** %9
%36 = call i32 @inet_hostaddr(i8* %35)
%37 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %37, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %36, i32* %39
%40 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 1
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = load i32, i32* @INADDR_NONE
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %52
46: ; preds = %29
%47 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %46
store i32 1, i32* %11
br label %52
52: ; preds = %51, %46, %29
br label %53
53: ; preds = %52, %4
%54 = load i8*, i8** %9
%55 = call i32 @free(i8* %54)
%56 = load i32, i32* %11
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i32 0
define i32 @inet_parseaddr(%struct.sockaddr_in* nocapture %0, i8* nocapture %1, i8* %2, i8* %3) {
%5 = tail call i8* @strdup(i8* %1)
%6 = tail call i8* @strchr(i8* dereferenceable(1) %5, i8 signext 58)
%7 = icmp eq i8* %5, null
br i1 %7, label %26, label %8
8: ; preds = %4
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2
store i32 %9, i32* %10
%11 = icmp eq i8* %6, null
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %6, i64 1
store i8 0, i8* %6
br label %14
14: ; preds = %8, %12
%15 = phi i8* [ %13, %12 ], [ %3, %8 ]
%16 = tail call i32 @inet_service(i8* %15, i8* %2)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @inet_hostaddr(i8* %5)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_NONE
%21 = icmp eq i32 %18, %20
br i1 %21, label %26, label %22
22: ; preds = %14
%23 = load i32, i32* %17
%24 = icmp sgt i32 %23, 0
%25 = zext i1 %24 to i32
br label %26
26: ; preds = %22, %14, %4
%27 = phi i32 [ 0, %14 ], [ 0, %4 ], [ %25, %22 ]
%28 = tail call i32 @free(i8* %5)
ret i32 %27
}