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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ gg_debug(int, char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
char *gg_read_line(int sock, char *buf, int length) {
  int ret;
  if (!buf || length < 0)
    return NULL;
  for (; length > 1; buf++, length--) {
    do {
      if ((ret = recv(sock, buf, 1, 0)) == -1 && errno != EINTR &&
          errno != EAGAIN) {
        gg_debug(16,
                 "// gg_read_line() "
                 "error on read (errno=%d, %s)\n",
                 errno, strerror(errno));
        *buf = 0;
        return NULL;
      } else if (ret == 0) {
        gg_debug(16, "// gg_read_line() "
                     "eof reached\n");
        *buf = 0;
        return NULL;
      }
    } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
    if (*buf == '\n') {
      buf++;
      break;
    }
  }
  *buf = 0;
  return buf;
}


x86 assembly (clang, -O0):
<gg_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1157 <gg_read_line+0x27>
cmpl   $0x0,-0x1c(%rbp)
jge    1164 <gg_read_line+0x34>
movq   $0x0,-0x8(%rbp)
jmp    12f8 <gg_read_line+0x1c8>
jmp    1169 <gg_read_line+0x39>
cmpl   $0x1,-0x1c(%rbp)
jle    12e9 <gg_read_line+0x1b9>
jmp    1178 <gg_read_line+0x48>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1215 <gg_read_line+0xe5>
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
je     1215 <gg_read_line+0xe5>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e06(%rip),%rcx
cmp    (%rcx),%rax
je     1215 <gg_read_line+0xe5>
mov    0x2e0e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
mov    $0x10,%edi
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1040 <gg_debug@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12f8 <gg_read_line+0x1c8>
cmpl   $0x0,-0x20(%rbp)
jne    1246 <gg_read_line+0x116>
mov    $0x10,%edi
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <gg_debug@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12f8 <gg_read_line+0x1c8>
jmp    124b <gg_read_line+0x11b>
jmp    1250 <gg_read_line+0x120>
xor    %eax,%eax
cmpl   $0xffffffff,-0x20(%rbp)
mov    %al,-0x29(%rbp)
jne    129e <gg_read_line+0x16e>
mov    0x2d7a(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2d66(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x2a(%rbp)
je     1298 <gg_read_line+0x168>
mov    0x2d5b(%rip),%rax
mov    (%rax),%rax
mov    0x2d39(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1178 <gg_read_line+0x48>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    12ca <gg_read_line+0x19a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    12e9 <gg_read_line+0x1b9>
jmp    12cf <gg_read_line+0x19f>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1169 <gg_read_line+0x39>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gg_read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %rsi,%rsi
je     1203 <gg_read_line+0xd3>
mov    %edx,%r14d
test   %edx,%edx
js     1203 <gg_read_line+0xd3>
mov    %rsi,%rbx
cmp    $0x2,%r14d
jb     11be <gg_read_line+0x8e>
mov    %edi,%r15d
mov    0x2e7c(%rip),%r12
mov    0x2e6d(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    119f <gg_read_line+0x6f>
mov    (%r12),%rbp
cmp    0x0(%r13),%rbp
je     1170 <gg_read_line+0x40>
mov    0x2e30(%rip),%rax
cmp    (%rax),%rbp
je     1170 <gg_read_line+0x40>
jmp    11d8 <gg_read_line+0xa8>
test   %eax,%eax
je     11c3 <gg_read_line+0x93>
lea    0x1(%rbx),%rax
cmpb   $0xa,(%rbx)
je     1212 <gg_read_line+0xe2>
lea    -0x1(%r14),%ecx
mov    %rax,%rbx
cmp    $0x2,%r14d
mov    %ecx,%r14d
jg     1170 <gg_read_line+0x40>
jmp    11fd <gg_read_line+0xcd>
mov    %rbx,%rax
jmp    11fd <gg_read_line+0xcd>
lea    0xe66(%rip),%rsi
mov    $0x10,%edi
xor    %eax,%eax
call   1040 <gg_debug@plt>
jmp    11f8 <gg_read_line+0xc8>
mov    %rbp,%rdi
call   1050 <strerror@plt>
lea    0xe19(%rip),%rsi
mov    $0x10,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <gg_debug@plt>
mov    %rbx,%rax
xor    %ebx,%ebx
movb   $0x0,(%rax)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%rbx
jmp    11fd <gg_read_line+0xcd>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [48 x i8] c"// gg_read_line() error on read (errno=%d, %s)\0A\00"
@.str.1 = [31 x i8] c"// gg_read_line() eof reached\0A\00"
define i8* @gg_read_line(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11, %3
store i8* null, i8** %4
br label %79
15: ; preds = %11
br label %16
16: ; preds = %71, %15
%17 = load i32, i32* %7
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %76
19: ; preds = %16
br label %20
20: ; preds = %60, %19
%21 = load i32, i32* %5
%22 = load i8*, i8** %6
%23 = call i32 @recv(i32 %21, i8* %22, i32 1, i32 0)
store i32 %23, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %39
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %39
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EAGAIN
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i64, i64* @errno
%35 = load i64, i64* @errno
%36 = call i32 @strerror(i64 %35)
%37 = call i32 (i32, i8*, ...) @gg_debug(i32 16, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i64 %34, i32 %36)
%38 = load i8*, i8** %6
store i8 0, i8* %38
store i8* null, i8** %4
br label %79
39: ; preds = %29, %25, %20
%40 = load i32, i32* %8
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 (i32, i8*, ...) @gg_debug(i32 16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%44 = load i8*, i8** %6
store i8 0, i8* %44
store i8* null, i8** %4
br label %79
45: ; preds = %39
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46
%48 = load i32, i32* %8
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %60
50: ; preds = %47
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp eq i64 %51, %52
br i1 %53, label %58, label %54
54: ; preds = %50
%55 = load i64, i64* @errno
%56 = load i64, i64* @EAGAIN
%57 = icmp eq i64 %55, %56
br label %58
58: ; preds = %54, %50
%59 = phi i1 [ true, %50 ], [ %57, %54 ]
br label %60
60: ; preds = %58, %47
%61 = phi i1 [ false, %47 ], [ %59, %58 ]
br i1 %61, label %20, label %62
62: ; preds = %60
%63 = load i8*, i8** %6
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 10
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = load i8*, i8** %6
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %6
br label %76
70: ; preds = %62
br label %71
71: ; preds = %70
%72 = load i8*, i8** %6
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %6
%74 = load i32, i32* %7
%75 = add nsw i32 %74, -1
store i32 %75, i32* %7
br label %16
76: ; preds = %67, %16
%77 = load i8*, i8** %6
store i8 0, i8* %77
%78 = load i8*, i8** %6
store i8* %78, i8** %4
br label %79
79: ; preds = %76, %42, %33, %14
%80 = load i8*, i8** %4
ret i8* %80
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [48 x i8] c"// gg_read_line() error on read (errno=%d, %s)\0A\00"
@.str.1 = [31 x i8] c"// gg_read_line() eof reached\0A\00"
define i8* @gg_read_line(i32 %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %1, null
%5 = icmp slt i32 %2, 0
%6 = or i1 %4, %5
br i1 %6, label %36, label %7
7: ; preds = %3
%8 = icmp ugt i32 %2, 1
br i1 %8, label %9, label %33
9: ; preds = %7, %26
%10 = phi i32 [ %30, %26 ], [ %2, %7 ]
%11 = phi i8* [ %29, %26 ], [ %1, %7 ]
br label %12
12: ; preds = %14, %9
%13 = tail call i32 @recv(i32 %0, i8* %11, i32 1, i32 0)
switch i32 %13, label %26 [
i32 -1, label %14
i32 0, label %24
]
14: ; preds = %12
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %15, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %12, label %21
21: ; preds = %14
%22 = tail call i32 @strerror(i64 %15)
%23 = tail call i32 (i32, i8*, ...) @gg_debug(i32 16, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i64 %15, i32 %22)
br label %33
24: ; preds = %12
%25 = tail call i32 (i32, i8*, ...) @gg_debug(i32 16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %33
26: ; preds = %12
%27 = load i8, i8* %11
%28 = icmp ne i8 %27, 10
%29 = getelementptr inbounds i8, i8* %11, i64 1
%30 = add nsw i32 %10, -1
%31 = icmp sgt i32 %10, 2
%32 = select i1 %28, i1 %31, i1 false
br i1 %32, label %9, label %33
33: ; preds = %26, %7, %21, %24
%34 = phi i8* [ %11, %24 ], [ %11, %21 ], [ %1, %7 ], [ %29, %26 ]
%35 = phi i8* [ null, %24 ], [ null, %21 ], [ %1, %7 ], [ %29, %26 ]
store i8 0, i8* %34
br label %36
36: ; preds = %33, %3
%37 = phi i8* [ null, %3 ], [ %35, %33 ]
ret i8* %37
}