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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ AI_ADDRCONFIG;
scalar_t__ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int start_server(const char *port, struct addrinfo **res, int local) {
  const char *hostname = local ? "localhost" : 0;
  const char *portname = port;
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_protocol = 0;
  hints.ai_flags = AI_ADDRCONFIG || AI_PASSIVE;
  if (getaddrinfo(hostname, portname, &hints, res) != 0) {
    perror("Could not resolve local socket address");
    exit(1);
  }
  int sock =
      socket((*res)->ai_family, (*res)->ai_socktype, (*res)->ai_protocol);
  if (sock < 0) {
    perror("Could not open socket");
    exit(1);
  }
  if (bind(sock, (*res)->ai_addr, (*res)->ai_addrlen) < 0) {
    perror("Could not bind socket");
    exit(1);
  }
  return sock;
}


x86 assembly (clang, -O0):
<start_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%edx
xor    %eax,%eax
lea    0xe81(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    $0x1,%al
mov    0x2df5(%rip),%rcx
cmpq   $0x0,(%rcx)
mov    %al,-0x4d(%rbp)
jne    11e9 <start_server+0x89>
mov    0x2e09(%rip),%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x4d(%rbp)
mov    -0x4d(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    -0x48(%rbp),%rdx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1229 <start_server+0xc9>
lea    0xdf0(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x1c(%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x18(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x10(%rax),%rdx
call   1080 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1270 <start_server+0x110>
lea    0xdd0(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x8(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
jge    12ac <start_server+0x14c>
lea    0xdaa(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_server>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebx,%ebx
test   %edx,%edx
lea    0xe84(%rip),%rax
cmovne %rax,%rbx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2e20(%rip),%rcx
xor    %edx,%edx
or     (%rcx),%rax
setne  %dl
mov    %edx,0x8(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
jne    1225 <start_server+0xc5>
mov    (%r15),%rax
mov    0x1c(%rax),%edi
mov    0x18(%rax),%esi
mov    0x10(%rax),%rdx
call   1080 <socket@plt>
test   %eax,%eax
js     122e <start_server+0xce>
mov    %eax,%ebx
mov    (%r15),%rax
mov    0x4(%rax),%edx
mov    0x8(%rax),%esi
mov    %ebx,%edi
call   1040 <bind@plt>
test   %rax,%rax
js     1237 <start_server+0xd7>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdde(%rip),%rdi
jmp    123e <start_server+0xde>
lea    0xdfc(%rip),%rdi
jmp    123e <start_server+0xde>
lea    0xe09(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i64, i32, i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AI_ADDRCONFIG = global i64 0
@AI_PASSIVE = global i64 0
@.str.1 = [39 x i8] c"Could not resolve local socket address\00"
@.str.2 = [22 x i8] c"Could not open socket\00"
@.str.3 = [22 x i8] c"Could not bind socket\00"
define i32 @start_server(i8* %0, %struct.addrinfo** %1, i32 %2) {
%4 = alloca %struct.addrinfo
%5 = icmp eq i32 %2, 0
%6 = select i1 %5, i8* null, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0)
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_DGRAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i64 0, i64* %13
%14 = load i64, i64* @AI_ADDRCONFIG
%15 = icmp ne i64 %14, 0
%16 = load i64, i64* @AI_PASSIVE
%17 = icmp ne i64 %16, 0
%18 = select i1 %15, i1 true, i1 %17
%19 = zext i1 %18 to i32
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %19, i32* %20
%21 = call i64 @getaddrinfo(i8* %6, i8* %0, %struct.addrinfo* %4, %struct.addrinfo** %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %3
%24 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %3
%27 = load %struct.addrinfo*, %struct.addrinfo** %1
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 5
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 4
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 3
%33 = load i64, i64* %32
%34 = call i32 @socket(i32 %29, i32 %31, i64 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %26
%40 = load %struct.addrinfo*, %struct.addrinfo** %1
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 2
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 1
%44 = load i32, i32* %43
%45 = call i64 @bind(i32 %34, i32 %42, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %39
ret i32 %34
}