#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
typedef int /*<<< orphan*/ straddrbuf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char const *, TYPE_1__ *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ strcmp(char const *, char *);
char *strrchr(char const *, char);
int resolve_ip4_literal_addr(const char *addr, struct sockaddr_in *addr_in) {
  memset(addr_in, 0, sizeof(struct sockaddr_in));
  addr_in->sin_family = AF_INET;
  char *colon = strrchr(addr, ':');
  int port = 0;
  if (colon) {
    const char *pos = colon + 1;
    while (*pos) {
      if (*pos < '0' || *pos > '9') {
        errno = EINVAL;
        return -1;
      }
      port *= 10;
      port += *pos - '0';
      ++pos;
    }
    if (port < 0 || port > 0xffff) {
      errno = EINVAL;
      return -1;
    }
  }
  addr_in->sin_port = htons(port);
  const char *straddr;
  char straddrbuf[256];
  if (!colon)
    straddr = addr;
  else {
    assert(colon - addr + 1 <= sizeof(straddrbuf));
    memcpy(straddrbuf, addr, colon - addr);
    straddrbuf[colon - addr] = 0;
    straddr = straddrbuf;
  }
  if (strcmp(straddr, "*") == 0) {
    addr_in->sin_addr.s_addr = INADDR_ANY;
    return 0;
  }
  int rc = inet_pton(AF_INET, straddr, &addr_in->sin_addr);
  if (rc != 1) {
    errno = EINVAL;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<resolve_ip4_literal_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1280 <resolve_ip4_literal_addr+0x110>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     1246 <resolve_ip4_literal_addr+0xd6>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     1200 <resolve_ip4_literal_addr+0x90>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    121e <resolve_ip4_literal_addr+0xae>
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1389 <resolve_ip4_literal_addr+0x219>
imul   $0xa,-0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11d3 <resolve_ip4_literal_addr+0x63>
cmpl   $0x0,-0x24(%rbp)
jl     125d <resolve_ip4_literal_addr+0xed>
cmpl   $0xffff,-0x24(%rbp)
jle    127b <resolve_ip4_literal_addr+0x10b>
mov    0x2d64(%rip),%rax
mov    (%rax),%ecx
mov    0x2d63(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1389 <resolve_ip4_literal_addr+0x219>
jmp    1280 <resolve_ip4_literal_addr+0x110>
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpq   $0x0,-0x20(%rbp)
jne    12a9 <resolve_ip4_literal_addr+0x139>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1306 <resolve_ip4_literal_addr+0x196>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
cmp    $0x100,%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xcef(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    133b <resolve_ip4_literal_addr+0x1cb>
mov    0x2cb9(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1389 <resolve_ip4_literal_addr+0x219>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1090 <inet_pton@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x1,-0x144(%rbp)
je     1382 <resolve_ip4_literal_addr+0x212>
mov    0x2c5d(%rip),%rax
mov    (%rax),%ecx
mov    0x2c5c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1389 <resolve_ip4_literal_addr+0x219>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_ip4_literal_addr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebx,%ebx
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e4a(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x8(%r15)
mov    %r14,%rdi
mov    $0x3a,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     123a <resolve_ip4_literal_addr+0xca>
mov    %rax,%r12
mov    0x1(%rax),%cl
test   %cl,%cl
je     11fb <resolve_ip4_literal_addr+0x8b>
lea    0x2(%r12),%rax
xor    %ebx,%ebx
nopl   (%rax)
lea    -0x3a(%rcx),%edx
cmp    $0xf5,%dl
jbe    1271 <resolve_ip4_literal_addr+0x101>
movzbl %cl,%ecx
lea    (%rbx,%rbx,4),%edx
lea    (%rcx,%rdx,2),%ebx
add    $0xffffffd0,%ebx
movzbl (%rax),%ecx
add    $0x1,%rax
test   %cl,%cl
jne    11d0 <resolve_ip4_literal_addr+0x60>
cmp    $0x10000,%ebx
jae    1271 <resolve_ip4_literal_addr+0x101>
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%r15)
mov    %rsp,%rbx
sub    %r14,%r12
lea    0x1(%r12),%rax
xor    %edi,%edi
cmp    $0x101,%rax
setb   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1080 <memcpy@plt>
movb   $0x0,(%rsp,%r12,1)
mov    %rbx,%r14
jmp    1245 <resolve_ip4_literal_addr+0xd5>
xor    %edi,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%r15)
lea    0xdb4(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     128a <resolve_ip4_literal_addr+0x11a>
mov    0x0(%r13),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <inet_pton@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
je     1298 <resolve_ip4_literal_addr+0x128>
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    0x2d4f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1298 <resolve_ip4_literal_addr+0x128>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r15)
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"*\00"
@INADDR_ANY = global i32 0
define i32 @resolve_ip4_literal_addr(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [256 x i8]
%11 = alloca i32
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%13 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
store i32 %14, i32* %16
%17 = load i8*, i8** %4
%18 = call i8* @strrchr(i8* %17, i8 signext 58)
store i8* %18, i8** %6
store i32 0, i32* %7
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %60
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8* %23, i8** %8
br label %24
24: ; preds = %40, %21
%25 = load i8*, i8** %8
%26 = load i8, i8* %25
%27 = icmp ne i8 %26, 0
br i1 %27, label %28, label %51
28: ; preds = %24
%29 = load i8*, i8** %8
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp slt i32 %31, 48
br i1 %32, label %38, label %33
33: ; preds = %28
%34 = load i8*, i8** %8
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp sgt i32 %36, 57
br i1 %37, label %38, label %40
38: ; preds = %33, %28
%39 = load i32, i32* @EINVAL
store i32 %39, i32* @errno
store i32 -1, i32* %3
br label %115
40: ; preds = %33
%41 = load i32, i32* %7
%42 = mul nsw i32 %41, 10
store i32 %42, i32* %7
%43 = load i8*, i8** %8
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = sub nsw i32 %45, 48
%47 = load i32, i32* %7
%48 = add nsw i32 %47, %46
store i32 %48, i32* %7
%49 = load i8*, i8** %8
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %8
br label %24
51: ; preds = %24
%52 = load i32, i32* %7
%53 = icmp slt i32 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %51
%55 = load i32, i32* %7
%56 = icmp sgt i32 %55, 65535
br i1 %56, label %57, label %59
57: ; preds = %54, %51
%58 = load i32, i32* @EINVAL
store i32 %58, i32* @errno
store i32 -1, i32* %3
br label %115
59: ; preds = %54
br label %60
60: ; preds = %59, %2
%61 = load i32, i32* %7
%62 = call i32 @htons(i32 %61)
%63 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %63, i32 0, i32 1
store i32 %62, i32* %64
%65 = load i8*, i8** %6
%66 = icmp ne i8* %65, null
br i1 %66, label %69, label %67
67: ; preds = %60
%68 = load i8*, i8** %4
store i8* %68, i8** %9
br label %95
69: ; preds = %60
%70 = load i8*, i8** %6
%71 = load i8*, i8** %4
%72 = ptrtoint i8* %70 to i64
%73 = ptrtoint i8* %71 to i64
%74 = sub i64 %72, %73
%75 = add nsw i64 %74, 1
%76 = icmp ule i64 %75, 256
%77 = zext i1 %76 to i32
%78 = call i32 @assert(i32 %77)
%79 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%80 = load i8*, i8** %4
%81 = load i8*, i8** %6
%82 = load i8*, i8** %4
%83 = ptrtoint i8* %81 to i64
%84 = ptrtoint i8* %82 to i64
%85 = sub i64 %83, %84
%86 = trunc i64 %85 to i32
%87 = call i32 @memcpy(i8* %79, i8* %80, i32 %86)
%88 = load i8*, i8** %6
%89 = load i8*, i8** %4
%90 = ptrtoint i8* %88 to i64
%91 = ptrtoint i8* %89 to i64
%92 = sub i64 %90, %91
%93 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %92
store i8 0, i8* %93
%94 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
store i8* %94, i8** %9
br label %95
95: ; preds = %69, %67
%96 = load i8*, i8** %9
%97 = call i64 @strcmp(i8* %96, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%98 = icmp eq i64 %97, 0
br i1 %98, label %99, label %104
99: ; preds = %95
%100 = load i32, i32* @INADDR_ANY
%101 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%102 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %101, i32 0, i32 0
%103 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %102, i32 0, i32 0
store i32 %100, i32* %103
store i32 0, i32* %3
br label %115
104: ; preds = %95
%105 = load i32, i32* @AF_INET
%106 = load i8*, i8** %9
%107 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%108 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %107, i32 0, i32 0
%109 = call i32 @inet_pton(i32 %105, i8* %106, %struct.TYPE_2__* %108)
store i32 %109, i32* %11
%110 = load i32, i32* %11
%111 = icmp ne i32 %110, 1
br i1 %111, label %112, label %114
112: ; preds = %104
%113 = load i32, i32* @EINVAL
store i32 %113, i32* @errno
store i32 -1, i32* %3
br label %115
114: ; preds = %104
store i32 0, i32* %3
br label %115
115: ; preds = %114, %112, %99, %57, %38
%116 = load i32, i32* %3
ret i32 %116
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"*\00"
@INADDR_ANY = global i32 0
define i32 @resolve_ip4_literal_addr(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca [256 x i8]
%4 = tail call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 58)
%8 = icmp eq i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %37, label %17
13: ; preds = %2
%14 = tail call i32 @htons(i32 0)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
br label %52
17: ; preds = %9, %25
%18 = phi i8 [ %31, %25 ], [ %11, %9 ]
%19 = phi i8* [ %30, %25 ], [ %10, %9 ]
%20 = phi i32 [ %29, %25 ], [ 0, %9 ]
%21 = add i8 %18, -58
%22 = icmp ult i8 %21, -10
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* @EINVAL
store i32 %24, i32* @errno
br label %69
25: ; preds = %17
%26 = zext i8 %18 to i32
%27 = mul nsw i32 %20, 10
%28 = add i32 %27, -48
%29 = add i32 %28, %26
%30 = getelementptr inbounds i8, i8* %19, i64 1
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %33, label %17
33: ; preds = %25
%34 = icmp ugt i32 %29, 65535
br i1 %34, label %35, label %37
35: ; preds = %33
%36 = load i32, i32* @EINVAL
store i32 %36, i32* @errno
br label %69
37: ; preds = %9, %33
%38 = phi i32 [ %29, %33 ], [ 0, %9 ]
%39 = tail call i32 @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %39, i32* %40
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%42 = ptrtoint i8* %7 to i64
%43 = ptrtoint i8* %0 to i64
%44 = sub i64 %42, %43
%45 = add nsw i64 %44, 1
%46 = icmp ult i64 %45, 257
%47 = zext i1 %46 to i32
%48 = tail call i32 @assert(i32 %47)
%49 = trunc i64 %44 to i32
%50 = call i32 @memcpy(i8* %41, i8* %0, i32 %49)
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %44
store i8 0, i8* %51
br label %52
52: ; preds = %13, %37
%53 = phi i8* [ %41, %37 ], [ %0, %13 ]
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%55 = call i64 @strcmp(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i32, i32* @INADDR_ANY
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %58, i32* %59
br label %67
60: ; preds = %52
%61 = load i32, i32* @AF_INET
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%63 = call i32 @inet_pton(i32 %61, i8* %53, %struct.TYPE_2__* %62)
%64 = icmp eq i32 %63, 1
br i1 %64, label %67, label %65
65: ; preds = %60
%66 = load i32, i32* @EINVAL
store i32 %66, i32* @errno
br label %67
67: ; preds = %65, %60, %57
%68 = phi i32 [ 0, %57 ], [ -1, %65 ], [ 0, %60 ]
br label %69
69: ; preds = %23, %35, %67
%70 = phi i32 [ %68, %67 ], [ -1, %35 ], [ -1, %23 ]
ret i32 %70
}