#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 /*<<< orphan*/ s_addr;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int _evdns_nameserver_add_impl(int /*<<< orphan*/, int);
int /*<<< orphan*/ inet_aton(char const *, struct in_addr *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strchr(char const *, char);
int strtoint(char const *);
int evdns_nameserver_ip_add(const char *ip_as_string) {
  struct in_addr ina;
  int port;
  char buf[20];
  const char *cp;
  cp = strchr(ip_as_string, ':');
  if (!cp) {
    cp = ip_as_string;
    port = 53;
  } else {
    port = strtoint(cp + 1);
    if (port < 0 || port > 65535) {
      return 4;
    }
    if ((cp - ip_as_string) >= (int)sizeof(buf)) {
      return 4;
    }
    memcpy(buf, ip_as_string, cp - ip_as_string);
    buf[cp - ip_as_string] = '\0';
    cp = buf;
  }
  if (!inet_aton(cp, &ina)) {
    return 4;
  }
  return _evdns_nameserver_add_impl(ina.s_addr, port);
}


x86 assembly (clang, -O0):
<evdns_nameserver_ip_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    118d <evdns_nameserver_ip_add+0x3d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x35,-0x1c(%rbp)
jmp    1213 <evdns_nameserver_ip_add+0xc3>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   1030 <strtoint@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11b4 <evdns_nameserver_ip_add+0x64>
cmpl   $0xffff,-0x1c(%rbp)
jle    11c0 <evdns_nameserver_ip_add+0x70>
movl   $0x4,-0x4(%rbp)
jmp    1243 <evdns_nameserver_ip_add+0xf3>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x14,%rax
jl     11e1 <evdns_nameserver_ip_add+0x91>
movl   $0x4,-0x4(%rbp)
jmp    1243 <evdns_nameserver_ip_add+0xf3>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%eax
jne    1235 <evdns_nameserver_ip_add+0xe5>
movl   $0x4,-0x4(%rbp)
jmp    1243 <evdns_nameserver_ip_add+0xf3>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <_evdns_nameserver_add_impl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evdns_nameserver_ip_add>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11ae <evdns_nameserver_ip_add+0x5e>
mov    %rax,%rbp
lea    0x1(%rax),%rdi
call   1030 <strtoint@plt>
mov    %eax,%r14d
mov    $0x4,%eax
cmp    $0xffff,%r14d
ja     11d8 <evdns_nameserver_ip_add+0x88>
sub    %rbx,%rbp
cmp    $0x13,%rbp
jg     11d8 <evdns_nameserver_ip_add+0x88>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
movb   $0x0,0x10(%rsp,%rbp,1)
mov    %r15,%rbx
jmp    11b4 <evdns_nameserver_ip_add+0x64>
mov    $0x35,%r14d
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <inet_aton@plt>
test   %eax,%eax
je     11d3 <evdns_nameserver_ip_add+0x83>
mov    0x8(%rsp),%edi
mov    %r14d,%esi
call   1050 <_evdns_nameserver_add_impl@plt>
jmp    11d8 <evdns_nameserver_ip_add+0x88>
mov    $0x4,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
define i32 @evdns_nameserver_ip_add(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.in_addr
%5 = alloca i32
%6 = alloca [20 x i8]
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strchr(i8* %8, i8 signext 58)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp ne i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
store i8* %13, i8** %7
store i32 53, i32* %5
br label %49
14: ; preds = %1
%15 = load i8*, i8** %7
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = call i32 @strtoint(i8* %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %14
%21 = load i32, i32* %5
%22 = icmp sgt i32 %21, 65535
br i1 %22, label %23, label %24
23: ; preds = %20, %14
store i32 4, i32* %2
br label %59
24: ; preds = %20
%25 = load i8*, i8** %7
%26 = load i8*, i8** %3
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = icmp sge i64 %29, 20
br i1 %30, label %31, label %32
31: ; preds = %24
store i32 4, i32* %2
br label %59
32: ; preds = %24
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%34 = load i8*, i8** %3
%35 = load i8*, i8** %7
%36 = load i8*, i8** %3
%37 = ptrtoint i8* %35 to i64
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %37, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @memcpy(i8* %33, i8* %34, i32 %40)
%42 = load i8*, i8** %7
%43 = load i8*, i8** %3
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
%47 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 %46
store i8 0, i8* %47
%48 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
store i8* %48, i8** %7
br label %49
49: ; preds = %32, %12
%50 = load i8*, i8** %7
%51 = call i32 @inet_aton(i8* %50, %struct.in_addr* %4)
%52 = icmp ne i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %49
store i32 4, i32* %2
br label %59
54: ; preds = %49
%55 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i32 0, i32 0
%56 = load i32, i32* %55
%57 = load i32, i32* %5
%58 = call i32 @_evdns_nameserver_add_impl(i32 %56, i32 %57)
store i32 %58, i32* %2
br label %59
59: ; preds = %54, %53, %31, %23
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
define i32 @evdns_nameserver_ip_add(i8* %0) {
%2 = alloca %struct.in_addr
%3 = alloca [20 x i8]
%4 = bitcast %struct.in_addr* %2 to i8*
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%6 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 58)
%7 = icmp eq i8* %6, null
br i1 %7, label %21, label %8
8: ; preds = %1
%9 = getelementptr inbounds i8, i8* %6, i64 1
%10 = tail call i32 @strtoint(i8* %9)
%11 = icmp ugt i32 %10, 65535
br i1 %11, label %30, label %12
12: ; preds = %8
%13 = ptrtoint i8* %6 to i64
%14 = ptrtoint i8* %0 to i64
%15 = sub i64 %13, %14
%16 = icmp sgt i64 %15, 19
br i1 %16, label %30, label %17
17: ; preds = %12
%18 = trunc i64 %15 to i32
%19 = call i32 @memcpy(i8* %5, i8* %0, i32 %18)
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %15
store i8 0, i8* %20
br label %21
21: ; preds = %1, %17
%22 = phi i32 [ %10, %17 ], [ 53, %1 ]
%23 = phi i8* [ %5, %17 ], [ %0, %1 ]
%24 = call i32 @inet_aton(i8* %23, %struct.in_addr* %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %21
%27 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @_evdns_nameserver_add_impl(i32 %28, i32 %22)
br label %30
30: ; preds = %21, %12, %8, %26
%31 = phi i32 [ %29, %26 ], [ 4, %8 ], [ 4, %12 ], [ 4, %21 ]
ret i32 %31
}