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

/* 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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int lp_get_socket(int pt) {
  struct addrinfo hints, *servinfo;
  int sock_fd, status;
  char port;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  hints.ai_flags = AI_PASSIVE;
  sprintf(&port, "%d", pt);
  if ((status = getaddrinfo(NULL, &port, &hints, &servinfo)) != 0) {
    fprintf(stderr, "Address fetch error %s", gai_strerror(status));
    return -1;
  }
  sock_fd =
      socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
  if (sock_fd <= 0) {
    fprintf(stderr, "Fatal: Server thread error");
    return -1;
  }
  if (bind(sock_fd, servinfo->ai_addr, servinfo->ai_addrlen) != 0) {
    fprintf(stderr, "Fatal: Socket binding error");
    return -1;
  }
  freeaddrinfo(servinfo);
  return sock_fd;
}


x86 assembly (clang, -O0):
<lp_get_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edx
lea    -0x39(%rbp),%rdi
lea    0xe2b(%rip),%rsi
call   1070 <sprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    -0x39(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
je     122f <lp_get_socket+0xaf>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <lp_get_socket+0x14b>
mov    -0x30(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     127a <lp_get_socket+0xfa>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <lp_get_socket+0x14b>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
je     12bc <lp_get_socket+0x13c>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <lp_get_socket+0x14b>
mov    -0x30(%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):
<lp_get_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0xe2b(%rip),%rsi
lea    0x7(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     1224 <lp_get_socket+0xa4>
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdf2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    128e <lp_get_socket+0x10e>
mov    0x8(%rsp),%rax
mov    0x14(%rax),%edi
mov    0x10(%rax),%esi
mov    0x8(%rax),%rdx
call   10a0 <socket@plt>
test   %eax,%eax
jle    1266 <lp_get_socket+0xe6>
mov    %eax,%ebx
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1060 <bind@plt>
test   %rax,%rax
je     1284 <lp_get_socket+0x104>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
jmp    1276 <lp_get_socket+0xf6>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    128e <lp_get_socket+0x10e>
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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