#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int snprintf(char *, int, char *, int);
scalar_t__ write(int, char *, int);
void enable_fastopen(void) {
  int fd = open("/proc/sys/net/ipv4/tcp_fastopen", 0);
  int rw_mask = 3;
  int val, size;
  char buf[16];
  if (fd < 0) {
    error(1, errno, "Unable to open tcp_fastopen sysctl");
  }
  if (read(fd, buf, sizeof(buf)) <= 0) {
    error(1, errno, "Unable to read tcp_fastopen sysctl");
  }
  val = atoi(buf);
  close(fd);
  if ((val & rw_mask) != rw_mask) {
    fd = open("/proc/sys/net/ipv4/tcp_fastopen", O_RDWR);
    if (fd < 0)
      error(1, errno, "Unable to open tcp_fastopen sysctl for writing");
    val |= rw_mask;
    size = snprintf(buf, 16, "%d", val);
    if (write(fd, buf, size) <= 0) {
      error(1, errno, "Unable to write tcp_fastopen sysctl");
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<enable_fastopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x3,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11b4 <enable_fastopen+0x44>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe71(%rip),%rdx
call   1070 <error@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jg     11e9 <enable_fastopen+0x79>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe5f(%rip),%rdx
call   1070 <error@plt>
lea    -0x20(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
and    -0x8(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     12a7 <enable_fastopen+0x137>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
lea    0xde4(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1248 <enable_fastopen+0xd8>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe23(%rip),%rdx
call   1070 <error@plt>
mov    -0x8(%rbp),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x10,%esi
lea    0xe31(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jg     129f <enable_fastopen+0x12f>
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdfe(%rip),%rdx
call   1070 <error@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enable_fastopen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe81(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a6 <enable_fastopen+0x36>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1060 <read@plt>
test   %rax,%rax
jg     11d4 <enable_fastopen+0x64>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
mov    %rsp,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
not    %eax
test   $0x3,%al
je     1274 <enable_fastopen+0x104>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xdff(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1226 <enable_fastopen+0xb6>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
or     $0x3,%ebp
lea    0xe65(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x10,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jg     126d <enable_fastopen+0xfd>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"/proc/sys/net/ipv4/tcp_fastopen\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"Unable to open tcp_fastopen sysctl\00"
@.str.2 = [35 x i8] c"Unable to read tcp_fastopen sysctl\00"
@O_RDWR = global i32 0
@.str.3 = [47 x i8] c"Unable to open tcp_fastopen sysctl for writing\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [36 x i8] c"Unable to write tcp_fastopen sysctl\00"
define void @enable_fastopen() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [16 x i8]
%6 = call i32 @open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %6, i32* %1
store i32 3, i32* %2
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = call i32 @error(i32 1, i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %9, %0
%13 = load i32, i32* %1
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%15 = call i64 @read(i32 %13, i8* %14, i32 16)
%16 = icmp sle i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @errno
%19 = call i32 @error(i32 1, i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %17, %12
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %3
%23 = load i32, i32* %1
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %3
%26 = load i32, i32* %2
%27 = and i32 %25, %26
%28 = load i32, i32* %2
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %56
30: ; preds = %20
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %31)
store i32 %32, i32* %1
%33 = load i32, i32* %1
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @errno
%37 = call i32 @error(i32 1, i32 %36, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %35, %30
%39 = load i32, i32* %2
%40 = load i32, i32* %3
%41 = or i32 %40, %39
store i32 %41, i32* %3
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%43 = load i32, i32* %3
%44 = call i32 @snprintf(i8* %42, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %43)
store i32 %44, i32* %4
%45 = load i32, i32* %1
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%47 = load i32, i32* %4
%48 = call i64 @write(i32 %45, i8* %46, i32 %47)
%49 = icmp sle i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %38
%51 = load i32, i32* @errno
%52 = call i32 @error(i32 1, i32 %51, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %50, %38
%54 = load i32, i32* %1
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53, %20
ret void
}

LLVM IR (-O2):
@.str = [32 x i8] c"/proc/sys/net/ipv4/tcp_fastopen\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"Unable to open tcp_fastopen sysctl\00"
@.str.2 = [35 x i8] c"Unable to read tcp_fastopen sysctl\00"
@O_RDWR = global i32 0
@.str.3 = [47 x i8] c"Unable to open tcp_fastopen sysctl for writing\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [36 x i8] c"Unable to write tcp_fastopen sysctl\00"
define void @enable_fastopen() {
%1 = alloca [16 x i8]
%2 = tail call i32 @open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 0)
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = icmp slt i32 %2, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 @error(i32 1, i32 %6, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %5, %0
%9 = call i64 @read(i32 %2, i8* %3, i32 16)
%10 = icmp slt i64 %9, 1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = tail call i32 @error(i32 1, i32 %12, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %11, %8
%15 = call i32 @atoi(i8* %3)
%16 = tail call i32 @close(i32 %2)
%17 = and i32 %15, 3
%18 = icmp eq i32 %17, 3
br i1 %18, label %36, label %19
19: ; preds = %14
%20 = load i32, i32* @O_RDWR
%21 = tail call i32 @open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @errno
%25 = tail call i32 @error(i32 1, i32 %24, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %23, %19
%27 = or i32 %15, 3
%28 = call i32 @snprintf(i8* %3, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = call i64 @write(i32 %21, i8* %3, i32 %28)
%30 = icmp slt i64 %29, 1
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* @errno
%33 = call i32 @error(i32 1, i32 %32, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %31, %26
%35 = call i32 @close(i32 %21)
br label %36
36: ; preds = %34, %14
ret void
}