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

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_SNDBUF;
int _IOFBF;
int _IOLBF;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int,
                           int);
FILE *sock_to_file(int sock, const char *mode) {
  int bufsz = (4 * 1024);
  int bufmode = _IOFBF;
  FILE *fp = fdopen(sock, (*mode == 'w') ? "w" : "r");
  switch (mode[1]) {
  case 't':
    bufmode = _IOLBF;
    break;
  case 'b':
    bufsz = (28 * 1024) * 2;
    setsockopt(sock, SOL_SOCKET, (*mode == 'w') ? SO_SNDBUF : SO_RCVBUF, &bufsz,
               sizeof(bufsz));
    bufsz = (28 * 1024);
    break;
  }
  setvbuf(fp, NULL, bufmode, bufsz);
  return fp;
}


x86 assembly (clang, -O0):
<sock_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1000,-0x14(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%al
sub    $0x77,%al
lea    0xe9c(%rip),%rax
lea    0xe97(%rip),%rsi
cmove  %rax,%rsi
call   1040 <fdopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x62,%eax
je     11b3 <sock_to_file+0x83>
jmp    1191 <sock_to_file+0x61>
mov    -0x24(%rbp),%eax
sub    $0x74,%eax
jne    1218 <sock_to_file+0xe8>
jmp    11a2 <sock_to_file+0x72>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1218 <sock_to_file+0xe8>
movl   $0xe000,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    11ed <sock_to_file+0xbd>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    11f9 <sock_to_file+0xc9>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x7000,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setvbuf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x1000,0x4(%rsp)
mov    0x2e75(%rip),%rax
mov    (%rax),%r14d
cmpb   $0x77,(%rsi)
lea    0xea8(%rip),%rax
lea    0xea3(%rip),%rsi
cmove  %rax,%rsi
call   1040 <fdopen@plt>
mov    %rax,%r15
movsbl 0x1(%rbx),%eax
cmp    $0x62,%eax
je     118a <sock_to_file+0x5a>
mov    $0x1000,%ecx
cmp    $0x74,%eax
jne    11d1 <sock_to_file+0xa1>
mov    0x2e43(%rip),%rax
mov    (%rax),%r14d
jmp    11d1 <sock_to_file+0xa1>
movl   $0xe000,0x4(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
cmpb   $0x77,(%rbx)
jne    11a9 <sock_to_file+0x79>
mov    0x2e39(%rip),%rax
jmp    11b0 <sock_to_file+0x80>
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x7000,0x4(%rsp)
mov    $0x7000,%ecx
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1050 <setvbuf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_IOFBF = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@_IOLBF = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
define i32* @sock_to_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 4096, i32* %5
%8 = load i32, i32* @_IOFBF
store i32 %8, i32* %6
%9 = load i32, i32* %3
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 119
%14 = zext i1 %13 to i64
%15 = select i1 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%16 = call i32* @fdopen(i32 %9, i8* %15)
store i32* %16, i32** %7
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
switch i32 %20, label %37 [
i32 116, label %21
i32 98, label %23
]
21: ; preds = %2
%22 = load i32, i32* @_IOLBF
store i32 %22, i32* %6
br label %37
23: ; preds = %2
store i32 57344, i32* %5
%24 = load i32, i32* %3
%25 = load i32, i32* @SOL_SOCKET
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 119
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = load i32, i32* @SO_SNDBUF
br label %34
32: ; preds = %23
%33 = load i32, i32* @SO_RCVBUF
br label %34
34: ; preds = %32, %30
%35 = phi i32 [ %31, %30 ], [ %33, %32 ]
%36 = call i32 @setsockopt(i32 %24, i32 %25, i32 %35, i32* %5, i32 4)
store i32 28672, i32* %5
br label %37
37: ; preds = %2, %34, %21
%38 = load i32*, i32** %7
%39 = load i32, i32* %6
%40 = load i32, i32* %5
%41 = call i32 @setvbuf(i32* %38, i32* null, i32 %39, i32 %40)
%42 = load i32*, i32** %7
ret i32* %42
}

LLVM IR (-O2):
@_IOFBF = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@_IOLBF = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
define noalias i32* @sock_to_file(i32 %0, i8* nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 4096, i32* %3
%5 = load i32, i32* @_IOFBF
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 119
%8 = select i1 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%9 = tail call i32* @fdopen(i32 %0, i8* %8)
%10 = getelementptr inbounds i8, i8* %1, i64 1
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
switch i32 %12, label %23 [
i32 116, label %13
i32 98, label %15
]
13: ; preds = %2
%14 = load i32, i32* @_IOLBF
br label %23
15: ; preds = %2
store i32 57344, i32* %3
%16 = load i32, i32* @SOL_SOCKET
%17 = load i8, i8* %1
%18 = icmp eq i8 %17, 119
%19 = load i32, i32* @SO_SNDBUF
%20 = load i32, i32* @SO_RCVBUF
%21 = select i1 %18, i32 %19, i32 %20
%22 = call i32 @setsockopt(i32 %0, i32 %16, i32 %21, i32* %3, i32 4)
store i32 28672, i32* %3
br label %23
23: ; preds = %2, %15, %13
%24 = phi i32 [ 4096, %2 ], [ 28672, %15 ], [ 4096, %13 ]
%25 = phi i32 [ %5, %2 ], [ %5, %15 ], [ %14, %13 ]
%26 = call i32 @setvbuf(i32* %9, i32* null, i32 %25, i32 %24)
ret i32* %9
}