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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ htons(unsigned int);
int inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin) {
  sin->sin_family = AF_INET;
  sin->sin_port = htons(port);
  if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
    fprintf(stderr, "Failed to translate %s into sin_addr\n", s);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<parse_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
je     11a8 <parse_ipv4+0x78>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe69(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <parse_ipv4+0x7f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ipv4>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rdi,%r14
mov    0x2ea6(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rdx)
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    (%r15),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <inet_pton@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
je     1186 <parse_ipv4+0x56>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"Failed to translate %s into sin_addr\0A\00"
define i32 @parse_ipv4(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%8 = load i32, i32* @AF_INET
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %8, i32* %10
%11 = load i32, i32* %6
%12 = call i32 @htons(i32 %11)
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
store i32 %12, i32* %14
%15 = load i32, i32* @AF_INET
%16 = load i8*, i8** %5
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
%19 = call i32 @inet_pton(i32 %15, i8* %16, i32* %18)
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %5
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 -1, i32* %4
br label %26
25: ; preds = %3
store i32 0, i32* %4
br label %26
26: ; preds = %25, %21
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"Failed to translate %s into sin_addr\0A\00"
define i32 @parse_ipv4(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %4, i32* %5
%6 = tail call i32 @htons(i32 %1)
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %6, i32* %7
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%10 = tail call i32 @inet_pton(i32 %8, i8* %0, i32* %9)
%11 = icmp eq i32 %10, 1
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %3, %12
%16 = phi i32 [ -1, %12 ], [ 0, %3 ]
ret i32 %16
}