#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 atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(char *, ...);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ syserr(char *);
int main(int argc, char *argv[]) {
  int read_dsc, buf_len;
  char buf[1024];
  if (argc != 2)
    fatal("Usage: %s <read_fd>\n", argv[0]);
  read_dsc = atoi(argv[1]);
  printf("Reading data from descriptor %d\n", read_dsc);
  if ((buf_len = read(read_dsc, buf, 1024 - 1)) == -1)
    syserr("Error in read\n");
  ;
  buf[buf_len < 1024 - 1 ? buf_len : 1024 - 1] = '\0';
  if (buf_len == 0)
    fatal("Unexpected end-of-file\n");
  else
    printf("Read %d byte(s): \"%s\"\n", buf_len, buf);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1198 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11e5 <main+0x85>
lea    0xe56(%rip),%rdi
call   1040 <syserr@plt>
cmpl   $0x3ff,-0x18(%rbp)
jge    1200 <main+0xa0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x424(%rbp)
jmp    1210 <main+0xb0>
mov    $0x3ff,%eax
mov    %eax,-0x424(%rbp)
jmp    1210 <main+0xb0>
mov    -0x424(%rbp),%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
cmpl   $0x0,-0x18(%rbp)
jne    123d <main+0xdd>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
jmp    1255 <main+0xf5>
mov    -0x18(%rbp),%esi
lea    -0x420(%rbp),%rdx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     1182 <main+0x22>
mov    (%rbx),%rsi
lea    0xe85(%rip),%rdi
xor    %eax,%eax
call   1080 <fatal@plt>
mov    0x8(%rbx),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
lea    0xe81(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rsi
mov    $0x3ff,%ebp
mov    %ebx,%edi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11cb <main+0x6b>
lea    0xe77(%rip),%rdi
call   1040 <syserr@plt>
movb   $0x0,-0x1(%rsp)
jmp    11f4 <main+0x94>
cmp    $0x3ff,%ebx
cmovl  %ebx,%ebp
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
test   %ebx,%ebx
jne    11f4 <main+0x94>
lea    0xe5f(%rip),%rdi
xor    %eax,%eax
call   1080 <fatal@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe62(%rip),%rdi
mov    %rsp,%rdx
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Usage: %s <read_fd>\0A\00"
@.str.1 = [33 x i8] c"Reading data from descriptor %d\0A\00"
@.str.2 = [15 x i8] c"Error in read\0A\00"
@.str.3 = [24 x i8] c"Unexpected end-of-file\0A\00"
@.str.4 = [23 x i8] c"Read %d byte(s): \22%s\22\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %11, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %6
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 1023)
store i32 %25, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %16
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 1023
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i32, i32* %7
br label %35
34: ; preds = %29
br label %35
35: ; preds = %34, %32
%36 = phi i32 [ %33, %32 ], [ 1023, %34 ]
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %47
43: ; preds = %35
%44 = load i32, i32* %7
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %44, i8* %45)
br label %47
47: ; preds = %43, %41
%48 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [21 x i8] c"Usage: %s <read_fd>\0A\00"
@.str.1 = [33 x i8] c"Reading data from descriptor %d\0A\00"
@.str.2 = [15 x i8] c"Error in read\0A\00"
@.str.3 = [24 x i8] c"Unexpected end-of-file\0A\00"
@.str.4 = [23 x i8] c"Read %d byte(s): \22%s\22\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %9
9: ; preds = %6, %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = call i32 @read(i32 %12, i8* %4, i32 1023)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = tail call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 -1
store i8 0, i8* %18
br label %27
19: ; preds = %9
%20 = icmp slt i32 %14, 1023
%21 = select i1 %20, i32 %14, i32 1023
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
%24 = icmp eq i32 %14, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %29
27: ; preds = %16, %19
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %14, i8* %4)
br label %29
29: ; preds = %27, %25
%30 = call i32 @exit(i32 0)
unreachable
}