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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ inet_aton(char const *, TYPE_1__ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
void sockaddr_init(const char *address, unsigned short port,
                   struct sockaddr *sockaddr) {
  struct sockaddr_in sockaddr_in;
  sockaddr_in.sin_family = AF_INET;
  if (inet_aton(address, &sockaddr_in.sin_addr) == 0) {
    if (strcmp(address, "") == 0) {
      sockaddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
    } else {
      fprintf(stderr, "Invalid IP Address.\n");
      exit(EXIT_FAILURE);
    }
  }
  if (port == 0) {
    fprintf(stderr, "invalid port number.\n");
    exit(EXIT_FAILURE);
  }
  sockaddr_in.sin_port = htons(port);
  *sockaddr = *((struct sockaddr *)&sockaddr_in);
}


x86 assembly (clang, -O0):
<sockaddr_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jne    11f6 <sockaddr_init+0x96>
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11ce <sockaddr_init+0x6e>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x24(%rbp)
jmp    11f1 <sockaddr_init+0x91>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11f6 <sockaddr_init+0x96>
movzwl -0xa(%rbp),%eax
cmp    $0x0,%eax
jne    1226 <sockaddr_init+0xc6>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movzwl -0xa(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rsi
call   1070 <inet_aton@plt>
test   %rax,%rax
jne    11b2 <sockaddr_init+0x52>
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11dd <sockaddr_init+0x7d>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
test   %bp,%bp
je     11cb <sockaddr_init+0x6b>
movzwl %bp,%edi
call   1030 <htons@plt>
mov    %eax,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
jmp    11ed <sockaddr_init+0x8d>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@.str = [1 x i8] zeroinitializer
@INADDR_ANY = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"Invalid IP Address.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [22 x i8] c"invalid port number.\0A\00"
define void @sockaddr_init(i8* %0, i16 zeroext %1, %struct.sockaddr* %2) {
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.sockaddr*
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i16 %1, i16* %5
store %struct.sockaddr* %2, %struct.sockaddr** %6
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i8*, i8** %4
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%12 = call i64 @inet_aton(i8* %10, %struct.TYPE_2__* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
br label %28
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
br label %29
29: ; preds = %28, %3
%30 = load i16, i16* %5
%31 = zext i16 %30 to i32
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = load i16, i16* %5
%40 = call i32 @htons(i16 zeroext %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %40, i32* %41
%42 = load %struct.sockaddr*, %struct.sockaddr** %6
%43 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%44 = bitcast %struct.sockaddr* %42 to i8*
%45 = bitcast %struct.sockaddr* %43 to i8*
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@.str = [1 x i8] zeroinitializer
@INADDR_ANY = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"Invalid IP Address.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [22 x i8] c"invalid port number.\0A\00"
define void @sockaddr_init(i8* %0, i16 zeroext %1, %struct.sockaddr* nocapture writeonly %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%9 = call i64 @inet_aton(i8* %0, %struct.TYPE_2__* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %3
%12 = call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i64 0, i32 0
store i32 %16, i32* %17
br label %23
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %14, %3
%24 = icmp eq i16 %1, 0
br i1 %24, label %25, label %30
25: ; preds = %23
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %23
%31 = call i32 @htons(i16 zeroext %1)
%32 = getelementptr %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
store i32 %31, i32* %32
ret void
}