#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 */

/* Variables and functions */
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ cctools_debug(long long, char *, char const *);
int /*<<< orphan*/ getenv(char *);
int sscanf(char const *, char *, char *, int *);
char *strchr(char const *, char);
const char *parse_hostlist(const char *hosts, char *host, int *port) {
  const char *next = strchr(hosts, ',');
  switch (sscanf(hosts, "%[^:,]:%d", host, port)) {
  case 1:
    *port = (getenv("CATALOG_PORT") ? atoi(getenv("CATALOG_PORT")) : 9097);
    break;
  case 2:
    break;
  default:
    cctools_debug((1LL << 3), "bad host specification: %s", hosts);
    return NULL;
    break;
  }
  return next ? next + 1 : NULL;
}


x86 assembly (clang, -O0):
<parse_hostlist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2c,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe77(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0x1,%eax
je     11b2 <parse_hostlist+0x62>
jmp    11a1 <parse_hostlist+0x51>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     11fd <parse_hostlist+0xad>
jmp    1202 <parse_hostlist+0xb2>
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%eax
je     11e2 <parse_hostlist+0x92>
lea    0xe3c(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   1070 <atoi@plt>
mov    %eax,-0x30(%rbp)
jmp    11ef <parse_hostlist+0x9f>
mov    $0x2389,%eax
mov    %eax,-0x30(%rbp)
jmp    11ef <parse_hostlist+0x9f>
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1224 <parse_hostlist+0xd4>
jmp    1224 <parse_hostlist+0xd4>
mov    -0x10(%rbp),%rdx
mov    $0x8,%edi
lea    0xe05(%rip),%rsi
call   1050 <cctools_debug@plt>
movq   $0x0,-0x8(%rbp)
jmp    1253 <parse_hostlist+0x103>
cmpq   $0x0,-0x28(%rbp)
je     1240 <parse_hostlist+0xf0>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    124b <parse_hostlist+0xfb>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    124b <parse_hostlist+0xfb>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hostlist>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
mov    $0x2c,%esi
call   1040 <strchr@plt>
mov    %rax,%r15
lea    0xe8b(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
call   1060 <sscanf@plt>
cmp    $0x2,%eax
je     11c6 <parse_hostlist+0x76>
cmp    $0x1,%eax
jne    11a6 <parse_hostlist+0x56>
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
test   %eax,%eax
je     11be <parse_hostlist+0x6e>
mov    %eax,%edi
call   1070 <atoi@plt>
jmp    11c3 <parse_hostlist+0x73>
lea    0xe6a(%rip),%rsi
mov    $0x8,%edi
mov    %rbx,%rdx
call   1050 <cctools_debug@plt>
xor    %eax,%eax
jmp    11d1 <parse_hostlist+0x81>
mov    $0x2389,%eax
mov    %eax,(%r14)
lea    0x1(%r15),%rax
test   %r15,%r15
cmove  %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"%[^:,]:%d\00"
@.str.1 = [13 x i8] c"CATALOG_PORT\00"
@.str.2 = [27 x i8] c"bad host specification: %s\00"
define i8* @parse_hostlist(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = call i8* @strchr(i8* %9, i8 signext 44)
store i8* %10, i8** %8
%11 = load i8*, i8** %5
%12 = load i8*, i8** %6
%13 = load i32*, i32** %7
%14 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %12, i32* %13)
switch i32 %14, label %26 [
i32 1, label %15
i32 2, label %25
]
15: ; preds = %3
%16 = call i32 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @atoi(i32 %19)
br label %22
21: ; preds = %15
br label %22
22: ; preds = %21, %18
%23 = phi i32 [ %20, %18 ], [ 9097, %21 ]
%24 = load i32*, i32** %7
store i32 %23, i32* %24
br label %29
25: ; preds = %3
br label %29
26: ; preds = %3
%27 = load i8*, i8** %5
%28 = call i32 @cctools_debug(i64 8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i8* null, i8** %4
br label %38
29: ; preds = %25, %22
%30 = load i8*, i8** %8
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %8
%34 = getelementptr inbounds i8, i8* %33, i64 1
br label %36
35: ; preds = %29
br label %36
36: ; preds = %35, %32
%37 = phi i8* [ %34, %32 ], [ null, %35 ]
store i8* %37, i8** %4
br label %38
38: ; preds = %36, %26
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
@.str = [10 x i8] c"%[^:,]:%d\00"
@.str.1 = [13 x i8] c"CATALOG_PORT\00"
@.str.2 = [27 x i8] c"bad host specification: %s\00"
define i8* @parse_hostlist(i8* %0, i8* %1, i32* %2) {
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 44)
%5 = tail call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %1, i32* %2)
switch i32 %5, label %13 [
i32 1, label %6
i32 2, label %15
]
6: ; preds = %3
%7 = tail call i32 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @atoi(i32 %7)
br label %11
11: ; preds = %6, %9
%12 = phi i32 [ %10, %9 ], [ 9097, %6 ]
store i32 %12, i32* %2
br label %15
13: ; preds = %3
%14 = tail call i32 @cctools_debug(i64 8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %19
15: ; preds = %3, %11
%16 = icmp eq i8* %4, null
%17 = getelementptr inbounds i8, i8* %4, i64 1
%18 = select i1 %16, i8* null, i8* %17
br label %19
19: ; preds = %15, %13
%20 = phi i8* [ null, %13 ], [ %18, %15 ]
ret i8* %20
}