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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                       struct addrinfo **);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int setup_server(char *port, int backlog) {
  int lis_sock;
  struct addrinfo hints, *scan, *res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if (getaddrinfo(NULL, port, &hints, &res) != 0) {
    fprintf(stderr, "Failed to lookup the local address");
    return -1;
  }
  for (scan = res; scan != NULL; scan = scan->ai_next) {
    if ((lis_sock = socket(scan->ai_family, scan->ai_socktype,
                           scan->ai_protocol)) < 0) {
      fprintf(stderr, "Failed to setup server\n");
      return -4;
    }
    if (bind(lis_sock, scan->ai_addr, scan->ai_addrlen) != 0) {
      fprintf(stderr, "Failed to setup server\n");
      return -4;
    }
    if (listen(lis_sock, backlog) != 0) {
      fprintf(stderr, "Failed to setup server\n");
      return -4;
    }
    return lis_sock;
  }
  fprintf(stderr, "No suitable address\n");
  return -4;
}


x86 assembly (clang, -O0):
<setup_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11e3 <setup_server+0x83>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <setup_server+0x17a>
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12be <setup_server+0x15e>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    123d <setup_server+0xdd>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12da <setup_server+0x17a>
mov    -0x18(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
je     127d <setup_server+0x11d>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12da <setup_server+0x17a>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
je     12b3 <setup_server+0x153>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12da <setup_server+0x17a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12da <setup_server+0x17a>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
je     11f2 <setup_server+0x92>
mov    $0xffffffff,%r15d
lea    0xe31(%rip),%r12
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
call   1040 <fprintf@plt>
mov    %r15d,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rbx
mov    $0xfffffffc,%r15d
test   %rbx,%rbx
je     123e <setup_server+0xde>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1080 <socket@plt>
lea    0xe0d(%rip),%r12
test   %eax,%eax
js     11cf <setup_server+0x6f>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1060 <bind@plt>
test   %rax,%rax
jne    11cf <setup_server+0x6f>
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <listen@plt>
test   %rax,%rax
jne    11cf <setup_server+0x6f>
jmp    11e3 <setup_server+0x83>
lea    0xdf6(%rip),%r12
jmp    11cf <setup_server+0x6f>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"Failed to lookup the local address\00"
@.str.1 = [24 x i8] c"Failed to setup server\0A\00"
@.str.2 = [21 x i8] c"No suitable address\0A\00"
define i32 @setup_server(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 40)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 6
store i32 %15, i32* %16
%17 = load i8*, i8** %4
%18 = call i64 @getaddrinfo(i32* null, i8* %17, %struct.addrinfo* %7, %struct.addrinfo** %9)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
23: ; preds = %2
%24 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %24, %struct.addrinfo** %8
br label %25
25: ; preds = %66, %23
%26 = load %struct.addrinfo*, %struct.addrinfo** %8
%27 = icmp ne %struct.addrinfo* %26, null
br i1 %27, label %28, label %70
28: ; preds = %25
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 4
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 3
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %8
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = call i32 @socket(i32 %31, i32 %34, i32 %37)
store i32 %38, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %28
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -4, i32* %3
br label %73
43: ; preds = %28
%44 = load i32, i32* %6
%45 = load %struct.addrinfo*, %struct.addrinfo** %8
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %8
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i64 @bind(i32 %44, i32 %47, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %43
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -4, i32* %3
br label %73
56: ; preds = %43
%57 = load i32, i32* %6
%58 = load i32, i32* %5
%59 = call i64 @listen(i32 %57, i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -4, i32* %3
br label %73
64: ; preds = %56
%65 = load i32, i32* %6
store i32 %65, i32* %3
br label %73
66: ; No predecessors!
%67 = load %struct.addrinfo*, %struct.addrinfo** %8
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %67, i32 0, i32 5
%69 = load %struct.addrinfo*, %struct.addrinfo** %68
store %struct.addrinfo* %69, %struct.addrinfo** %8
br label %25
70: ; preds = %25
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -4, i32* %3
br label %73
73: ; preds = %70, %64, %61, %53, %40, %20
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"Failed to lookup the local address\00"
@.str.1 = [24 x i8] c"Failed to setup server\0A\00"
@.str.2 = [21 x i8] c"No suitable address\0A\00"
define i32 @setup_server(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %12, i32* %13
%14 = call i64 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %38
16: ; preds = %2
%17 = load %struct.addrinfo*, %struct.addrinfo** %4
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %38, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 4
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 3
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 2
%25 = load i32, i32* %24
%26 = call i32 @socket(i32 %21, i32 %23, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %19
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @bind(i32 %26, i32 %30, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = call i64 @listen(i32 %26, i32 %1)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %16, %35, %28, %19, %2
%39 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %19 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %28 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %35 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), %16 ]
%40 = phi i32 [ -1, %2 ], [ -4, %19 ], [ -4, %28 ], [ -4, %35 ], [ -4, %16 ]
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* %39)
br label %43
43: ; preds = %38, %35
%44 = phi i32 [ %26, %35 ], [ %40, %38 ]
ret i32 %44
}