#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 in_addr {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  scalar_t__ sin_port;
  struct in_addr sin_addr;
};
struct servent {
  scalar_t__ s_port;
};
struct hostent {
  scalar_t__ h_addrtype;
  int h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char const *);
struct servent *getservbyname(char const *, char const *);
scalar_t__ htons(int /*<<< orphan*/);
char *strtok(char *, char *);
int /*<<< orphan*/ strtoul(char const *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *);
struct sockaddr_in parse_address(char *str, const char *proto) {
  struct sockaddr_in sin;
  struct servent *service;
  const char *first = strtok(str, ":");
  const char *second = strtok(NULL, "");
  char *end;
  sin.sin_family = AF_INET;
  if (NULL == second) {
    second = first;
    sin.sin_addr.s_addr = 0;
  } else {
    struct hostent *const host = gethostbyname(first);
    if (NULL == host) {
      syslog(LOG_ERR, "%s: unknown host", first);
      exit(2);
    }
    if (sin.sin_family != host->h_addrtype ||
        sizeof(sin.sin_addr) != host->h_length) {
      syslog(LOG_ERR, "%s: wrong address type", first);
      exit(2);
    }
    sin.sin_addr = *(struct in_addr *)host->h_addr;
  }
  service = getservbyname(second, proto);
  if (NULL != service)
    sin.sin_port = service->s_port;
  else {
    sin.sin_port = htons(strtoul(second, &end, 0));
    if (*second == '\0' || *end != '\0' || 0 == sin.sin_port) {
      syslog(LOG_ERR, "%s: unknown service and invalid port number", second);
      exit(3);
    }
  }
  return sin;
}


