#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ gg_debug(int, char *, ...);
int read(int, char *, int);
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 = read(sock, buf, 1)) == -1 && errno != EINTR) {
        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);
    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    12b7 <gg_read_line+0x187>
jmp    1169 <gg_read_line+0x39>
cmpl   $0x1,-0x1c(%rbp)
jle    12a8 <gg_read_line+0x178>
jmp    1178 <gg_read_line+0x48>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11f9 <gg_read_line+0xc9>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
je     11f9 <gg_read_line+0xc9>
mov    0x2e2a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
mov    $0x10,%edi
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1040 <gg_debug@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12b7 <gg_read_line+0x187>
cmpl   $0x0,-0x20(%rbp)
jne    122a <gg_read_line+0xfa>
mov    $0x10,%edi
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1040 <gg_debug@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12b7 <gg_read_line+0x187>
jmp    122f <gg_read_line+0xff>
jmp    1234 <gg_read_line+0x104>
xor    %eax,%eax
cmpl   $0xffffffff,-0x20(%rbp)
mov    %al,-0x29(%rbp)
jne    125d <gg_read_line+0x12d>
mov    0x2d96(%rip),%rax
mov    (%rax),%rax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
sete   %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    1289 <gg_read_line+0x159>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    12a8 <gg_read_line+0x178>
jmp    128e <gg_read_line+0x15e>
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     11f5 <gg_read_line+0xc5>
mov    %edx,%r14d
test   %edx,%edx
js     11f5 <gg_read_line+0xc5>
mov    %rsi,%rbx
cmp    $0x2,%r14d
jb     11b0 <gg_read_line+0x80>
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
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    1191 <gg_read_line+0x61>
mov    (%r12),%rbp
cmp    0x0(%r13),%rbp
je     1170 <gg_read_line+0x40>
jmp    11ca <gg_read_line+0x9a>
test   %eax,%eax
je     11b5 <gg_read_line+0x85>
lea    0x1(%rbx),%rax
cmpb   $0xa,(%rbx)
je     1204 <gg_read_line+0xd4>
lea    -0x1(%r14),%ecx
mov    %rax,%rbx
cmp    $0x2,%r14d
mov    %ecx,%r14d
jg     1170 <gg_read_line+0x40>
jmp    11ef <gg_read_line+0xbf>
mov    %rbx,%rax
jmp    11ef <gg_read_line+0xbf>
lea    0xe74(%rip),%rsi
mov    $0x10,%edi
xor    %eax,%eax
call   1040 <gg_debug@plt>
jmp    11ea <gg_read_line+0xba>
mov    %rbp,%rdi
call   1050 <strerror@plt>
lea    0xe27(%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    11ef <gg_read_line+0xbf>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = 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 %69
15: ; preds = %11
br label %16
16: ; preds = %61, %15
%17 = load i32, i32* %7
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %66
19: ; preds = %16
br label %20
20: ; preds = %50, %19
%21 = load i32, i32* %5
%22 = load i8*, i8** %6
%23 = call i32 @read(i32 %21, i8* %22, i32 1)
store i32 %23, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %35
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 %35
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @errno
%32 = call i32 @strerror(i64 %31)
%33 = call i32 (i32, i8*, ...) @gg_debug(i32 16, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i64 %30, i32 %32)
%34 = load i8*, i8** %6
store i8 0, i8* %34
store i8* null, i8** %4
br label %69
35: ; preds = %25, %20
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 (i32, i8*, ...) @gg_debug(i32 16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%40 = load i8*, i8** %6
store i8 0, i8* %40
store i8* null, i8** %4
br label %69
41: ; preds = %35
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINTR
%49 = icmp eq i64 %47, %48
br label %50
50: ; preds = %46, %43
%51 = phi i1 [ false, %43 ], [ %49, %46 ]
br i1 %51, label %20, label %52
52: ; preds = %50
%53 = load i8*, i8** %6
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 10
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i8*, i8** %6
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %6
br label %66
60: ; preds = %52
br label %61
61: ; preds = %60
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %6
%64 = load i32, i32* %7
%65 = add nsw i32 %64, -1
store i32 %65, i32* %7
br label %16
66: ; preds = %57, %16
%67 = load i8*, i8** %6
store i8 0, i8* %67
%68 = load i8*, i8** %6
store i8* %68, i8** %4
br label %69
69: ; preds = %66, %38, %29, %14
%70 = load i8*, i8** %4
ret i8* %70
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = 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 %33, label %7
7: ; preds = %3
%8 = icmp ugt i32 %2, 1
br i1 %8, label %9, label %30
9: ; preds = %7, %23
%10 = phi i32 [ %27, %23 ], [ %2, %7 ]
%11 = phi i8* [ %26, %23 ], [ %1, %7 ]
br label %12
12: ; preds = %14, %9
%13 = tail call i32 @read(i32 %0, i8* %11, i32 1)
switch i32 %13, label %23 [
i32 -1, label %14
i32 0, label %21
]
14: ; preds = %12
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %12, label %18
18: ; preds = %14
%19 = tail call i32 @strerror(i64 %15)
%20 = 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 %19)
br label %30
21: ; preds = %12
%22 = 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 %30
23: ; preds = %12
%24 = load i8, i8* %11
%25 = icmp ne i8 %24, 10
%26 = getelementptr inbounds i8, i8* %11, i64 1
%27 = add nsw i32 %10, -1
%28 = icmp sgt i32 %10, 2
%29 = select i1 %25, i1 %28, i1 false
br i1 %29, label %9, label %30
30: ; preds = %23, %7, %18, %21
%31 = phi i8* [ %11, %21 ], [ %11, %18 ], [ %1, %7 ], [ %26, %23 ]
%32 = phi i8* [ null, %21 ], [ null, %18 ], [ %1, %7 ], [ %26, %23 ]
store i8 0, i8* %31
br label %33
33: ; preds = %30, %3
%34 = phi i8* [ null, %3 ], [ %32, %30 ]
ret i8* %34
}