#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, int *, int);
int read_input(int sock, int (*parse)(char *msg)) {
  char buffer[512];
  int len, i, ch;
  i = 0;
  ch = 0;
  while (ch != '\n') {
    len = read(sock, &ch, 1);
    if (len == -1) {
      perror("read");
      return -1;
    }
    buffer[i] = ch;
    if (ch == '\n')
      buffer[i] = 0;
    else
      i++;
  }
  return parse(buffer);
}


x86 assembly (clang, -O0):
<read_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x218(%rbp)
movl   $0x0,-0x21c(%rbp)
cmpl   $0xa,-0x21c(%rbp)
je     11dd <read_input+0xbd>
mov    -0x8(%rbp),%edi
lea    -0x21c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0xffffffff,-0x214(%rbp)
jne    1192 <read_input+0x72>
lea    0xe7f(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <read_input+0xcd>
mov    -0x21c(%rbp),%eax
mov    %al,%cl
movslq -0x218(%rbp),%rax
mov    %cl,-0x210(%rbp,%rax,1)
cmpl   $0xa,-0x21c(%rbp)
jne    11c9 <read_input+0xa9>
movslq -0x218(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
jmp    11d8 <read_input+0xb8>
mov    -0x218(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x218(%rbp)
jmp    1146 <read_input+0x26>
mov    -0x10(%rbp),%rax
lea    -0x210(%rbp),%rdi
call   *%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_input>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1183 <read_input+0x63>
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%eax
mov    %al,(%rbx)
cmp    $0xa,%eax
je     1196 <read_input+0x76>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
add    $0x1,%rbx
cmp    $0xffffffff,%eax
jne    1160 <read_input+0x40>
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a1 <read_input+0x81>
movb   $0x0,(%rbx)
lea    0x10(%rsp),%rdi
call   *%r14
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
define i32 @read_input(i32 %0, i32 (i8*)* nocapture %1) {
%3 = alloca [512 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = call i32 @read(i32 %0, i32* %4, i32 1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %13, label %15
9: ; preds = %15
%10 = add nuw nsw i32 %16, 1
%11 = call i32 @read(i32 %0, i32* %4, i32 1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9, %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %26
15: ; preds = %2, %9
%16 = phi i32 [ %10, %9 ], [ 0, %2 ]
%17 = load i32, i32* %4
%18 = trunc i32 %17 to i8
%19 = zext i32 %16 to i64
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %19
store i8 %18, i8* %20
%21 = icmp eq i32 %17, 10
br i1 %21, label %22, label %9
22: ; preds = %15
%23 = zext i32 %16 to i64
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = call i32 %1(i8* %5)
br label %26
26: ; preds = %22, %13
%27 = phi i32 [ -1, %13 ], [ %25, %22 ]
ret i32 %27
}