x86 assembly (clang, -O0):
<parse_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x40(%rbp)
mov    %rdi,-0x48(%rbp)
mov    %rsi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe5a(%rip),%rsi
call   1070 <strtok@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,-0x28(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rdi)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11e6 <parse_address+0x76>
mov    -0x40(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,-0x28(%rbp)
movq   $0x0,0x10(%rax)
jmp    1282 <parse_address+0x112>
mov    -0x20(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jne    1222 <parse_address+0xb2>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdef(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    -0x38(%rbp),%rcx
cmp    (%rcx),%rax
jne    124c <parse_address+0xdc>
mov    -0x38(%rbp),%rax
movslq 0x8(%rax),%rcx
mov    $0x8,%eax
cmp    %rcx,%rax
je     126f <parse_address+0xff>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdb3(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    0x10(%rcx),%rcx
mov    (%rcx),%rcx
mov    %rcx,0x10(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     12b3 <parse_address+0x143>
mov    -0x40(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,0x8(%rax)
jmp    132c <parse_address+0x1bc>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1304 <parse_address+0x194>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1304 <parse_address+0x194>
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
cmp    0x8(%rcx),%rax
jne    1327 <parse_address+0x1b7>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd12(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
jmp    132c <parse_address+0x1bc>
mov    -0x48(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_address>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%rax
mov    %rdi,%r12
lea    0xe73(%rip),%rsi
mov    %rax,%rdi
call   1070 <strtok@plt>
mov    %rax,%r14
lea    0xe62(%rip),%rsi
xor    %edi,%edi
call   1070 <strtok@plt>
mov    %rax,%r13
mov    0x2e38(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,(%r12)
test   %r13,%r13
je     11e9 <parse_address+0x79>
mov    %r14,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1281 <parse_address+0x111>
cmp    (%rax),%rbx
jne    124d <parse_address+0xdd>
cmpl   $0x8,0x8(%rax)
jne    124d <parse_address+0xdd>
mov    0x10(%rax),%rax
mov    (%rax),%rax
mov    %rax,0x10(%r12)
mov    %r13,%r14
jmp    11f2 <parse_address+0x82>
movq   $0x0,0x10(%r12)
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <getservbyname@plt>
test   %rax,%rax
je     120c <parse_address+0x9c>
mov    (%rax),%rax
mov    %rax,0x8(%r12)
jmp    123c <parse_address+0xcc>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%r12)
cmpb   $0x0,(%r14)
je     125f <parse_address+0xef>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    125f <parse_address+0xef>
test   %rax,%rax
je     125f <parse_address+0xef>
mov    %r12,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
jmp    1291 <parse_address+0x121>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
mov    %r14,%rdx
call   1060 <syslog@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
mov    %r14,%rdx
call   1060 <syslog@plt>
mov    $0x2,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i64, %struct.in_addr }
%struct.in_addr = type { i64 }
%struct.servent = type { i64 }
%struct.hostent = type { i64, i32, i64 }
@.str = [2 x i8] c":\00"
@.str.1 = [1 x i8] zeroinitializer
@AF_INET = global i64 0
@LOG_ERR = global i32 0
@.str.2 = [17 x i8] c"%s: unknown host\00"
@.str.3 = [23 x i8] c"%s: wrong address type\00"
@.str.4 = [44 x i8] c"%s: unknown service and invalid port number\00"
define void @parse_address(%struct.sockaddr_in* noalias sret(%struct.sockaddr_in) align 8 %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.servent*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.hostent*
store i8* %1, i8** %4
store i8* %2, i8** %5
%11 = load i8*, i8** %4
%12 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %7
%13 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %13, i8** %8
%14 = load i64, i64* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i32 0, i32 0
store i64 %14, i64* %15
%16 = load i8*, i8** %8
%17 = icmp eq i8* null, %16
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i8*, i8** %7
store i8* %19, i8** %8
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i32 0, i32 2
%21 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %20, i32 0, i32 0
store i64 0, i64* %21
br label %58
22: ; preds = %3
%23 = load i8*, i8** %7
%24 = call %struct.hostent* @gethostbyname(i8* %23)
store %struct.hostent* %24, %struct.hostent** %10
%25 = load %struct.hostent*, %struct.hostent** %10
%26 = icmp eq %struct.hostent* null, %25
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* @LOG_ERR
%29 = load i8*, i8** %7
%30 = call i32 @syslog(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 2)
unreachable
32: ; preds = %22
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load %struct.hostent*, %struct.hostent** %10
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = icmp ne i64 %34, %37
br i1 %38, label %45, label %39
39: ; preds = %32
%40 = load %struct.hostent*, %struct.hostent** %10
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = sext i32 %42 to i64
%44 = icmp ne i64 8, %43
br i1 %44, label %45, label %50
45: ; preds = %39, %32
%46 = load i32, i32* @LOG_ERR
%47 = load i8*, i8** %7
%48 = call i32 @syslog(i32 %46, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = call i32 @exit(i32 2)
unreachable
50: ; preds = %39
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i32 0, i32 2
%52 = load %struct.hostent*, %struct.hostent** %10
%53 = getelementptr inbounds %struct.hostent, %struct.hostent* %52, i32 0, i32 2
%54 = load i64, i64* %53
%55 = inttoptr i64 %54 to %struct.in_addr*
%56 = bitcast %struct.in_addr* %51 to i8*
%57 = bitcast %struct.in_addr* %55 to i8*
br label %58
58: ; preds = %50, %18
%59 = load i8*, i8** %8
%60 = load i8*, i8** %5
%61 = call %struct.servent* @getservbyname(i8* %59, i8* %60)
store %struct.servent* %61, %struct.servent** %6
%62 = load %struct.servent*, %struct.servent** %6
%63 = icmp ne %struct.servent* null, %62
br i1 %63, label %64, label %69
64: ; preds = %58
%65 = load %struct.servent*, %struct.servent** %6
%66 = getelementptr inbounds %struct.servent, %struct.servent* %65, i32 0, i32 0
%67 = load i64, i64* %66
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i32 0, i32 1
store i64 %67, i64* %68
br label %93
69: ; preds = %58
%70 = load i8*, i8** %8
%71 = call i32 @strtoul(i8* %70, i8** %9, i32 0)
%72 = call i64 @htons(i32 %71)
%73 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i32 0, i32 1
store i64 %72, i64* %73
%74 = load i8*, i8** %8
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 0
br i1 %77, label %87, label %78
78: ; preds = %69
%79 = load i8*, i8** %9
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = icmp ne i32 %81, 0
br i1 %82, label %87, label %83
83: ; preds = %78
%84 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i32 0, i32 1
%85 = load i64, i64* %84
%86 = icmp eq i64 0, %85
br i1 %86, label %87, label %92
87: ; preds = %83, %78, %69
%88 = load i32, i32* @LOG_ERR
%89 = load i8*, i8** %8
%90 = call i32 @syslog(i32 %88, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %89)
%91 = call i32 @exit(i32 3)
unreachable
92: ; preds = %83
br label %93
93: ; preds = %92, %64
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i64, %struct.in_addr }
%struct.in_addr = type { i64 }
%struct.hostent = type { i64, i32, i64 }
%struct.servent = type { i64 }
@.str = [2 x i8] c":\00"
@.str.1 = [1 x i8] zeroinitializer
@AF_INET = global i64 0
@LOG_ERR = global i32 0
@.str.2 = [17 x i8] c"%s: unknown host\00"
@.str.3 = [23 x i8] c"%s: wrong address type\00"
@.str.4 = [44 x i8] c"%s: unknown service and invalid port number\00"
define void @parse_address(%struct.sockaddr_in* noalias nocapture sret(%struct.sockaddr_in) align 8 %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = tail call i8* @strtok(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%7 = bitcast i8** %4 to i8*
%8 = load i64, i64* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
store i64 %8, i64* %9
%10 = icmp eq i8* %6, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2, i32 0
store i64 0, i64* %12
br label %38
13: ; preds = %3
%14 = tail call %struct.hostent* @gethostbyname(i8* %5)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @LOG_ERR
%18 = tail call i32 @syslog(i32 %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%19 = tail call i32 @exit(i32 2)
unreachable
20: ; preds = %13
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%22 = load i64, i64* %21
%23 = icmp eq i64 %8, %22
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 8
br i1 %27, label %32, label %28
28: ; preds = %24, %20
%29 = load i32, i32* @LOG_ERR
%30 = tail call i32 @syslog(i32 %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%31 = tail call i32 @exit(i32 2)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 2
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to i64*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2, i32 0
%37 = load i64, i64* %35
store i64 %37, i64* %36
br label %38
38: ; preds = %32, %11
%39 = phi i8* [ %5, %11 ], [ %6, %32 ]
%40 = tail call %struct.servent* @getservbyname(i8* %39, i8* %2)
%41 = icmp eq %struct.servent* %40, null
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = getelementptr inbounds %struct.servent, %struct.servent* %40, i64 0, i32 0
%44 = load i64, i64* %43
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1
store i64 %44, i64* %45
br label %62
46: ; preds = %38
%47 = call i32 @strtoul(i8* %39, i8** %4, i32 0)
%48 = tail call i64 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1
store i64 %48, i64* %49
%50 = load i8, i8* %39
%51 = icmp eq i8 %50, 0
br i1 %51, label %58, label %52
52: ; preds = %46
%53 = load i8*, i8** %4
%54 = load i8, i8* %53
%55 = icmp ne i8 %54, 0
%56 = icmp eq i64 %48, 0
%57 = select i1 %55, i1 true, i1 %56
br i1 %57, label %58, label %62
58: ; preds = %52, %46
%59 = load i32, i32* @LOG_ERR
%60 = tail call i32 @syslog(i32 %59, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%61 = tail call i32 @exit(i32 3)
unreachable
62: ; preds = %52, %42
ret void
}