#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_3__ TYPE_1__;

/* Type definitions */
struct sockaddr_un {
  int sun_family;
  int /*<<< orphan*/ sun_path;
};
struct TYPE_3__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in6 {
  int sin6_family;
  TYPE_1__ sin6_addr;
  void *sin6_port;
};
struct sockaddr_in {
  int sin_family;
  TYPE_1__ sin_addr;
  void *sin_port;
};

/* Variables and functions */
int AF_INET;
int AF_INET6;
int AF_UNIX;
int /*<<< orphan*/ INADDR_ANY;
int IPPROTO_TCP;
void *htons(int);
TYPE_1__ in6addr_any;
int /*<<< orphan*/ inet_pton(int, char const *, TYPE_1__ *);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int gen_address(int netproto, void *buffer, const char *addr, int port,
                int *subproto) {
  struct sockaddr_un *sun = buffer;
  struct sockaddr_in *sin = buffer;
  struct sockaddr_in6 *sin6 = buffer;
  *subproto = 0;
  if (netproto == AF_UNIX) {
    if (!addr)
      return -1;
    sun->sun_family = AF_UNIX;
    strcpy(sun->sun_path, addr);
    return sizeof(*sun);
  }
  if (port < 0 || port > 0xFFFF)
    return -1;
  *subproto = IPPROTO_TCP;
  if (netproto == AF_INET) {
    sin->sin_family = AF_INET;
    sin->sin_addr.s_addr = INADDR_ANY;
    sin->sin_port = htons(port);
    if (addr && strcmp(addr, "ANY"))
      inet_pton(AF_INET, addr, &(sin->sin_addr));
    return sizeof(*sin);
  }
  if (netproto == AF_INET6) {
    sin6->sin6_family = AF_INET6;
    sin6->sin6_addr = in6addr_any;
    sin6->sin6_port = htons(port);
    if (addr && strcmp(addr, "ANY"))
      inet_pton(AF_INET6, addr, &(sin6->sin6_addr));
    return sizeof(*sin6);
  }
  return -1;
}


