#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 */
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int ipc_read_line(int fd, char *line, int max_line_length) {
  int p;
  if (fd < 0 || !line)
    return 0;
  for (p = 0; p < max_line_length - 1; p++) {
    if (read(fd, &line[p], 1) != 1) {
      perror("read");
      break;
    }
    if (line[p] == '\n')
      break;
  }
  line[p] = '\0';
  return p ? p - 1 : 0;
}


x86 assembly (clang, -O0):
<ipc_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1147 <ipc_read_line+0x27>
cmpq   $0x0,-0x10(%rbp)
jne    1153 <ipc_read_line+0x33>
movl   $0x0,-0x4(%rbp)
jmp    11fe <ipc_read_line+0xde>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11ca <ipc_read_line+0xaa>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     119d <ipc_read_line+0x7d>
lea    0xe6d(%rip),%rdi
call   1040 <perror@plt>
jmp    11ca <ipc_read_line+0xaa>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11b7 <ipc_read_line+0x97>
jmp    11ca <ipc_read_line+0xaa>
jmp    11bc <ipc_read_line+0x9c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115a <ipc_read_line+0x3a>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x18(%rbp)
je     11ee <ipc_read_line+0xce>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f8 <ipc_read_line+0xd8>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f8 <ipc_read_line+0xd8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %edi,%edi
js     11a1 <ipc_read_line+0x81>
mov    %rsi,%r15
test   %rsi,%rsi
je     11a1 <ipc_read_line+0x81>
mov    %edx,%r14d
cmp    $0x1,%edx
jle    1179 <ipc_read_line+0x59>
mov    %edi,%r12d
add    $0xffffffff,%r14d
mov    %r14d,%r13d
xor    %ebp,%ebp
nopl   (%rax)
lea    (%r15,%rbp,1),%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    117f <ipc_read_line+0x5f>
cmpb   $0xa,(%rbx)
je     118b <ipc_read_line+0x6b>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1150 <ipc_read_line+0x30>
jmp    118e <ipc_read_line+0x6e>
movb   $0x0,(%r15)
jmp    119f <ipc_read_line+0x7f>
lea    0xe7a(%rip),%rdi
call   1040 <perror@plt>
mov    %ebp,%r14d
mov    %r14d,%eax
movb   $0x0,(%r15,%rax,1)
sub    $0x1,%r14d
mov    %r14d,%eax
jae    11a1 <ipc_read_line+0x81>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
define i32 @ipc_read_line(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%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 i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11, %3
store i32 0, i32* %4
br label %57
15: ; preds = %11
store i32 0, i32* %8
br label %16
16: ; preds = %41, %15
%17 = load i32, i32* %8
%18 = load i32, i32* %7
%19 = sub nsw i32 %18, 1
%20 = icmp slt i32 %17, %19
br i1 %20, label %21, label %44
21: ; preds = %16
%22 = load i32, i32* %5
%23 = load i8*, i8** %6
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = call i32 @read(i32 %22, i8* %26, i32 1)
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %44
31: ; preds = %21
%32 = load i8*, i8** %6
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %40
39: ; preds = %31
br label %44
40: ; preds = %31
br label %41
41: ; preds = %40
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %16
44: ; preds = %39, %29, %16
%45 = load i8*, i8** %6
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = load i32, i32* %8
%53 = sub nsw i32 %52, 1
br label %55
54: ; preds = %44
br label %55
55: ; preds = %54, %51
%56 = phi i32 [ %53, %51 ], [ 0, %54 ]
store i32 %56, i32* %4
br label %57
57: ; preds = %55, %14
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
define i32 @ipc_read_line(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %0, -1
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
br i1 %6, label %7, label %36
7: ; preds = %3
%8 = add i32 %2, -1
%9 = icmp sgt i32 %2, 1
br i1 %9, label %11, label %10
10: ; preds = %7
store i8 0, i8* %1
br label %35
11: ; preds = %7
%12 = zext i32 %8 to i64
br label %13
13: ; preds = %11, %24
%14 = phi i64 [ 0, %11 ], [ %25, %24 ]
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = tail call i32 @read(i32 %0, i8* %15, i32 1)
%17 = icmp eq i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = trunc i64 %14 to i32
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %29
21: ; preds = %13
%22 = load i8, i8* %15
%23 = icmp eq i8 %22, 10
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = add nuw nsw i64 %14, 1
%26 = icmp eq i64 %25, %12
br i1 %26, label %29, label %13
27: ; preds = %21
%28 = trunc i64 %14 to i32
br label %29
29: ; preds = %24, %27, %18
%30 = phi i32 [ %19, %18 ], [ %28, %27 ], [ %8, %24 ]
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %1, i64 %31
store i8 0, i8* %32
%33 = icmp eq i32 %30, 0
%34 = add nsw i32 %30, -1
br i1 %33, label %35, label %36
35: ; preds = %10, %29
br label %36
36: ; preds = %35, %29, %3
%37 = phi i32 [ 0, %3 ], [ 0, %35 ], [ %34, %29 ]
ret i32 %37
}