#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 */
typedef int /*<<< orphan*/ yes;
struct addrinfo {
  scalar_t__ ai_family;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
};

/* Variables and functions */
scalar_t__ AF_INET6;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_V6ONLY;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ add_socket(int, int /*<<< orphan*/);
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *, int);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
void add_listen(struct addrinfo *res) {
  const int yes = 1;
  int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (sock == -1) {
    perror("socket");
    exit(1);
  }
  if (res->ai_family == AF_INET6) {
    if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1) {
      perror("setsockopt");
      exit(1);
    }
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
    perror("setsockopt");
    exit(1);
  }
  if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
    perror("bind");
    exit(1);
  }
  if (listen(sock, 1) == -1) {
    perror("listen");
    exit(1);
  }
  add_socket(sock, POLLIN);
}


x86 assembly (clang, -O0):
<add_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11c0 <add_listen+0x50>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%rax
jne    121f <add_listen+0xaf>
mov    -0x10(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    121a <add_listen+0xaa>
lea    0xdfc(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    121f <add_listen+0xaf>
mov    -0x10(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1262 <add_listen+0xf2>
lea    0xdb4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1297 <add_listen+0x127>
lea    0xd8a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c3 <add_listen+0x153>
lea    0xd63(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
call   1030 <add_socket@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_listen>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
movl   $0x1,0x4(%rsp)
mov    (%rdi),%rdi
mov    0x14(%rbx),%esi
mov    0x10(%rbx),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     123b <add_listen+0xcb>
mov    %eax,%ebp
mov    (%rbx),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    11cf <add_listen+0x5f>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1232 <add_listen+0xc2>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1232 <add_listen+0xc2>
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
mov    %ebp,%edi
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     1244 <add_listen+0xd4>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
je     124d <add_listen+0xdd>
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <add_socket@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdce(%rip),%rdi
jmp    1254 <add_listen+0xe4>
lea    0xdbe(%rip),%rdi
jmp    1254 <add_listen+0xe4>
lea    0xdc7(%rip),%rdi
jmp    1254 <add_listen+0xe4>
lea    0xdc3(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32 }
@.str = [7 x i8] c"socket\00"
@AF_INET6 = global i64 0
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@POLLIN = global i32 0
define void @add_listen(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i32
%4 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %2
store i32 1, i32* %3
%5 = load %struct.addrinfo*, %struct.addrinfo** %2
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 0
%7 = load i64, i64* %6
%8 = load %struct.addrinfo*, %struct.addrinfo** %2
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
%10 = load i32, i32* %9
%11 = load %struct.addrinfo*, %struct.addrinfo** %2
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 3
%13 = load i32, i32* %12
%14 = call i32 @socket(i64 %7, i32 %10, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = load %struct.addrinfo*, %struct.addrinfo** %2
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @AF_INET6
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %36
26: ; preds = %20
%27 = load i32, i32* %4
%28 = load i32, i32* @IPPROTO_IPV6
%29 = load i32, i32* @IPV6_V6ONLY
%30 = call i32 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %3, i32 4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %26
br label %36
36: ; preds = %35, %20
%37 = load i32, i32* %4
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_REUSEADDR
%40 = call i32 @setsockopt(i32 %37, i32 %38, i32 %39, i32* %3, i32 4)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
%46 = load i32, i32* %4
%47 = load %struct.addrinfo*, %struct.addrinfo** %2
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 2
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %2
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 1
%52 = load i32, i32* %51
%53 = call i32 @bind(i32 %46, i32 %49, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %45
%56 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %45
%59 = load i32, i32* %4
%60 = call i32 @listen(i32 %59, i32 1)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %58
%66 = load i32, i32* %4
%67 = load i32, i32* @POLLIN
%68 = call i32 @add_socket(i32 %66, i32 %67)
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32 }
@.str = [7 x i8] c"socket\00"
@AF_INET6 = global i64 0
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@POLLIN = global i32 0
define void @add_listen(%struct.addrinfo* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%4 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%5 = load i64, i64* %4
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 4
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 3
%9 = load i32, i32* %8
%10 = tail call i32 @socket(i64 %5, i32 %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i64, i64* %4
%17 = load i64, i64* @AF_INET6
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i32, i32* @IPPROTO_IPV6
%21 = load i32, i32* @IPV6_V6ONLY
%22 = call i32 @setsockopt(i32 %10, i32 %20, i32 %21, i32* %2, i32 4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19, %15
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = call i32 @setsockopt(i32 %10, i32 %28, i32 %29, i32* %2, i32 4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %27
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%39 = load i32, i32* %38
%40 = call i32 @bind(i32 %10, i32 %37, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %35
%46 = call i32 @listen(i32 %10, i32 1)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %45
%52 = load i32, i32* @POLLIN
%53 = call i32 @add_socket(i32 %10, i32 %52)
ret void
}