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

/* Variables and functions */
int BUFSIZ;
int EAGAIN;
int EWOULDBLOCK;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int /*<<< orphan*/ strip_cr(char *, scalar_t__ *);
void save_req(int conn, FILE *req) {
  char buf[BUFSIZ];
  ssize_t r;
  int e;
  int started = 0;
  if (fcntl(conn, F_SETFL, O_NONBLOCK) < 0) {
    fprintf(stderr, "Unable to make socket non-blocking: %s\n",
            strerror(errno));
    exit(1);
  }
  for (;;) {
    r = read(conn, buf, sizeof buf);
    if (!started) {
      if (r < 0) {
        e = errno;
        if (e == EAGAIN || e == EWOULDBLOCK) {
          continue;
        }
        fclose(req);
        fprintf(stderr, "Unable to read socket: %s\n", strerror(e));
        exit(1);
      }
      started = 1;
    }
    if (r < 0) {
      e = errno;
      putc('\n', req);
      fclose(req);
      if (e != EAGAIN && e != EWOULDBLOCK) {
        fprintf(stderr, "Unable to read socket: %s\n", strerror(e));
        exit(1);
      }
      return;
    }
    strip_cr(buf, &r);
    fwrite(buf, 1, r, req);
  }
}


x86 assembly (clang, -O0):
<save_req>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1230 <save_req+0xa0>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xddf(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1235 <save_req+0xa5>
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x4(%rbp),%edi
mov    %eax,%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    12d5 <save_req+0x145>
cmpq   $0x0,-0x28(%rbp)
jge    12ce <save_req+0x13e>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%eax
je     1290 <save_req+0x100>
mov    -0x2c(%rbp),%eax
mov    0x2d30(%rip),%rcx
cmp    (%rcx),%eax
jne    1295 <save_req+0x105>
jmp    1235 <save_req+0xa5>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xd69(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x1,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1363 <save_req+0x1d3>
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rsi
mov    $0xa,%edi
call   1070 <putc@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    0x2c9b(%rip),%rcx
cmp    (%rcx),%eax
je     1357 <save_req+0x1c7>
mov    -0x2c(%rbp),%eax
mov    0x2c99(%rip),%rcx
cmp    (%rcx),%eax
je     1357 <save_req+0x1c7>
mov    0x2caa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x2c(%rbp),%edi
call   10b0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xce0(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret
mov    -0x38(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <strip_cr@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    1235 <save_req+0xa5>

x86 assembly (clang, -O2):
<save_req>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x30(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
mov    %rax,-0x40(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
mov    %edi,%r12d
call   1030 <fcntl@plt>
test   %rax,%rax
js     12e7 <save_req+0x157>
mov    $0x1,%r14b
nopl   0x0(%rax)
mov    %r14d,%ebx
mov    %r12d,%edi
mov    %r15,%rsi
mov    -0x40(%rbp),%rdx
call   1060 <read@plt>
mov    %rax,-0x38(%rbp)
test   %rax,%rax
sets   %r14b
and    %bl,%r14b
cmp    $0x1,%r14b
jne    1240 <save_req+0xb0>
mov    0x2da3(%rip),%rax
mov    (%rax),%r13d
mov    0x2d81(%rip),%rax
cmp    (%rax),%r13d
je     11f0 <save_req+0x60>
mov    0x2d85(%rip),%rax
cmp    (%rax),%r13d
je     11f0 <save_req+0x60>
jmp    12b2 <save_req+0x122>
nopw   0x0(%rax,%rax,1)
test   %rax,%rax
js     1268 <save_req+0xd8>
mov    %r15,%rdi
lea    -0x38(%rbp),%rsi
call   1040 <strip_cr@plt>
mov    -0x38(%rbp),%rdx
mov    %r15,%rdi
mov    $0x1,%esi
mov    -0x30(%rbp),%rcx
call   10a0 <fwrite@plt>
jmp    11f0 <save_req+0x60>
mov    0x2d51(%rip),%rax
mov    (%rax),%r15d
mov    $0xa,%edi
mov    -0x30(%rbp),%rbx
mov    %rbx,%rsi
call   1070 <putc@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d16(%rip),%rax
cmp    (%rax),%r15d
je     12a3 <save_req+0x113>
mov    0x2d1a(%rip),%rax
cmp    (%rax),%r15d
jne    1307 <save_req+0x177>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%ebx
mov    %r13d,%edi
call   10b0 <strerror@plt>
lea    0xd55(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xcfb(%rip),%rsi
jmp    12d3 <save_req+0x143>
mov    0x2cca(%rip),%rax
mov    (%rax),%r14d
mov    %r15d,%edi
call   10b0 <strerror@plt>
lea    0xd08(%rip),%rsi
mov    %r14d,%edi
jmp    12d5 <save_req+0x145>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Unable to make socket non-blocking: %s\0A\00"
@errno = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
@.str.1 = [27 x i8] c"Unable to read socket: %s\0A\00"
define void @save_req(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
store i32 0, i32* %9
%14 = load i32, i32* %3
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* @O_NONBLOCK
%17 = call i64 @fcntl(i32 %14, i32 %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
br label %26
26: ; preds = %78, %44, %25
%27 = load i32, i32* %3
%28 = trunc i64 %11 to i32
%29 = call i64 @read(i32 %27, i8* %13, i32 %28)
store i64 %29, i64* %7
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 0
br i1 %31, label %54, label %32
32: ; preds = %26
%33 = load i64, i64* %7
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %53
35: ; preds = %32
%36 = load i32, i32* @errno
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = load i32, i32* @EAGAIN
%39 = icmp eq i32 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %35
%41 = load i32, i32* %8
%42 = load i32, i32* @EWOULDBLOCK
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40, %35
br label %26
45: ; preds = %40
%46 = load i32*, i32** %4
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* @stderr
%49 = load i32, i32* %8
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %32
store i32 1, i32* %9
br label %54
54: ; preds = %53, %26
%55 = load i64, i64* %7
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %78
57: ; preds = %54
%58 = load i32, i32* @errno
store i32 %58, i32* %8
%59 = load i32*, i32** %4
%60 = call i32 @putc(i8 signext 10, i32* %59)
%61 = load i32*, i32** %4
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %8
%64 = load i32, i32* @EAGAIN
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %76
66: ; preds = %57
%67 = load i32, i32* %8
%68 = load i32, i32* @EWOULDBLOCK
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %76
70: ; preds = %66
%71 = load i32, i32* @stderr
%72 = load i32, i32* %8
%73 = call i8* @strerror(i32 %72)
%74 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %66, %57
%77 = load i8*, i8** %5
ret void
78: ; preds = %54
%79 = call i32 @strip_cr(i8* %13, i64* %7)
%80 = load i64, i64* %7
%81 = load i32*, i32** %4
%82 = call i32 @fwrite(i8* %13, i32 1, i64 %80, i32* %81)
br label %26
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Unable to make socket non-blocking: %s\0A\00"
@errno = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
@.str.1 = [27 x i8] c"Unable to read socket: %s\0A\00"
define void @save_req(i32 %0, i32* nocapture %1) {
%3 = alloca i64
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i64* %3 to i8*
%8 = load i32, i32* @F_SETFL
%9 = load i32, i32* @O_NONBLOCK
%10 = tail call i64 @fcntl(i32 %0, i32 %8, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2, %56
%19 = phi i1 [ %22, %56 ], [ true, %2 ]
%20 = call i64 @read(i32 %0, i8* %6, i32 %4)
store i64 %20, i64* %3
%21 = icmp slt i64 %20, 0
%22 = and i1 %19, %21
br i1 %22, label %23, label %36
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = load i32, i32* @EAGAIN
%26 = icmp eq i32 %24, %25
%27 = load i32, i32* @EWOULDBLOCK
%28 = icmp eq i32 %24, %27
%29 = select i1 %26, i1 true, i1 %28
br i1 %29, label %56, label %30
30: ; preds = %23
%31 = call i32 @fclose(i32* %1)
%32 = load i32, i32* @stderr
%33 = call i8* @strerror(i32 %24)
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %18
br i1 %21, label %37, label %52
37: ; preds = %36
%38 = load i32, i32* @errno
%39 = call i32 @putc(i8 signext 10, i32* %1)
%40 = call i32 @fclose(i32* %1)
%41 = load i32, i32* @EAGAIN
%42 = icmp eq i32 %38, %41
%43 = load i32, i32* @EWOULDBLOCK
%44 = icmp eq i32 %38, %43
%45 = select i1 %42, i1 true, i1 %44
br i1 %45, label %51, label %46
46: ; preds = %37
%47 = load i32, i32* @stderr
%48 = call i8* @strerror(i32 %38)
%49 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %37
ret void
52: ; preds = %36
%53 = call i32 @strip_cr(i8* %6, i64* %3)
%54 = load i64, i64* %3
%55 = call i32 @fwrite(i8* %6, i32 1, i64 %54, i32* %1)
br label %56
56: ; preds = %52, %23
br label %18
}