#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ local_server_addr;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ construct_sockaddr_in(struct sockaddr_in *,
                                         int /*<<< orphan*/,
                                         int /*<<< orphan*/);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_local_server_socket() {
  int local_server_socket;
  struct sockaddr_in local_server_addr;
  unsigned short local_server_port = 80;
  if ((local_server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    die("socket() failed");
  }
  construct_sockaddr_in(&local_server_addr, htonl(INADDR_ANY),
                        htons(local_server_port));
  if (bind(local_server_socket, (struct sockaddr *)&local_server_addr,
           sizeof(local_server_addr)) < 0) {
    die("bind() failed");
  }
  if (listen(local_server_socket, 5) < 0)
    die("listen() failed");
  return local_server_socket;
}


x86 assembly (clang, -O0):
<init_local_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movw   $0x50,-0xa(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11b6 <init_local_server_socket+0x46>
lea    0xe4f(%rip),%rdi
call   1080 <die@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x10(%rbp)
movzwl -0xa(%rbp),%edi
call   1040 <htons@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    -0x8(%rbp),%rdi
call   1030 <construct_sockaddr_in@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1205 <init_local_server_socket+0x95>
lea    0xe10(%rip),%rdi
call   1080 <die@plt>
mov    -0x4(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1228 <init_local_server_socket+0xb8>
lea    0xdfb(%rip),%rdi
call   1080 <die@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_local_server_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11aa <init_local_server_socket+0x3a>
lea    0xe5b(%rip),%rdi
call   1080 <die@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,%ebp
mov    $0x50,%edi
call   1040 <htons@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <construct_sockaddr_in@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    11f5 <init_local_server_socket+0x85>
lea    0xe20(%rip),%rdi
call   1080 <die@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    1212 <init_local_server_socket+0xa2>
lea    0xe11(%rip),%rdi
call   1080 <die@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"bind() failed\00"
@.str.2 = [16 x i8] c"listen() failed\00"
define i32 @init_local_server_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i16
store i16 80, i16* %3
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @IPPROTO_TCP
%7 = call i32 @socket(i32 %4, i32 %5, i32 %6)
store i32 %7, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
%14 = load i16, i16* %3
%15 = call i32 @htons(i16 zeroext %14)
%16 = call i32 @construct_sockaddr_in(%struct.sockaddr_in* %2, i32 %13, i32 %15)
%17 = load i32, i32* %1
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i64 @bind(i32 %17, %struct.sockaddr* %18, i32 4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %11
%24 = load i32, i32* %1
%25 = call i64 @listen(i32 %24, i32 5)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"bind() failed\00"
@.str.2 = [16 x i8] c"listen() failed\00"
define i32 @init_local_server_socket() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* @INADDR_ANY
%12 = tail call i32 @htonl(i32 %11)
%13 = tail call i32 @htons(i16 zeroext 80)
%14 = call i32 @construct_sockaddr_in(%struct.sockaddr_in* %1, i32 %12, i32 %13)
%15 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%16 = call i64 @bind(i32 %6, %struct.sockaddr* %15, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %10
%21 = call i64 @listen(i32 %6, i32 5)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
ret i32 %6
}