unsigned char *read_command(int fd) {
  int size;
  if (read_n_bytes(fd, sizeof(size), (char *)&size) < 0)
    return ((void *)0);
  if (strncmp("quit", (char *)&size, 4) == 0)
    cgc_exit(0);
  if (size > (16 * 1024) || size < 0)
    return ((void *)0);
  unsigned char *command = calloc(sizeof(unsigned char), size);
  if (!command)
    ({
      fdprintf(2,
               "ERROR %s:%d:\t"
               "bad calloc"
               "\n",
               "src/main.c", 216);
      cgc_exit(1);
    });
  if (read_n_bytes(fd, size, (char *)command) != size) {
    free(command);
    return ((void *)0);
  }
  return command;
}


x86 assembly (clang, -O0):
<read_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rdx
mov    $0x4,%esi
mov    $0x0,%al
call   1080 <read_n_bytes@plt>
cmp    $0x0,%eax
jge    1194 <read_command+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1262 <read_command+0x102>
lea    -0x10(%rbp),%rsi
lea    0xe61(%rip),%rdi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    11bb <read_command+0x5b>
xor    %edi,%edi
mov    $0x0,%al
call   1060 <cgc_exit@plt>
cmpl   $0x4000,-0x10(%rbp)
jg     11d2 <read_command+0x72>
cmpl   $0x0,-0x10(%rbp)
jge    11df <read_command+0x7f>
movq   $0x0,-0x8(%rbp)
jmp    1262 <read_command+0x102>
movslq -0x10(%rbp),%rsi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122a <read_command+0xca>
mov    $0x2,%edi
lea    0xdfd(%rip),%rsi
lea    0xe0f(%rip),%rdx
mov    $0xd8,%ecx
mov    $0x0,%al
call   1050 <fdprintf@plt>
mov    $0x1,%edi
mov    $0x0,%al
call   1060 <cgc_exit@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1080 <read_n_bytes@plt>
cmp    -0x10(%rbp),%eax
je     125a <read_command+0xfa>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1262 <read_command+0x102>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_command>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
xor    %ebx,%ebx
lea    0xc(%rsp),%rdx
mov    $0x4,%esi
xor    %eax,%eax
call   1070 <read_n_bytes@plt>
test   %eax,%eax
js     11f9 <read_command+0xa9>
mov    0xc(%rsp),%ebp
cmp    $0x74697571,%ebp
jne    118f <read_command+0x3f>
xor    %edi,%edi
xor    %eax,%eax
call   1050 <cgc_exit@plt>
mov    0xc(%rsp),%ebp
cmp    $0x4000,%ebp
ja     11f7 <read_command+0xa7>
mov    %ebp,%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11da <read_command+0x8a>
lea    0xe4e(%rip),%rsi
lea    0xe60(%rip),%rdx
mov    $0x2,%edi
mov    $0xd8,%ecx
xor    %eax,%eax
call   1040 <fdprintf@plt>
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <cgc_exit@plt>
mov    0xc(%rsp),%ebp
mov    %r14d,%edi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <read_n_bytes@plt>
cmp    0xc(%rsp),%eax
je     11f9 <read_command+0xa9>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"quit\00"
@.str.1 = [25 x i8] c"ERROR %s:%d:\09bad calloc\0A\00"
@.str.2 = [11 x i8] c"src/main.c\00"
define i8* @read_command(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = bitcast i32* %4 to i8*
%9 = call i32 (i32, i64, i8*, ...) bitcast (i32 (...)* @read_n_bytes to i32 (i32, i64, i8*, ...)*)(i32 %7, i64 4, i8* %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* null, i8** %2
br label %46
12: ; preds = %1
%13 = bitcast i32* %4 to i8*
%14 = call i32 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %13, i64 4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 (i32, ...) bitcast (i32 (...)* @cgc_exit to i32 (i32, ...)*)(i32 0)
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 16384
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21, %18
store i8* null, i8** %2
br label %46
25: ; preds = %21
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = call i8* @calloc(i64 1, i64 %27)
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = icmp ne i8* %29, null
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = call i32 (i32, i8*, i8*, i32, ...) bitcast (i32 (...)* @fdprintf to i32 (i32, i8*, i8*, i32, ...)*)(i32 2, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 216)
%33 = call i32 (i32, ...) bitcast (i32 (...)* @cgc_exit to i32 (i32, ...)*)(i32 1)
store i32 %33, i32* %6
%34 = load i32, i32* %6
br label %35
35: ; preds = %31, %25
%36 = load i32, i32* %3
%37 = load i32, i32* %4
%38 = load i8*, i8** %5
%39 = call i32 (i32, i32, i8*, ...) bitcast (i32 (...)* @read_n_bytes to i32 (i32, i32, i8*, ...)*)(i32 %36, i32 %37, i8* %38)
%40 = load i32, i32* %4
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = load i8*, i8** %5
store i8* null, i8** %2
br label %46
44: ; preds = %35
%45 = load i8*, i8** %5
store i8* %45, i8** %2
br label %46
46: ; preds = %44, %42, %24, %11
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c"ERROR %s:%d:\09bad calloc\0A\00"
@.str.2 = [11 x i8] c"src/main.c\00"
define i8* @read_command(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 (i32, i64, i8*, ...) bitcast (i32 (...)* @read_n_bytes to i32 (i32, i64, i8*, ...)*)(i32 %0, i64 4, i8* %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %29, label %6
6: ; preds = %1
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, 1953068401
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = call i32 (i32, ...) bitcast (i32 (...)* @cgc_exit to i32 (i32, ...)*)(i32 0)
%11 = load i32, i32* %2
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ %11, %9 ], [ %7, %6 ]
%14 = icmp ugt i32 %13, 16384
br i1 %14, label %29, label %15
15: ; preds = %12
%16 = zext i32 %13 to i64
%17 = call i8* @calloc(i64 1, i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = call i32 (i32, i8*, i8*, i32, ...) bitcast (i32 (...)* @fdprintf to i32 (i32, i8*, i8*, i32, ...)*)(i32 2, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 216)
%21 = call i32 (i32, ...) bitcast (i32 (...)* @cgc_exit to i32 (i32, ...)*)(i32 1)
%22 = load i32, i32* %2
br label %23
23: ; preds = %19, %15
%24 = phi i32 [ %22, %19 ], [ %13, %15 ]
%25 = call i32 (i32, i32, i8*, ...) bitcast (i32 (...)* @read_n_bytes to i32 (i32, i32, i8*, ...)*)(i32 %0, i32 %24, i8* %17)
%26 = load i32, i32* %2
%27 = icmp eq i32 %25, %26
br i1 %27, label %29, label %28
28: ; preds = %23
br label %29
29: ; preds = %28, %23, %12, %1
%30 = phi i8* [ null, %1 ], [ null, %12 ], [ null, %28 ], [ %17, %23 ]
ret i8* %30
}