x86 assembly (clang, -O0):
<gen_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%eax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%eax
jne    11d0 <gen_address+0x90>
cmpq   $0x0,-0x18(%rbp)
jne    11a5 <gen_address+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <gen_address+0x1e7>
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x8,-0x4(%rbp)
jmp    1327 <gen_address+0x1e7>
cmpl   $0x0,-0x1c(%rbp)
jl     11e7 <gen_address+0xa7>
cmpl   $0xffff,-0x1c(%rbp)
jle    11f3 <gen_address+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <gen_address+0x1e7>
mov    0x2dce(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%eax
jne    1291 <gen_address+0x151>
mov    0x2dc5(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2da6(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x8(%rax)
cmpq   $0x0,-0x18(%rbp)
je     1285 <gen_address+0x145>
mov    -0x18(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1285 <gen_address+0x145>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
add    $0x4,%rdx
call   1060 <inet_pton@plt>
movl   $0x10,-0x4(%rbp)
jmp    1327 <gen_address+0x1e7>
mov    -0x8(%rbp),%eax
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%eax
jne    1320 <gen_address+0x1e0>
mov    0x2d16(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    0x2d33(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x8(%rax)
cmpq   $0x0,-0x18(%rbp)
je     1314 <gen_address+0x1d4>
mov    -0x18(%rbp),%rdi
lea    0xd15(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1314 <gen_address+0x1d4>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x40(%rbp),%rdx
add    $0x4,%rdx
call   1060 <inet_pton@plt>
movl   $0x10,-0x4(%rbp)
jmp    1327 <gen_address+0x1e7>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_address>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
movl   $0x0,(%r8)
mov    0x2e7d(%rip),%rax
cmp    %edi,(%rax)
jne    117b <gen_address+0x3b>
test   %r14,%r14
je     11ab <gen_address+0x6b>
mov    %edi,(%rbx)
mov    0x4(%rbx),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    $0x8,%ebp
jmp    1207 <gen_address+0xc7>
mov    $0xffffffff,%ebp
cmp    $0xffff,%ecx
ja     1207 <gen_address+0xc7>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r8)
mov    0x2e45(%rip),%r15
cmp    %edi,(%r15)
jne    11b2 <gen_address+0x72>
mov    %edi,(%rbx)
mov    0x2e27(%rip),%rax
jmp    11c7 <gen_address+0x87>
mov    $0xffffffff,%ebp
jmp    1207 <gen_address+0xc7>
mov    0x2e07(%rip),%r15
cmp    %edi,(%r15)
jne    1207 <gen_address+0xc7>
mov    %edi,(%rbx)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    %ecx,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rbx)
mov    $0x10,%ebp
test   %r14,%r14
je     1207 <gen_address+0xc7>
lea    0xe18(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1207 <gen_address+0xc7>
add    $0x4,%rbx
mov    (%r15),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <inet_pton@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_3__, i8* }
%struct.sockaddr_in6 = type { i32, %struct.TYPE_3__, i8* }
@AF_UNIX = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [4 x i8] c"ANY\00"
@AF_INET6 = global i32 0
@in6addr_any = global %struct.TYPE_3__ zeroinitializer
define i32 @gen_address(i32 %0, i8* %1, i8* %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca %struct.sockaddr_un*
%13 = alloca %struct.sockaddr_in*
%14 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%15 = load i8*, i8** %8
%16 = bitcast i8* %15 to %struct.sockaddr_un*
store %struct.sockaddr_un* %16, %struct.sockaddr_un** %12
%17 = load i8*, i8** %8
%18 = bitcast i8* %17 to %struct.sockaddr_in*
store %struct.sockaddr_in* %18, %struct.sockaddr_in** %13
%19 = load i8*, i8** %8
%20 = bitcast i8* %19 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %20, %struct.sockaddr_in6** %14
%21 = load i32*, i32** %11
store i32 0, i32* %21
%22 = load i32, i32* %7
%23 = load i32, i32* @AF_UNIX
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %38
25: ; preds = %5
%26 = load i8*, i8** %9
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %25
store i32 -1, i32* %6
br label %105
29: ; preds = %25
%30 = load i32, i32* @AF_UNIX
%31 = load %struct.sockaddr_un*, %struct.sockaddr_un** %12
%32 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load %struct.sockaddr_un*, %struct.sockaddr_un** %12
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %33, i32 0, i32 1
%35 = load i32, i32* %34
%36 = load i8*, i8** %9
%37 = call i32 @strcpy(i32 %35, i8* %36)
store i32 8, i32* %6
br label %105
38: ; preds = %5
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38
%42 = load i32, i32* %10
%43 = icmp sgt i32 %42, 65535
br i1 %43, label %44, label %45
44: ; preds = %41, %38
store i32 -1, i32* %6
br label %105
45: ; preds = %41
%46 = load i32, i32* @IPPROTO_TCP
%47 = load i32*, i32** %11
store i32 %46, i32* %47
%48 = load i32, i32* %7
%49 = load i32, i32* @AF_INET
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %76
51: ; preds = %45
%52 = load i32, i32* @AF_INET
%53 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %53, i32 0, i32 0
store i32 %52, i32* %54
%55 = load i32, i32* @INADDR_ANY
%56 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %56, i32 0, i32 1
%58 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %57, i32 0, i32 0
store i32 %55, i32* %58
%59 = load i32, i32* %10
%60 = call i8* @htons(i32 %59)
%61 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %61, i32 0, i32 2
store i8* %60, i8** %62
%63 = load i8*, i8** %9
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %75
65: ; preds = %51
%66 = load i8*, i8** %9
%67 = call i64 @strcmp(i8* %66, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %65
%70 = load i32, i32* @AF_INET
%71 = load i8*, i8** %9
%72 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%73 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %72, i32 0, i32 1
%74 = call i32 @inet_pton(i32 %70, i8* %71, %struct.TYPE_3__* %73)
br label %75
75: ; preds = %69, %65, %51
store i32 16, i32* %6
br label %105
76: ; preds = %45
%77 = load i32, i32* %7
%78 = load i32, i32* @AF_INET6
%79 = icmp eq i32 %77, %78
br i1 %79, label %80, label %104
80: ; preds = %76
%81 = load i32, i32* @AF_INET6
%82 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%83 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %82, i32 0, i32 0
store i32 %81, i32* %83
%84 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%85 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %84, i32 0, i32 1
%86 = bitcast %struct.TYPE_3__* %85 to i8*
%87 = load i32, i32* %10
%88 = call i8* @htons(i32 %87)
%89 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%90 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %89, i32 0, i32 2
store i8* %88, i8** %90
%91 = load i8*, i8** %9
%92 = icmp ne i8* %91, null
br i1 %92, label %93, label %103
93: ; preds = %80
%94 = load i8*, i8** %9
%95 = call i64 @strcmp(i8* %94, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %103
97: ; preds = %93
%98 = load i32, i32* @AF_INET6
%99 = load i8*, i8** %9
%100 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%101 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %100, i32 0, i32 1
%102 = call i32 @inet_pton(i32 %98, i8* %99, %struct.TYPE_3__* %101)
br label %103
103: ; preds = %97, %93, %80
store i32 16, i32* %6
br label %105
104: ; preds = %76
store i32 -1, i32* %6
br label %105
105: ; preds = %104, %103, %75, %44, %29, %28
%106 = load i32, i32* %6
ret i32 %106
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@AF_UNIX = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [4 x i8] c"ANY\00"
@AF_INET6 = global i32 0
@in6addr_any = global %struct.TYPE_3__ zeroinitializer
define i32 @gen_address(i32 %0, i8* %1, i8* %2, i32 %3, i32* nocapture writeonly %4) {
store i32 0, i32* %4
%6 = load i32, i32* @AF_UNIX
%7 = icmp eq i32 %6, %0
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = icmp eq i8* %2, null
br i1 %9, label %57, label %10
10: ; preds = %8
%11 = bitcast i8* %1 to i32*
store i32 %0, i32* %11
%12 = getelementptr inbounds i8, i8* %1, i64 4
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
%15 = tail call i32 @strcpy(i32 %14, i8* %2)
br label %57
16: ; preds = %5
%17 = icmp ugt i32 %3, 65535
br i1 %17, label %57, label %18
18: ; preds = %16
%19 = load i32, i32* @IPPROTO_TCP
store i32 %19, i32* %4
%20 = load i32, i32* @AF_INET
%21 = icmp eq i32 %20, %0
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = bitcast i8* %1 to i32*
store i32 %0, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds i8, i8* %1, i64 4
%26 = bitcast i8* %25 to %struct.TYPE_3__*
%27 = bitcast i8* %25 to i32*
store i32 %24, i32* %27
%28 = tail call i8* @htons(i32 %3)
%29 = getelementptr inbounds i8, i8* %1, i64 8
%30 = bitcast i8* %29 to i8**
store i8* %28, i8** %30
%31 = icmp eq i8* %2, null
br i1 %31, label %57, label %32
32: ; preds = %22
%33 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %57, label %35
35: ; preds = %32
%36 = load i32, i32* @AF_INET
%37 = tail call i32 @inet_pton(i32 %36, i8* %2, %struct.TYPE_3__* %26)
br label %57
38: ; preds = %18
%39 = load i32, i32* @AF_INET6
%40 = icmp eq i32 %39, %0
br i1 %40, label %41, label %57
41: ; preds = %38
%42 = bitcast i8* %1 to i32*
store i32 %0, i32* %42
%43 = getelementptr inbounds i8, i8* %1, i64 4
%44 = bitcast i8* %43 to %struct.TYPE_3__*
%45 = bitcast i8* %43 to i32*
%46 = load i32, i32* getelementptr inbounds (%struct.TYPE_3__, %struct.TYPE_3__* @in6addr_any, i64 0, i32 0)
store i32 %46, i32* %45
%47 = tail call i8* @htons(i32 %3)
%48 = getelementptr inbounds i8, i8* %1, i64 8
%49 = bitcast i8* %48 to i8**
store i8* %47, i8** %49
%50 = icmp eq i8* %2, null
br i1 %50, label %57, label %51
51: ; preds = %41
%52 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %51
%55 = load i32, i32* @AF_INET6
%56 = tail call i32 @inet_pton(i32 %55, i8* %2, %struct.TYPE_3__* %44)
br label %57
57: ; preds = %38, %41, %51, %54, %22, %32, %35, %16, %8, %10
%58 = phi i32 [ 8, %10 ], [ -1, %8 ], [ -1, %16 ], [ 16, %35 ], [ 16, %32 ], [ 16, %22 ], [ 16, %54 ], [ 16, %51 ], [ 16, %41 ], [ -1, %38 ]
ret i32 %58
}