#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int myread(int fd) {
  char buf[1024];
  int ret, n = 0;
  while (1) {
    ret = read(fd, buf, sizeof(buf));
    if (ret < 0) {
      if (errno == EINTR) {
        continue;
      }
      break;
    }
    if (ret == 0) {
      break;
    }
    buf[ret] = 0;
    printf("%s\n", buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<myread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x418(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    1183 <myread+0x63>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
jne    117e <myread+0x5e>
jmp    1138 <myread+0x18>
jmp    11bc <myread+0x9c>
cmpl   $0x0,-0x414(%rbp)
jne    1195 <myread+0x75>
jmp    11bc <myread+0x9c>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe4e(%rip),%rdi
call   1030 <printf@plt>
jmp    1138 <myread+0x18>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myread>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    0x2ea4(%rip),%r12
mov    0x2e95(%rip),%r13
lea    0xeb6(%rip),%r15
jmp    115a <myread+0x3a>
nopl   0x0(%rax)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1182 <myread+0x62>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1150 <myread+0x30>
je     1182 <myread+0x62>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
jmp    115a <myread+0x3a>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @myread(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
br label %6
6: ; preds = %1, %16, %22
%7 = load i32, i32* %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 1024)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %12
br label %6
17: ; preds = %12
br label %28
18: ; preds = %6
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
br label %28
22: ; preds = %18
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%27 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %6
28: ; preds = %21, %17
ret i32 0
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @myread(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %17, %1
%5 = call i32 @read(i32 %0, i8* %3, i32 1024)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
br i1 %10, label %17, label %18
11: ; preds = %4
%12 = icmp eq i32 %5, 0
br i1 %12, label %18, label %13
13: ; preds = %11
%14 = zext i32 %5 to i64
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %17
17: ; preds = %13, %7
br label %4
18: ; preds = %11, %7
ret i32 0
}