#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(struct addrinfo *);
char *gai_strerror(int);
scalar_t__ 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 setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sh_server(char *server_port) {
  int yes = 1;
  struct addrinfo hints, *server;
  int sockfd, rc;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if (rc = getaddrinfo(NULL, server_port, &hints, &server) != 0) {
    perror(gai_strerror(rc));
    exit(-1);
  }
  sockfd = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
  if (sockfd == -1) {
    perror("ERROR opening socket");
    exit(-1);
  }
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    perror("setsockopt");
    exit(-1);
  }
  rc = bind(sockfd, server->ai_addr, server->ai_addrlen);
  if (rc == -1) {
    perror("ERROR on connect");
    close(sockfd);
    exit(-1);
  }
  free(server);
  listen(sockfd, 10);
  return sockfd;
}


x86 assembly (clang, -O0):
<sh_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x38(%rbp)
test   $0x1,%al
jne    122b <sh_server+0x7b>
jmp    1245 <sh_server+0x95>
mov    -0x38(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1282 <sh_server+0xd2>
lea    0xd8d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x34(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    0x2d2b(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12c5 <sh_server+0x115>
lea    0xd5f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1090 <bind@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1305 <sh_server+0x155>
lea    0xd32(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sh_server>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10c0 <getaddrinfo@plt>
test   %rax,%rax
jne    1288 <sh_server+0xd8>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1297 <sh_server+0xe7>
mov    %eax,%ebx
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12a0 <sh_server+0xf0>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     12b6 <sh_server+0x106>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rdi
jmp    12a7 <sh_server+0xf7>
lea    0xd62(%rip),%rdi
jmp    12a7 <sh_server+0xf7>
lea    0xd6e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xd63(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

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

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