#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;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EINVAL;
int atoi(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, struct in_addr *);
char *malloc(scalar_t__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int sock_strtoaddr(const char *s, struct sockaddr *saout) {
  char *ips, *ps;
  struct in_addr ia;
  int port;
  int rc;
  ips = malloc(strlen(s) + 1);
  strcpy(ips, s);
  ps = strchr(ips, ':');
  if (ps != NULL) {
    *ps = '\0';
    ps++;
  } else
    ps = "0";
  rc = inet_pton(AF_INET, ips, &ia);
  if (rc == 1) {
    port = atoi(ps);
    if (port > 65535)
      rc = 0;
  }
  if (rc == 1) {
    struct sockaddr_in *sa;
    sa = (struct sockaddr_in *)saout;
    memset(sa, 0, sizeof(struct sockaddr_in));
    sa->sin_family = AF_INET;
    sa->sin_addr = ia;
    sa->sin_port = htons(port);
  }
  if (rc != 1) {
    if (rc != -1)
      errno = EINVAL;
  }
  free(ips);
  return rc;
}


x86 assembly (clang, -O0):
<sock_strtoaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11fb <sock_strtoaddr+0x6b>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1206 <sock_strtoaddr+0x76>
lea    0xdfe(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1090 <inet_pton@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x1,-0x30(%rbp)
jne    124e <sock_strtoaddr+0xbe>
mov    -0x20(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffff,-0x2c(%rbp)
jle    1249 <sock_strtoaddr+0xb9>
movl   $0x0,-0x30(%rbp)
jmp    124e <sock_strtoaddr+0xbe>
cmpl   $0x1,-0x30(%rbp)
jne    129a <sock_strtoaddr+0x10a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,0x4(%rax)
mov    -0x2c(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x1,-0x30(%rbp)
je     12c5 <sock_strtoaddr+0x135>
cmpl   $0xffffffff,-0x30(%rbp)
je     12c0 <sock_strtoaddr+0x130>
mov    0x2d1b(%rip),%rax
mov    (%rax),%ecx
mov    0x2d1a(%rip),%rax
mov    %ecx,(%rax)
jmp    12c5 <sock_strtoaddr+0x135>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_strtoaddr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11dc <sock_strtoaddr+0x4c>
mov    %rax,%rbx
movb   $0x0,(%rax)
add    $0x1,%rbx
jmp    11e3 <sock_strtoaddr+0x53>
lea    0xe1d(%rip),%rbx
mov    0x2dfe(%rip),%r12
mov    (%r12),%edi
lea    0x8(%rsp),%rdx
mov    %r15,%rsi
call   1090 <inet_pton@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     125c <sock_strtoaddr+0xcc>
cmp    $0x1,%ebp
jne    1218 <sock_strtoaddr+0x88>
mov    %rbx,%rdi
call   10b0 <atoi@plt>
xor    %ebp,%ebp
cmp    $0xffff,%eax
jle    122c <sock_strtoaddr+0x9c>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    0x2db0(%rip),%rcx
mov    %eax,(%rcx)
jmp    125c <sock_strtoaddr+0xcc>
mov    %eax,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%r14)
mov    0x8(%rsp),%eax
mov    %eax,0x4(%r14)
mov    %ebx,%edi
call   1060 <htons@plt>
mov    %eax,(%r14)
mov    $0x1,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
@.str = [2 x i8] c"0\00"
@AF_INET = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @sock_strtoaddr(i8* %0, %struct.sockaddr* %1) {
%3 = alloca i8*
%4 = alloca %struct.sockaddr*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.in_addr
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in*
store i8* %0, i8** %3
store %struct.sockaddr* %1, %struct.sockaddr** %4
%11 = load i8*, i8** %3
%12 = call i64 @strlen(i8* %11)
%13 = add nsw i64 %12, 1
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = load i8*, i8** %3
%17 = call i32 @strcpy(i8* %15, i8* %16)
%18 = load i8*, i8** %5
%19 = call i8* @strchr(i8* %18, i8 signext 58)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = load i8*, i8** %6
store i8 0, i8* %23
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %6
br label %27
26: ; preds = %2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6
br label %27
27: ; preds = %26, %22
%28 = load i32, i32* @AF_INET
%29 = load i8*, i8** %5
%30 = call i32 @inet_pton(i32 %28, i8* %29, %struct.in_addr* %7)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = load i8*, i8** %6
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp sgt i32 %36, 65535
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 0, i32* %9
br label %39
39: ; preds = %38, %33
br label %40
40: ; preds = %39, %27
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %59
43: ; preds = %40
%44 = load %struct.sockaddr*, %struct.sockaddr** %4
%45 = bitcast %struct.sockaddr* %44 to %struct.sockaddr_in*
store %struct.sockaddr_in* %45, %struct.sockaddr_in** %10
%46 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%47 = call i32 @memset(%struct.sockaddr_in* %46, i32 0, i32 12)
%48 = load i32, i32* @AF_INET
%49 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %49, i32 0, i32 2
store i32 %48, i32* %50
%51 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %51, i32 0, i32 1
%53 = bitcast %struct.in_addr* %52 to i8*
%54 = bitcast %struct.in_addr* %7 to i8*
%55 = load i32, i32* %8
%56 = call i32 @htons(i32 %55)
%57 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %57, i32 0, i32 0
store i32 %56, i32* %58
br label %59
59: ; preds = %43, %40
%60 = load i32, i32* %9
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = load i32, i32* %9
%64 = icmp ne i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = load i32, i32* @EINVAL
store i32 %66, i32* @errno
br label %67
67: ; preds = %65, %62
br label %68
68: ; preds = %67, %59
%69 = load i8*, i8** %5
%70 = call i32 @free(i8* %69)
%71 = load i32, i32* %9
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
@.str = [2 x i8] c"0\00"
@AF_INET = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @sock_strtoaddr(i8* %0, %struct.sockaddr* %1) {
%3 = alloca %struct.in_addr
%4 = bitcast %struct.in_addr* %3 to i8*
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add nsw i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = tail call i32 @strcpy(i8* %7, i8* %0)
%9 = tail call i8* @strchr(i8* dereferenceable(1) %7, i8 signext 58)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %2
store i8 0, i8* %9
%12 = getelementptr inbounds i8, i8* %9, i64 1
br label %13
13: ; preds = %2, %11
%14 = phi i8* [ %12, %11 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%15 = load i32, i32* @AF_INET
%16 = call i32 @inet_pton(i32 %15, i8* %7, %struct.in_addr* %3)
switch i32 %16, label %30 [
i32 1, label %17
i32 -1, label %33
]
17: ; preds = %13
%18 = call i32 @atoi(i8* %14)
%19 = icmp sgt i32 %18, 65535
br i1 %19, label %30, label %20
20: ; preds = %17
%21 = bitcast %struct.sockaddr* %1 to %struct.sockaddr_in*
%22 = call i32 @memset(%struct.sockaddr_in* %21, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 2, i32 0
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%26 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 1, i32 0
%27 = load i32, i32* %25
store i32 %27, i32* %26
%28 = call i32 @htons(i32 %18)
%29 = getelementptr %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
store i32 %28, i32* %29
br label %33
30: ; preds = %13, %17
%31 = phi i32 [ 0, %17 ], [ %16, %13 ]
%32 = load i32, i32* @EINVAL
store i32 %32, i32* @errno
br label %33
33: ; preds = %13, %20, %30
%34 = phi i32 [ 1, %20 ], [ %31, %30 ], [ %16, %13 ]
%35 = call i32 @free(i8* %7)
ret i32 %34
}