#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 {
  int /*<<< orphan*/ sa_family;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ reuse;

/* Variables and functions */
int SOCK_SEQPACKET;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ ltr_int_my_perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int prepare_server_comm(int type, const struct sockaddr *addr,
                        socklen_t addr_len, int qlen) {
  int fd;
  int reuse = 1;
  fd = socket(addr->sa_family, type, 0);
  if (fd < 0) {
    ltr_int_my_perror("socket:");
    return -1;
  }
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) != 0) {
    ltr_int_my_perror("setsockopt:");
    goto error_label;
  }
  if (bind(fd, addr, addr_len) != 0) {
    ltr_int_my_perror("bind:");
    goto error_label;
  }
  if ((type == SOCK_STREAM) || (type == SOCK_SEQPACKET)) {
    if (listen(fd, qlen) < 0) {
      ltr_int_my_perror("listen:");
      goto error_label;
    }
  }
  return fd;
error_label:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<prepare_server_comm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b1 <prepare_server_comm+0x51>
lea    0xe60(%rip),%rdi
call   1060 <ltr_int_my_perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <prepare_server_comm+0x123>
mov    -0x1c(%rbp),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11f0 <prepare_server_comm+0x90>
lea    0xe22(%rip),%rdi
call   1060 <ltr_int_my_perror@plt>
jmp    1274 <prepare_server_comm+0x114>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     121a <prepare_server_comm+0xba>
lea    0xe04(%rip),%rdi
call   1060 <ltr_int_my_perror@plt>
jmp    1274 <prepare_server_comm+0x114>
mov    -0x8(%rbp),%eax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%eax
je     123e <prepare_server_comm+0xde>
mov    -0x8(%rbp),%eax
mov    0x2d92(%rip),%rcx
cmp    (%rcx),%eax
jne    1269 <prepare_server_comm+0x109>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    1264 <prepare_server_comm+0x104>
lea    0xdc0(%rip),%rdi
call   1060 <ltr_int_my_perror@plt>
jmp    1274 <prepare_server_comm+0x114>
jmp    1269 <prepare_server_comm+0x109>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <prepare_server_comm+0x123>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_server_comm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%r12d
movl   $0x1,0xc(%rsp)
mov    (%rsi),%edi
mov    %r12d,%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11c4 <prepare_server_comm+0x64>
mov    %eax,%ebx
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11d2 <prepare_server_comm+0x72>
lea    0xe46(%rip),%rdi
jmp    11eb <prepare_server_comm+0x8b>
lea    0xe35(%rip),%rdi
call   1060 <ltr_int_my_perror@plt>
jmp    11f7 <prepare_server_comm+0x97>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     120b <prepare_server_comm+0xab>
lea    0xe29(%rip),%rdi
call   1060 <ltr_int_my_perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc6(%rip),%rax
cmp    %r12d,(%rax)
je     1223 <prepare_server_comm+0xc3>
mov    0x2daa(%rip),%rax
cmp    %r12d,(%rax)
jne    11fc <prepare_server_comm+0x9c>
mov    %ebx,%edi
mov    %r14d,%esi
call   1050 <listen@plt>
test   %rax,%rax
jns    11fc <prepare_server_comm+0x9c>
lea    0xde1(%rip),%rdi
jmp    11eb <prepare_server_comm+0x8b>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"socket:\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [12 x i8] c"setsockopt:\00"
@.str.2 = [6 x i8] c"bind:\00"
@SOCK_STREAM = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.3 = [8 x i8] c"listen:\00"
define i32 @prepare_server_comm(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store %struct.sockaddr* %1, %struct.sockaddr** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 1, i32* %11
%12 = load %struct.sockaddr*, %struct.sockaddr** %7
%13 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* %6
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %58
21: ; preds = %4
%22 = load i32, i32* %10
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i64 @setsockopt(i32 %22, i32 %23, i32 %24, i32* %11, i32 4)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %55
29: ; preds = %21
%30 = load i32, i32* %10
%31 = load %struct.sockaddr*, %struct.sockaddr** %7
%32 = load i32, i32* %8
%33 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %55
37: ; preds = %29
%38 = load i32, i32* %6
%39 = load i32, i32* @SOCK_STREAM
%40 = icmp eq i32 %38, %39
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = load i32, i32* %6
%43 = load i32, i32* @SOCK_SEQPACKET
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %53
45: ; preds = %41, %37
%46 = load i32, i32* %10
%47 = load i32, i32* %9
%48 = call i64 @listen(i32 %46, i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %55
52: ; preds = %45
br label %53
53: ; preds = %52, %41
%54 = load i32, i32* %10
store i32 %54, i32* %5
br label %58
55: ; preds = %50, %35, %27
%56 = load i32, i32* %10
%57 = call i32 @close(i32 %56)
store i32 -1, i32* %5
br label %58
58: ; preds = %55, %53, %19
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"socket:\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [12 x i8] c"setsockopt:\00"
@.str.2 = [6 x i8] c"bind:\00"
@SOCK_STREAM = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.3 = [8 x i8] c"listen:\00"
define i32 @prepare_server_comm(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%7 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @socket(i32 %8, i32 %0, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = tail call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %34
13: ; preds = %4
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i64 @setsockopt(i32 %9, i32 %14, i32 %15, i32* %5, i32 4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = call i64 @bind(i32 %9, %struct.sockaddr* %1, i32 %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %18
%22 = load i32, i32* @SOCK_STREAM
%23 = icmp eq i32 %22, %0
%24 = load i32, i32* @SOCK_SEQPACKET
%25 = icmp eq i32 %24, %0
%26 = select i1 %23, i1 true, i1 %25
br i1 %26, label %27, label %34
27: ; preds = %21
%28 = call i64 @listen(i32 %9, i32 %3)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27, %18, %13
%31 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %18 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %27 ]
%32 = call i32 @ltr_int_my_perror(i8* %31)
%33 = call i32 @close(i32 %9)
br label %34
34: ; preds = %27, %21, %30, %11
%35 = phi i32 [ -1, %11 ], [ -1, %30 ], [ %9, %21 ], [ %9, %27 ]
ret i32 %35
}