#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 sockaddr_un {
  char *sun_path;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(unsigned int);
int sscanf(char const *, char *, char *, unsigned int *, unsigned int *);
char *strrchr(char *, char);
int convert_un_in(const struct sockaddr_un *un, struct sockaddr_in *in,
                  socklen_t *len) {
  unsigned int iface;
  unsigned int prt;
  const char *p;
  char type;
  if ((*len) < sizeof(struct sockaddr_in)) {
    return 0;
  }
  p = strrchr(un->sun_path, '/');
  if (p)
    p++;
  else
    p = un->sun_path;
  if (sscanf(p, "%c%02X%04X", &type, &iface, &prt) != 3) {
    errno = EINVAL;
    return -1;
  }
  if (type != 'T' && type != 'U') {
    errno = EINVAL;
    return -1;
  }
  if (iface == 0 || iface > 16) {
    errno = EINVAL;
    return -1;
  }
  if (prt > 0xFFFF) {
    errno = EINVAL;
    return -1;
  }
  in->sin_family = AF_INET;
  in->sin_addr.s_addr = htonl((127 << 24) | iface);
  in->sin_port = htons(prt);
  *len = sizeof(struct sockaddr_in);
  return 0;
}


x86 assembly (clang, -O0):
<convert_un_in>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
cmp    $0xc,%rax
jae    1171 <convert_un_in+0x31>
movl   $0x0,-0x4(%rbp)
jmp    12cd <convert_un_in+0x18d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11a2 <convert_un_in+0x62>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11ad <convert_un_in+0x6d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe48(%rip),%rsi
lea    -0x31(%rbp),%rdx
lea    -0x24(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1060 <sscanf@plt>
cmp    $0x3,%eax
je     11f0 <convert_un_in+0xb0>
mov    0x2df7(%rip),%rax
mov    (%rax),%ecx
mov    0x2df6(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <convert_un_in+0x18d>
movsbl -0x31(%rbp),%eax
cmp    $0x54,%eax
je     1228 <convert_un_in+0xe8>
movsbl -0x31(%rbp),%eax
cmp    $0x55,%eax
je     1228 <convert_un_in+0xe8>
mov    0x2dbf(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbe(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <convert_un_in+0x18d>
cmpl   $0x0,-0x24(%rbp)
je     123c <convert_un_in+0xfc>
cmpl   $0x10,-0x24(%rbp)
jbe    125a <convert_un_in+0x11a>
mov    0x2d8d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d8c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <convert_un_in+0x18d>
cmpl   $0xffff,-0x28(%rbp)
jbe    1285 <convert_un_in+0x145>
mov    0x2d62(%rip),%rax
mov    (%rax),%ecx
mov    0x2d61(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <convert_un_in+0x18d>
mov    0x2d5c(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x24(%rbp),%edi
or     $0x7f000000,%edi
call   1050 <htonl@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x28(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movl   $0xc,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_un_in>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %ebx,%ebx
cmpl   $0xc,(%rdx)
jb     11cb <convert_un_in+0x8b>
mov    %rdx,%r14
mov    %rsi,%r15
mov    (%rdi),%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
cmove  %rbp,%rdi
lea    0xe87(%rip),%rsi
lea    0xf(%rsp),%rdx
lea    0x14(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1060 <sscanf@plt>
cmp    $0x3,%eax
jne    11b4 <convert_un_in+0x74>
mov    0xf(%rsp),%al
and    $0xfe,%al
cmp    $0x54,%al
jne    11b4 <convert_un_in+0x74>
mov    0x14(%rsp),%edi
lea    -0x11(%rdi),%eax
cmp    $0xffffffef,%eax
jbe    11b4 <convert_un_in+0x74>
mov    0x10(%rsp),%ebp
cmp    $0x10000,%ebp
jb     11d8 <convert_un_in+0x98>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    0x2e14(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r15)
or     $0x7f000000,%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%r15)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,(%r15)
movl   $0xc,(%r14)
jmp    11cb <convert_un_in+0x8b>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8* }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@.str = [11 x i8] c"%c%02X%04X\00"
@EINVAL = global i32 0
@errno = global i32 0
@AF_INET = global i32 0
define i32 @convert_un_in(%struct.sockaddr_un* %0, %struct.sockaddr_in* %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_un*
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8
store %struct.sockaddr_un* %0, %struct.sockaddr_un** %5
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %6
store i32* %2, i32** %7
%12 = load i32*, i32** %7
%13 = load i32, i32* %12
%14 = sext i32 %13 to i64
%15 = icmp ult i64 %14, 12
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 0, i32* %4
br label %75
17: ; preds = %3
%18 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i8* @strrchr(i8* %20, i8 signext 47)
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i8*, i8** %10
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %10
br label %31
27: ; preds = %17
%28 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %28, i32 0, i32 0
%30 = load i8*, i8** %29
store i8* %30, i8** %10
br label %31
31: ; preds = %27, %24
%32 = load i8*, i8** %10
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %11, i32* %8, i32* %9)
%34 = icmp ne i32 %33, 3
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i32, i32* @EINVAL
store i32 %36, i32* @errno
store i32 -1, i32* %4
br label %75
37: ; preds = %31
%38 = load i8, i8* %11
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 84
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i8, i8* %11
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 85
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = load i32, i32* @EINVAL
store i32 %46, i32* @errno
store i32 -1, i32* %4
br label %75
47: ; preds = %41, %37
%48 = load i32, i32* %8
%49 = icmp eq i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %47
%51 = load i32, i32* %8
%52 = icmp ugt i32 %51, 16
br i1 %52, label %53, label %55
53: ; preds = %50, %47
%54 = load i32, i32* @EINVAL
store i32 %54, i32* @errno
store i32 -1, i32* %4
br label %75
55: ; preds = %50
%56 = load i32, i32* %9
%57 = icmp ugt i32 %56, 65535
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = load i32, i32* @EINVAL
store i32 %59, i32* @errno
store i32 -1, i32* %4
br label %75
60: ; preds = %55
%61 = load i32, i32* @AF_INET
%62 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %62, i32 0, i32 2
store i32 %61, i32* %63
%64 = load i32, i32* %8
%65 = or i32 2130706432, %64
%66 = call i32 @htonl(i32 %65)
%67 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %67, i32 0, i32 1
%69 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %68, i32 0, i32 0
store i32 %66, i32* %69
%70 = load i32, i32* %9
%71 = call i32 @htons(i32 %70)
%72 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%73 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %72, i32 0, i32 0
store i32 %71, i32* %73
%74 = load i32*, i32** %7
store i32 12, i32* %74
store i32 0, i32* %4
br label %75
75: ; preds = %60, %58, %53, %45, %35, %16
%76 = load i32, i32* %4
ret i32 %76
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8* }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@.str = [11 x i8] c"%c%02X%04X\00"
@EINVAL = global i32 0
@errno = global i32 0
@AF_INET = global i32 0
define i32 @convert_un_in(%struct.sockaddr_un* nocapture %0, %struct.sockaddr_in* nocapture writeonly %1, i32* nocapture %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i32, i32* %2
%10 = icmp ult i32 %9, 12
br i1 %10, label %47, label %11
11: ; preds = %3
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = tail call i8* @strrchr(i8* dereferenceable(1) %13, i8 signext 47)
%15 = icmp eq i8* %14, null
%16 = getelementptr inbounds i8, i8* %14, i64 1
%17 = select i1 %15, i8* %13, i8* %16
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %6, i32* %4, i32* %5)
%19 = icmp eq i32 %18, 3
br i1 %19, label %22, label %20
20: ; preds = %11
%21 = load i32, i32* @EINVAL
store i32 %21, i32* @errno
br label %47
22: ; preds = %11
%23 = load i8, i8* %6
%24 = and i8 %23, -2
%25 = icmp eq i8 %24, 84
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = load i32, i32* @EINVAL
store i32 %27, i32* @errno
br label %47
28: ; preds = %22
%29 = load i32, i32* %4
%30 = add i32 %29, -17
%31 = icmp ult i32 %30, -16
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = load i32, i32* @EINVAL
store i32 %33, i32* @errno
br label %47
34: ; preds = %28
%35 = load i32, i32* %5
%36 = icmp ugt i32 %35, 65535
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = load i32, i32* @EINVAL
store i32 %38, i32* @errno
br label %47
39: ; preds = %34
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %40, i32* %41
%42 = or i32 %29, 2130706432
%43 = call i32 @htonl(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %43, i32* %44
%45 = call i32 @htons(i32 %35)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %45, i32* %46
store i32 12, i32* %2
br label %47
47: ; preds = %3, %39, %37, %32, %26, %20
%48 = phi i32 [ -1, %20 ], [ -1, %26 ], [ -1, %32 ], [ -1, %37 ], [ 0, %39 ], [ 0, %3 ]
ret i32 %48
}