int read(int file, char *ptr, int len) {
  if (file == 0) {
    asm volatile("call *0x00100038" : "=c"(len) : "c"(len), "D"(ptr));
    ptr[len] = '\n';
    ptr[len + 1] = 0;
    len += 1;
    write(1, "\n", 1);
  } else {
    asm volatile("call *0x001000F8" : "=c"(len) : "a"(file));
  }
  return len;
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    117d <read+0x6d>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rdi
call   *0x100038
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    $0x1,%edx
lea    0xe91(%rip),%rsi
mov    %edx,%edi
mov    $0x0,%al
call   1030 <write@plt>
jmp    118a <read+0x7a>
mov    -0x4(%rbp),%eax
call   *0x1000f8
mov    %ecx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
test   %edi,%edi
je     1120 <read+0x10>
mov    %edi,%eax
call   *0x1000f8
mov    %ecx,%eax
ret
push   %rbx
mov    %edx,%ecx
mov    %rsi,%rdi
call   *0x100038
movslq %ecx,%rbx
movw   $0xa,(%rsi,%rbx,1)
add    $0x1,%ebx
lea    0xec0(%rip),%rsi
mov    $0x1,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
define i32 @read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %25
9: ; preds = %3
%10 = load i32, i32* %6
%11 = load i8*, i8** %5
%12 = call i32 asm sideeffect "call *0x00100038", "={cx},{cx},{di},~{dirflag},~{fpsr},~{flags}"(i32 %10, i8* %11)
store i32 %12, i32* %6
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 10, i8* %16
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8 0, i8* %21
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
%24 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %28
25: ; preds = %3
%26 = load i32, i32* %4
%27 = call i32 asm sideeffect "call *0x001000F8", "={cx},{ax},~{dirflag},~{fpsr},~{flags}"(i32 %26)
store i32 %27, i32* %6
br label %28
28: ; preds = %25, %9
%29 = load i32, i32* %6
ret i32 %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"\0A\00"
define i32 @read(i32 %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %0, 0
br i1 %4, label %5, label %13
5: ; preds = %3
%6 = tail call i32 asm sideeffect "call *0x00100038", "={cx},{cx},{di},~{dirflag},~{fpsr},~{flags}"(i32 %2, i8* %1)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 10, i8* %8
%9 = add nsw i32 %6, 1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %11
%12 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %15
13: ; preds = %3
%14 = tail call i32 asm sideeffect "call *0x001000F8", "={cx},{ax},~{dirflag},~{fpsr},~{flags}"(i32 %0)
br label %15
15: ; preds = %13, %5
%16 = phi i32 [ %9, %5 ], [ %14, %13 ]
ret i32 %16
}