#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*/ FILE;

/* Variables and functions */
size_t INT_MAX;
scalar_t__ check_max_pipe_size(size_t, size_t const);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, size_t *);
size_t get_pagesize();
size_t get_max_pipe_size(void) {
  int i;
  size_t prev_sz, sz, min, max;
  FILE *fp;
  const size_t page_size = get_pagesize();
  fp = fopen("/proc/sys/fs/pipe-max-size", "r");
  if (fp) {
    int ret = fscanf(fp, "%zu", &sz);
    (void)fclose(fp);
    if ((ret == 1) && !check_max_pipe_size(sz, page_size))
      return sz;
  }
  min = page_size;
  max = INT_MAX;
  prev_sz = 0;
  for (i = 0; i < 64; i++) {
    sz = min + (max - min) / 2;
    if (prev_sz == sz)
      return sz;
    prev_sz = sz;
    if (check_max_pipe_size(sz, page_size) == 0) {
      min = sz;
    } else {
      max = sz;
    }
  }
  return sz;
}


x86 assembly (clang, -O0):
<get_max_pipe_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    $0x0,%al
call   1050 <get_pagesize@plt>
mov    %rax,-0x40(%rbp)
lea    0xe96(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11d8 <get_max_pipe_size+0x88>
mov    -0x38(%rbp),%rdi
lea    0xe8d(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <fscanf@plt>
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x44(%rbp)
jne    11d3 <get_max_pipe_size+0x83>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1030 <check_max_pipe_size@plt>
cmp    $0x0,%rax
jne    11d3 <get_max_pipe_size+0x83>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1288 <get_max_pipe_size+0x138>
jmp    11d8 <get_max_pipe_size+0x88>
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x40,-0xc(%rbp)
jge    1280 <get_max_pipe_size+0x130>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    -0x28(%rbp),%rcx
shr    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1239 <get_max_pipe_size+0xe9>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1288 <get_max_pipe_size+0x138>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1030 <check_max_pipe_size@plt>
cmp    $0x0,%rax
jne    1265 <get_max_pipe_size+0x115>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    126d <get_max_pipe_size+0x11d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1272 <get_max_pipe_size+0x122>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11fd <get_max_pipe_size+0xad>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_max_pipe_size>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1050 <get_pagesize@plt>
mov    %rax,%r14
lea    0xe93(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b6 <get_max_pipe_size+0x66>
mov    %rax,%rbx
lea    0xe95(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x1,%ebp
jne    11b6 <get_max_pipe_size+0x66>
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1030 <check_max_pipe_size@plt>
test   %rax,%rax
je     120c <get_max_pipe_size+0xbc>
mov    0x2e2b(%rip),%rax
mov    (%rax),%r12
mov    $0x40,%r15d
xor    %ebx,%ebx
mov    %r14,%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rax
mov    %r12,%rbx
sub    %rbp,%rbx
shr    %rbx
add    %rbp,%rbx
mov    %rbx,0x8(%rsp)
cmp    %rbx,%rax
je     1211 <get_max_pipe_size+0xc1>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <check_max_pipe_size@plt>
test   %rax,%rax
mov    0x8(%rsp),%rax
cmove  %rax,%rbp
cmovne %rax,%r12
add    $0xffffffff,%r15d
jne    11d0 <get_max_pipe_size+0x80>
jmp    1211 <get_max_pipe_size+0xc1>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"/proc/sys/fs/pipe-max-size\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%zu\00"
@INT_MAX = global i64 0
define i64 @get_max_pipe_size() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = call i64 (...) @get_pagesize()
store i64 %10, i64* %8
%11 = call i32* @fopen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %29
14: ; preds = %0
%15 = load i32*, i32** %7
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %4)
store i32 %16, i32* %9
%17 = load i32*, i32** %7
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %28
21: ; preds = %14
%22 = load i64, i64* %4
%23 = load i64, i64* %8
%24 = call i64 @check_max_pipe_size(i64 %22, i64 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = load i64, i64* %4
store i64 %27, i64* %1
br label %63
28: ; preds = %21, %14
br label %29
29: ; preds = %28, %0
%30 = load i64, i64* %8
store i64 %30, i64* %5
%31 = load i64, i64* @INT_MAX
store i64 %31, i64* %6
store i64 0, i64* %3
store i32 0, i32* %2
br label %32
32: ; preds = %58, %29
%33 = load i32, i32* %2
%34 = icmp slt i32 %33, 64
br i1 %34, label %35, label %61
35: ; preds = %32
%36 = load i64, i64* %5
%37 = load i64, i64* %6
%38 = load i64, i64* %5
%39 = sub i64 %37, %38
%40 = udiv i64 %39, 2
%41 = add i64 %36, %40
store i64 %41, i64* %4
%42 = load i64, i64* %3
%43 = load i64, i64* %4
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %35
%46 = load i64, i64* %4
store i64 %46, i64* %1
br label %63
47: ; preds = %35
%48 = load i64, i64* %4
store i64 %48, i64* %3
%49 = load i64, i64* %4
%50 = load i64, i64* %8
%51 = call i64 @check_max_pipe_size(i64 %49, i64 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = load i64, i64* %4
store i64 %54, i64* %5
br label %57
55: ; preds = %47
%56 = load i64, i64* %4
store i64 %56, i64* %6
br label %57
57: ; preds = %55, %53
br label %58
58: ; preds = %57
%59 = load i32, i32* %2
%60 = add nsw i32 %59, 1
store i32 %60, i32* %2
br label %32
61: ; preds = %32
%62 = load i64, i64* %4
store i64 %62, i64* %1
br label %63
63: ; preds = %61, %45, %26
%64 = load i64, i64* %1
ret i64 %64
}

LLVM IR (-O2):
@.str = [27 x i8] c"/proc/sys/fs/pipe-max-size\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%zu\00"
@INT_MAX = global i64 0
define i64 @get_max_pipe_size() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i64 (...) @get_pagesize()
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %15, label %6
6: ; preds = %0
%7 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%8 = call i32 @fclose(i32* %4)
%9 = icmp eq i32 %7, 1
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i64, i64* %1
%12 = call i64 @check_max_pipe_size(i64 %11, i64 %3)
%13 = icmp eq i64 %12, 0
%14 = load i64, i64* %1
br i1 %13, label %34, label %15
15: ; preds = %6, %10, %0
%16 = load i64, i64* @INT_MAX
br label %17
17: ; preds = %15, %26
%18 = phi i32 [ 0, %15 ], [ %32, %26 ]
%19 = phi i64 [ 0, %15 ], [ %24, %26 ]
%20 = phi i64 [ %16, %15 ], [ %31, %26 ]
%21 = phi i64 [ %3, %15 ], [ %30, %26 ]
%22 = sub i64 %20, %21
%23 = lshr i64 %22, 1
%24 = add i64 %23, %21
store i64 %24, i64* %1
%25 = icmp eq i64 %19, %24
br i1 %25, label %34, label %26
26: ; preds = %17
%27 = call i64 @check_max_pipe_size(i64 %24, i64 %3)
%28 = icmp eq i64 %27, 0
%29 = load i64, i64* %1
%30 = select i1 %28, i64 %29, i64 %21
%31 = select i1 %28, i64 %20, i64 %29
%32 = add nuw nsw i32 %18, 1
%33 = icmp eq i32 %32, 64
br i1 %33, label %34, label %17
34: ; preds = %17, %26, %10
%35 = phi i64 [ %14, %10 ], [ %19, %17 ], [ %29, %26 ]
ret i64 %35
}