#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_storage {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, struct sockaddr_in *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
bool ipv4_str_to_sockaddr(const char *ipaddr, uint16_t port,
                          struct sockaddr_storage *sockaddr) {
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if (inet_pton(addr.sin_family, ipaddr, &addr.sin_addr) != 1)
    return false;
  memcpy(sockaddr, &addr, sizeof(addr));
  return true;
}


x86 assembly (clang, -O0):
<ipv4_str_to_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1060 <inet_pton@plt>
cmp    $0x1,%eax
je     119f <ipv4_str_to_sockaddr+0x5f>
movl   $0x0,-0x4(%rbp)
jmp    11b8 <ipv4_str_to_sockaddr+0x78>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1050 <memcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv4_str_to_sockaddr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
mov    %rsp,%r12
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x4(%rsp),%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <inet_pton@plt>
cmp    $0x1,%eax
jne    11a9 <ipv4_str_to_sockaddr+0x69>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0xc,%edx
call   1050 <memcpy@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
@AF_INET = global i32 0
define i32 @ipv4_str_to_sockaddr(i8* %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
%9 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* %6
%13 = call i32 @htons(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %5
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%19 = call i32 @inet_pton(i32 %16, i8* %17, i32* %18)
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 0, i32* %4
br label %25
22: ; preds = %3
%23 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%24 = call i32 @memcpy(%struct.sockaddr_storage* %23, %struct.sockaddr_in* %8, i32 12)
store i32 1, i32* %4
br label %25
25: ; preds = %22, %21
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
@AF_INET = global i32 0
define i32 @ipv4_str_to_sockaddr(i8* %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %7, i32* %8
%9 = call i32 @htons(i32 %1)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* %8
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%13 = call i32 @inet_pton(i32 %11, i8* %0, i32* %12)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @memcpy(%struct.sockaddr_storage* %2, %struct.sockaddr_in* %4, i32 12)
br label %17
17: ; preds = %3, %15
%18 = phi i32 [ 1, %15 ], [ 0, %3 ]
ret i32 %18
}