#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 szl_interp {
  int dummy;
};
struct addrinfo {
  int ai_flags;
  int ai_socktype;
  scalar_t__ ai_addrlen;
  int /*<<< orphan*/ *ai_addr;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ *ai_next;
  int /*<<< orphan*/ *ai_canonname;
};
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int AI_ADDRCONFIG;
int AI_PASSIVE;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, scalar_t__);
int /*<<< orphan*/ szl_set_last_strerror(struct szl_interp *, int);
int szl_socket_new_server(struct szl_interp *interp, const char *host,
                          const char *service, const int type) {
  struct addrinfo hints, *res;
  int fd, one = 1, err;
  hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = type;
  hints.ai_protocol = 0;
  hints.ai_addrlen = 0;
  hints.ai_addr = NULL;
  hints.ai_canonname = NULL;
  hints.ai_next = NULL;
  if (getaddrinfo(host, service, &hints, &res) != 0)
    return -1;
  fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (fd < 0) {
    err = errno;
    freeaddrinfo(res);
    szl_set_last_strerror(interp, err);
    return -1;
  }
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
    err = errno;
    close(fd);
    freeaddrinfo(res);
    szl_set_last_strerror(interp, err);
    return -1;
  }
  if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) {
    err = errno;
    close(fd);
    freeaddrinfo(res);
    szl_set_last_strerror(interp, err);
    return -1;
  }
  freeaddrinfo(res);
  return fd;
}


