#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 {
  int dummy;
};

/* Variables and functions */
int AF_INET;
int AF_INET6;
int /*<<< orphan*/ EAFNOSUPPORT;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ inet_aton(char const *, struct in_addr *);
int scan_ip6(char const *, void *);
int inet_pton(int AF, const char *CP, void *BUF) {
  int len;
  if (AF == AF_INET) {
    if (!inet_aton(CP, (struct in_addr *)BUF))
      return 0;
  } else if (AF == AF_INET6) {
    if (CP[len = scan_ip6(CP, BUF)])
      if (CP[len] != '%')
        return 0;
  } else {
    errno = EAFNOSUPPORT;
    return -1;
  }
  return 1;
}


x86 assembly (clang, -O0):
<inet_pton>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2eab(%rip),%rcx
cmp    (%rcx),%eax
jne    116c <inet_pton+0x4c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <inet_aton@plt>
cmp    $0x0,%eax
jne    1167 <inet_pton+0x47>
movl   $0x0,-0x4(%rbp)
jmp    11fe <inet_pton+0xde>
jmp    11f7 <inet_pton+0xd7>
mov    -0x8(%rbp),%eax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%eax
jne    11d4 <inet_pton+0xb4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <scan_ip6@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,-0x1c(%rbp)
movslq %ecx,%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11cf <inet_pton+0xaf>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x25,%eax
je     11ca <inet_pton+0xaa>
movl   $0x0,-0x4(%rbp)
jmp    11fe <inet_pton+0xde>
jmp    11cf <inet_pton+0xaf>
jmp    11f2 <inet_pton+0xd2>
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
mov    0x2dec(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <inet_pton+0xde>
jmp    11f7 <inet_pton+0xd7>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_pton>:
push   %rbx
mov    %rsi,%rbx
mov    0x2ebd(%rip),%rax
cmp    %edi,(%rax)
jne    1145 <inet_pton+0x25>
mov    %rbx,%rdi
mov    %rdx,%rsi
call   1030 <inet_aton@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
setne  %al
pop    %rbx
ret
mov    0x2e94(%rip),%rax
cmp    %edi,(%rax)
jne    1171 <inet_pton+0x51>
mov    %rbx,%rdi
mov    %rdx,%rsi
call   1040 <scan_ip6@plt>
cltq
mov    (%rbx,%rax,1),%al
test   %al,%al
sete   %cl
cmp    $0x25,%al
sete   %al
or     %cl,%al
movzbl %al,%eax
pop    %rbx
ret
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    0x2e4f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
define i32 @inet_pton(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i32, i32* %5
%10 = load i32, i32* @AF_INET
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %20
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = load i8*, i8** %7
%15 = bitcast i8* %14 to %struct.in_addr*
%16 = call i32 @inet_aton(i8* %13, %struct.in_addr* %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %12
store i32 0, i32* %4
br label %48
19: ; preds = %12
br label %47
20: ; preds = %3
%21 = load i32, i32* %5
%22 = load i32, i32* @AF_INET6
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %44
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i8*, i8** %6
%27 = load i8*, i8** %7
%28 = call i32 @scan_ip6(i8* %26, i8* %27)
store i32 %28, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %25, i64 %29
%31 = load i8, i8* %30
%32 = icmp ne i8 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %24
%34 = load i8*, i8** %6
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 37
br i1 %40, label %41, label %42
41: ; preds = %33
store i32 0, i32* %4
br label %48
42: ; preds = %33
br label %43
43: ; preds = %42, %24
br label %46
44: ; preds = %20
%45 = load i32, i32* @EAFNOSUPPORT
store i32 %45, i32* @errno
store i32 -1, i32* %4
br label %48
46: ; preds = %43
br label %47
47: ; preds = %46, %19
store i32 1, i32* %4
br label %48
48: ; preds = %47, %44, %41, %18
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
define i32 @inet_pton(i32 %0, i8* %1, i8* %2) {
%4 = load i32, i32* @AF_INET
%5 = icmp eq i32 %4, %0
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = bitcast i8* %2 to %struct.in_addr*
%8 = tail call i32 @inet_aton(i8* %1, %struct.in_addr* %7)
%9 = icmp ne i32 %8, 0
%10 = zext i1 %9 to i32
br label %25
11: ; preds = %3
%12 = load i32, i32* @AF_INET6
%13 = icmp eq i32 %12, %0
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = tail call i32 @scan_ip6(i8* %1, i8* %2)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
%20 = icmp eq i8 %18, 37
%21 = or i1 %19, %20
%22 = zext i1 %21 to i32
br label %25
23: ; preds = %11
%24 = load i32, i32* @EAFNOSUPPORT
store i32 %24, i32* @errno
br label %25
25: ; preds = %6, %14, %23
%26 = phi i32 [ -1, %23 ], [ %22, %14 ], [ %10, %6 ]
ret i32 %26
}