#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_storage {
  int dummy;
};
struct sockaddr_in6 {
  void *sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr_in {
  int sin_family;
  void *sin_port;
};
typedef int /*<<< orphan*/ ss;
typedef int socklen_t;
typedef void *in_port_t;

/* Variables and functions */
int AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ PF_INET6;
int SOCK_DGRAM;
int bind(int, void const *, int);
int /*<<< orphan*/ err(int, char *);
void *htons(void *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int cr(int af, int type, in_port_t p) {
  int sfd;
  struct sockaddr_storage ss;
  socklen_t slen;
  sfd = socket(af == AF_INET ? PF_INET : PF_INET6, type, 0);
  if (sfd == -1)
    err(1, "socket");
  p = htons(p);
  memset(&ss, 0, sizeof(ss));
  if (af == AF_INET) {
    struct sockaddr_in *s = (void *)&ss;
    s->sin_family = AF_INET;
    slen = sizeof(*s);
    s->sin_port = p;
  } else {
    struct sockaddr_in6 *s6 = (void *)&ss;
    s6->sin6_family = AF_INET6;
    slen = sizeof(*s6);
    s6->sin6_port = p;
  }
  if (bind(sfd, (const void *)&ss, slen) == -1)
    err(1, "bind");
  if (type != SOCK_DGRAM)
    if (listen(sfd, 5) == -1)
      err(1, "listen");
  return sfd;
}


x86 assembly (clang, -O0):
<cr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%eax
jne    1195 <cr+0x35>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    11a1 <cr+0x41>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11cc <cr+0x6c>
mov    $0x1,%edi
lea    0xe39(%rip),%rsi
call   1050 <err@plt>
mov    -0x10(%rbp),%rdi
call   1030 <htons@plt>
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%eax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%eax
jne    122a <cr+0xca>
lea    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x10,-0x1c(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    1254 <cr+0xf4>
lea    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x8(%rax)
movl   $0x10,-0x1c(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    127d <cr+0x11d>
mov    $0x1,%edi
lea    0xd8f(%rip),%rsi
call   1050 <err@plt>
mov    -0x8(%rbp),%eax
mov    0x2d51(%rip),%rcx
cmp    (%rcx),%eax
je     12bb <cr+0x15b>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    12b6 <cr+0x156>
mov    $0x1,%edi
lea    0xd5b(%rip),%rsi
call   1050 <err@plt>
jmp    12bb <cr+0x15b>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e66(%rip),%r13
cmp    %edi,0x0(%r13)
jne    1189 <cr+0x29>
mov    0x2e39(%rip),%rax
jmp    1190 <cr+0x30>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    %r14d,%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11b5 <cr+0x55>
lea    0xe55(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    %r15,%rdi
call   1030 <htons@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
cmp    %ebp,0x0(%r13)
jne    11df <cr+0x7f>
mov    %ebp,(%rsp)
mov    %rbx,0x8(%rsp)
jmp    11f0 <cr+0x90>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,(%rsp)
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1216 <cr+0xb6>
lea    0xdfb(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    0x2dbb(%rip),%rax
cmp    %r14d,(%rax)
je     1245 <cr+0xe5>
mov    %r12d,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1245 <cr+0xe5>
lea    0xdd1(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { i32, i8* }
%struct.sockaddr_in6 = type { i8*, i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@PF_INET6 = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET6 = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@SOCK_DGRAM = global i32 0
@.str.2 = [7 x i8] c"listen\00"
define i32 @cr(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca %struct.sockaddr_in*
%11 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%12 = load i32, i32* %4
%13 = load i32, i32* @AF_INET
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* @PF_INET
br label %19
17: ; preds = %3
%18 = load i32, i32* @PF_INET6
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %16, %15 ], [ %18, %17 ]
%21 = load i32, i32* %5
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
%28 = load i8*, i8** %6
%29 = call i8* @htons(i8* %28)
store i8* %29, i8** %6
%30 = call i32 @memset(%struct.sockaddr_storage* %8, i32 0, i32 4)
%31 = load i32, i32* %4
%32 = load i32, i32* @AF_INET
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %43
34: ; preds = %27
%35 = bitcast %struct.sockaddr_storage* %8 to i8*
%36 = bitcast i8* %35 to %struct.sockaddr_in*
store %struct.sockaddr_in* %36, %struct.sockaddr_in** %10
%37 = load i32, i32* @AF_INET
%38 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 0
store i32 %37, i32* %39
store i32 16, i32* %9
%40 = load i8*, i8** %6
%41 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %41, i32 0, i32 1
store i8* %40, i8** %42
br label %52
43: ; preds = %27
%44 = bitcast %struct.sockaddr_storage* %8 to i8*
%45 = bitcast i8* %44 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %45, %struct.sockaddr_in6** %11
%46 = load i32, i32* @AF_INET6
%47 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%48 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %47, i32 0, i32 1
store i32 %46, i32* %48
store i32 16, i32* %9
%49 = load i8*, i8** %6
%50 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%51 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %50, i32 0, i32 0
store i8* %49, i8** %51
br label %52
52: ; preds = %43, %34
%53 = load i32, i32* %7
%54 = bitcast %struct.sockaddr_storage* %8 to i8*
%55 = load i32, i32* %9
%56 = call i32 @bind(i32 %53, i8* %54, i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %60
60: ; preds = %58, %52
%61 = load i32, i32* %5
%62 = load i32, i32* @SOCK_DGRAM
%63 = icmp ne i32 %61, %62
br i1 %63, label %64, label %71
64: ; preds = %60
%65 = load i32, i32* %7
%66 = call i32 @listen(i32 %65, i32 5)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %70
70: ; preds = %68, %64
br label %71
71: ; preds = %70, %60
%72 = load i32, i32* %7
ret i32 %72
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@PF_INET6 = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET6 = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@SOCK_DGRAM = global i32 0
@.str.2 = [7 x i8] c"listen\00"
define i32 @cr(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.sockaddr_storage
%5 = bitcast %struct.sockaddr_storage* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = icmp eq i32 %6, %0
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @PF_INET6
%10 = select i1 %7, i32 %8, i32 %9
%11 = tail call i32 @socket(i32 %10, i32 %1, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %3
%16 = tail call i8* @htons(i8* %2)
%17 = call i32 @memset(%struct.sockaddr_storage* %4, i32 0, i32 4)
%18 = load i32, i32* @AF_INET
%19 = icmp eq i32 %18, %0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
store i32 %0, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 2
%23 = bitcast %struct.sockaddr_storage* %22 to i8**
store i8* %16, i8** %23
br label %28
24: ; preds = %15
%25 = load i32, i32* @AF_INET6
%26 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 2, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_storage* %4 to i8**
store i8* %16, i8** %27
br label %28
28: ; preds = %24, %20
%29 = call i32 @bind(i32 %11, i8* %5, i32 16)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = load i32, i32* @SOCK_DGRAM
%35 = icmp eq i32 %34, %1
br i1 %35, label %41, label %36
36: ; preds = %33
%37 = call i32 @listen(i32 %11, i32 5)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %36, %39, %33
ret i32 %11
}