#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;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int init_sock(int port) {
  struct addrinfo hints, *res;
  char pbuf[8];
  int sock;
  sprintf(pbuf, "%d", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  getaddrinfo(NULL, pbuf, &hints, &res);
  sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (sock <= 0) {
    freeaddrinfo(res);
    perror("socket");
    return -1;
  }
  if (bind(sock, res->ai_addr, res->ai_addrlen)) {
    freeaddrinfo(res);
    perror("bind");
    return -1;
  }
  listen(sock, 10);
  freeaddrinfo(res);
  return sock;
}


x86 assembly (clang, -O0):
<init_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe67(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     122f <init_sock+0xaf>
mov    -0x28(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
lea    0xde5(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <init_sock+0x10b>
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
je     126f <init_sock+0xef>
mov    -0x28(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
lea    0xdac(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <init_sock+0x10b>
mov    -0x34(%rbp),%edi
mov    $0xa,%esi
call   1040 <listen@plt>
mov    -0x28(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_sock>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%edx
lea    0xe70(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
call   1070 <sprintf@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <getaddrinfo@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10a0 <socket@plt>
mov    (%rsp),%rdi
test   %eax,%eax
jle    1234 <init_sock+0xb4>
mov    %eax,%ebx
mov    (%rdi),%edx
mov    0x4(%rdi),%esi
mov    %eax,%edi
call   1050 <bind@plt>
test   %rax,%rax
jne    1242 <init_sock+0xc2>
mov    %ebx,%edi
mov    $0xa,%esi
call   1040 <listen@plt>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
call   1090 <freeaddrinfo@plt>
lea    0xdc3(%rip),%rdi
jmp    1252 <init_sock+0xd2>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
lea    0xdb8(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    122a <init_sock+0xaa>

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

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