#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_http_listener() {
  int fd = socket(PF_INET, SOCK_STREAM, 0);
  if (fd >= 0) {
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080);
    addr.sin_addr.s_addr = INADDR_ANY;
    if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
      close(fd);
      return -1;
    }
    if (listen(fd, 128)) {
      close(fd);
      return -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_http_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1202 <open_http_listener+0xb2>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
je     11d2 <open_http_listener+0x82>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <open_http_listener+0xb8>
mov    -0x8(%rbp),%edi
mov    $0x80,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
je     11fd <open_http_listener+0xad>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <open_http_listener+0xb8>
jmp    1202 <open_http_listener+0xb2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_http_listener>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11cc <open_http_listener+0x7c>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
jne    11c0 <open_http_listener+0x70>
mov    %ebx,%edi
mov    $0x80,%esi
call   1050 <listen@plt>
test   %rax,%rax
je     11cc <open_http_listener+0x7c>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define i32 @open_http_listener() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %32
9: ; preds = %0
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @htons(i32 8080)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* %2
%18 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%19 = call i64 @bind(i32 %17, %struct.sockaddr* %18, i32 12)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %9
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %1
br label %34
24: ; preds = %9
%25 = load i32, i32* %2
%26 = call i64 @listen(i32 %25, i32 128)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %1
br label %34
31: ; preds = %24
br label %32
32: ; preds = %31, %0
%33 = load i32, i32* %2
store i32 %33, i32* %1
br label %34
34: ; preds = %32, %28, %21
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define i32 @open_http_listener() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @PF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %25
6: ; preds = %0
%7 = bitcast %struct.sockaddr_in* %1 to i8*
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 8080)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%15 = call i64 @bind(i32 %4, %struct.sockaddr* %14, i32 12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %6
%18 = call i32 @close(i32 %4)
br label %25
19: ; preds = %6
%20 = call i64 @listen(i32 %4, i32 128)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @close(i32 %4)
br label %25
24: ; preds = %19
br label %25
25: ; preds = %0, %24, %17, %22
%26 = phi i32 [ -1, %22 ], [ -1, %17 ], [ %4, %24 ], [ %4, %0 ]
ret i32 %26
}