x86 assembly (clang, -O0):
<szl_socket_new_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x1,-0x70(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    0x2e0f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x60(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x5c(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     120b <szl_socket_new_server+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <szl_socket_new_server+0x1b9>
mov    -0x68(%rbp),%rax
mov    0x20(%rax),%edi
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x68(%rbp),%rax
mov    0x18(%rax),%rdx
call   1090 <socket@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jge    1260 <szl_socket_new_server+0xf0>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x68(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x10(%rbp),%rdi
mov    -0x74(%rbp),%esi
call   1050 <szl_set_last_strerror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <szl_socket_new_server+0x1b9>
mov    -0x6c(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
lea    -0x70(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12c3 <szl_socket_new_server+0x153>
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x6c(%rbp),%edi
call   1040 <close@plt>
mov    -0x68(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x10(%rbp),%rdi
mov    -0x74(%rbp),%esi
call   1050 <szl_set_last_strerror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <szl_socket_new_server+0x1b9>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rdx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    131a <szl_socket_new_server+0x1aa>
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x6c(%rbp),%edi
call   1040 <close@plt>
mov    -0x68(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x10(%rbp),%rdi
mov    -0x74(%rbp),%esi
call   1050 <szl_set_last_strerror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <szl_socket_new_server+0x1b9>
mov    -0x68(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<szl_socket_new_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%r14
movl   $0x1,0xc(%rsp)
mov    0x2e54(%rip),%rax
mov    0x2e1d(%rip),%rdi
mov    (%rdi),%edi
or     (%rax),%edi
mov    %edi,0x10(%rsp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %ecx,0x14(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movq   $0x0,0x28(%rsp)
movups %xmm0,0x38(%rsp)
lea    0x10(%rsp),%rax
mov    %rsp,%rcx
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %rax,%rdx
call   1070 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11ef <szl_socket_new_server+0x7f>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rax
mov    0x20(%rax),%edi
mov    0x4(%rax),%esi
mov    0x18(%rax),%rdx
call   1090 <socket@plt>
test   %eax,%eax
js     1256 <szl_socket_new_server+0xe6>
mov    %eax,%ebp
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1277 <szl_socket_new_server+0x107>
mov    (%rsp),%rax
mov    0x8(%rax),%rdx
mov    0x10(%rax),%rsi
mov    %ebp,%edi
call   1060 <bind@plt>
test   %rax,%rax
js     1277 <szl_socket_new_server+0x107>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%ebx
jmp    11e2 <szl_socket_new_server+0x72>
mov    0x2d6b(%rip),%rax
mov    (%rax),%ebp
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <szl_set_last_strerror@plt>
jmp    11e2 <szl_socket_new_server+0x72>
mov    0x2d4a(%rip),%rax
mov    (%rax),%r15d
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1050 <szl_set_last_strerror@plt>
jmp    11e2 <szl_socket_new_server+0x72>

LLVM IR (-O0):
%struct.szl_interp = type { i32 }
%struct.addrinfo = type { i32, i32, i64, i32*, i64, i32, i32*, i32* }
@AI_PASSIVE = global i32 0
@AI_ADDRCONFIG = global i32 0
@AF_UNSPEC = global i32 0
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @szl_socket_new_server(%struct.szl_interp* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.szl_interp*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.addrinfo
%11 = alloca %struct.addrinfo*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store %struct.szl_interp* %0, %struct.szl_interp** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 1, i32* %13
%15 = load i32, i32* @AI_PASSIVE
%16 = load i32, i32* @AI_ADDRCONFIG
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @AF_UNSPEC
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 5
store i32 %19, i32* %20
%21 = load i32, i32* %9
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 4
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 2
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 3
store i32* null, i32** %25
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 7
store i32* null, i32** %26
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 6
store i32* null, i32** %27
%28 = load i8*, i8** %7
%29 = load i8*, i8** %8
%30 = call i64 @getaddrinfo(i8* %28, i8* %29, %struct.addrinfo* %10, %struct.addrinfo** %11)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %4
store i32 -1, i32* %5
br label %91
33: ; preds = %4
%34 = load %struct.addrinfo*, %struct.addrinfo** %11
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 5
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %11
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %11
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 4
%42 = load i64, i64* %41
%43 = call i32 @socket(i32 %36, i32 %39, i64 %42)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %33
%47 = load i32, i32* @errno
store i32 %47, i32* %14
%48 = load %struct.addrinfo*, %struct.addrinfo** %11
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
%50 = load %struct.szl_interp*, %struct.szl_interp** %6
%51 = load i32, i32* %14
%52 = call i32 @szl_set_last_strerror(%struct.szl_interp* %50, i32 %51)
store i32 -1, i32* %5
br label %91
53: ; preds = %33
%54 = load i32, i32* %12
%55 = load i32, i32* @SOL_SOCKET
%56 = load i32, i32* @SO_REUSEADDR
%57 = call i64 @setsockopt(i32 %54, i32 %55, i32 %56, i32* %13, i32 4)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %68
59: ; preds = %53
%60 = load i32, i32* @errno
store i32 %60, i32* %14
%61 = load i32, i32* %12
%62 = call i32 @close(i32 %61)
%63 = load %struct.addrinfo*, %struct.addrinfo** %11
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %63)
%65 = load %struct.szl_interp*, %struct.szl_interp** %6
%66 = load i32, i32* %14
%67 = call i32 @szl_set_last_strerror(%struct.szl_interp* %65, i32 %66)
store i32 -1, i32* %5
br label %91
68: ; preds = %53
%69 = load i32, i32* %12
%70 = load %struct.addrinfo*, %struct.addrinfo** %11
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 3
%72 = load i32*, i32** %71
%73 = load %struct.addrinfo*, %struct.addrinfo** %11
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 2
%75 = load i64, i64* %74
%76 = call i64 @bind(i32 %69, i32* %72, i64 %75)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %87
78: ; preds = %68
%79 = load i32, i32* @errno
store i32 %79, i32* %14
%80 = load i32, i32* %12
%81 = call i32 @close(i32 %80)
%82 = load %struct.addrinfo*, %struct.addrinfo** %11
%83 = call i32 @freeaddrinfo(%struct.addrinfo* %82)
%84 = load %struct.szl_interp*, %struct.szl_interp** %6
%85 = load i32, i32* %14
%86 = call i32 @szl_set_last_strerror(%struct.szl_interp* %84, i32 %85)
store i32 -1, i32* %5
br label %91
87: ; preds = %68
%88 = load %struct.addrinfo*, %struct.addrinfo** %11
%89 = call i32 @freeaddrinfo(%struct.addrinfo* %88)
%90 = load i32, i32* %12
store i32 %90, i32* %5
br label %91
91: ; preds = %87, %78, %59, %46, %32
%92 = load i32, i32* %5
ret i32 %92
}

LLVM IR (-O2):
%struct.szl_interp = type { i32 }
%struct.addrinfo = type { i32, i32, i64, i32*, i64, i32, i32*, i32* }
@AI_PASSIVE = global i32 0
@AI_ADDRCONFIG = global i32 0
@AF_UNSPEC = global i32 0
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @szl_socket_new_server(%struct.szl_interp* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca i32
%8 = bitcast %struct.addrinfo* %5 to i8*
%9 = bitcast %struct.addrinfo** %6 to i8*
%10 = bitcast i32* %7 to i8*
store i32 1, i32* %7
%11 = load i32, i32* @AI_PASSIVE
%12 = load i32, i32* @AI_ADDRCONFIG
%13 = or i32 %12, %11
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @AF_UNSPEC
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 5
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
store i32 %3, i32* %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 2
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 6
%20 = bitcast i64* %18 to i8*
%21 = bitcast i32** %19 to i8*
%22 = call i64 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %5, %struct.addrinfo** %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %67
24: ; preds = %4
%25 = load %struct.addrinfo*, %struct.addrinfo** %6
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%31 = load i64, i64* %30
%32 = call i32 @socket(i32 %27, i32 %29, i64 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %24
%35 = load i32, i32* @errno
%36 = load %struct.addrinfo*, %struct.addrinfo** %6
%37 = call i32 @freeaddrinfo(%struct.addrinfo* %36)
%38 = call i32 @szl_set_last_strerror(%struct.szl_interp* %0, i32 %35)
br label %67
39: ; preds = %24
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_REUSEADDR
%42 = call i64 @setsockopt(i32 %32, i32 %40, i32 %41, i32* %7, i32 4)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %39
%45 = load i32, i32* @errno
%46 = call i32 @close(i32 %32)
%47 = load %struct.addrinfo*, %struct.addrinfo** %6
%48 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
%49 = call i32 @szl_set_last_strerror(%struct.szl_interp* %0, i32 %45)
br label %67
50: ; preds = %39
%51 = load %struct.addrinfo*, %struct.addrinfo** %6
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i64 0, i32 3
%53 = load i32*, i32** %52
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i64 0, i32 2
%55 = load i64, i64* %54
%56 = call i64 @bind(i32 %32, i32* %53, i64 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %50
%59 = load i32, i32* @errno
%60 = call i32 @close(i32 %32)
%61 = load %struct.addrinfo*, %struct.addrinfo** %6
%62 = call i32 @freeaddrinfo(%struct.addrinfo* %61)
%63 = call i32 @szl_set_last_strerror(%struct.szl_interp* %0, i32 %59)
br label %67
64: ; preds = %50
%65 = load %struct.addrinfo*, %struct.addrinfo** %6
%66 = call i32 @freeaddrinfo(%struct.addrinfo* %65)
br label %67
67: ; preds = %4, %64, %58, %44, %34
%68 = phi i32 [ -1, %34 ], [ -1, %44 ], [ -1, %58 ], [ %32, %64 ], [ -1, %4 ]
ret i32 %68
}