#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strcmp(char *, char *);
char *strerrno(int);
int strlen(char *);
int test_read() {
  int fd, ret;
  char buf[16];
  errno = 0;
  fd = open("foo.fileio.test", O_RDONLY);
  if (fd >= 0) {
    memset(buf, 0, 16);
    errno = 0;
    ret = read(fd, buf, 16);
    buf[15] = '\0';
    if (ret == strlen("Hello World"))
      printf("read 1: %s %s\n", buf, !strcmp(buf, "Hello World") ? "OK" : "");
    else
      printf("read 1: ret = %d, errno = %d\n", ret, errno);
    close(fd);
  } else
    printf("read 1: ret = %d, errno = %d\n", ret, errno);
  errno = 0;
  ret = read(999, buf, 16);
  printf("read 2: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
}


x86 assembly (clang, -O0):
<test_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e49(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     127f <test_read+0xff>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e0a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1080 <read@plt>
mov    %eax,-0xc(%rbp)
movb   $0x0,-0x11(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xe17(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jne    1258 <test_read+0xd8>
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   1090 <strcmp@plt>
mov    -0x30(%rbp),%rsi
cmp    $0x0,%eax
setne  %cl
xor    $0xff,%cl
lea    0xe14(%rip),%rdx
lea    0xded(%rip),%rax
test   $0x1,%cl
cmovne %rax,%rdx
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1272 <test_read+0xf2>
mov    -0xc(%rbp),%esi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    1299 <test_read+0x119>
mov    -0xc(%rbp),%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d38(%rip),%rax
movl   $0x0,(%rax)
lea    -0x20(%rbp),%rsi
mov    $0x3e7,%edi
mov    $0x10,%edx
call   1080 <read@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e59(%rip),%r14
movl   $0x0,(%r14)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1225 <test_read+0xb5>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
movl   $0x0,(%r14)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1080 <read@plt>
mov    %eax,%ebp
movb   $0x0,0xf(%rsp)
lea    0xe35(%rip),%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jne    1238 <test_read+0xc8>
movabs $0x6f57206f6c6c6548,%rax
20 57 6f
xor    (%rsp),%rax
mov    0x8(%rsp),%ecx
xor    $0x646c72,%rcx
or     %rax,%rcx
lea    0xe24(%rip),%rax
lea    0xe3d(%rip),%rdx
cmove  %rax,%rdx
lea    0xe03(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    124b <test_read+0xdb>
mov    (%r14),%edx
lea    0xdff(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1252 <test_read+0xe2>
mov    (%r14),%edx
lea    0xdec(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
movl   $0x0,(%r14)
mov    %rsp,%rsi
mov    $0x3e7,%edi
mov    $0x10,%edx
call   1080 <read@plt>
mov    %eax,%ebx
mov    (%r14),%ebp
mov    %ebp,%edi
call   1030 <strerrno@plt>
lea    0xdce(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"Hello World\00"
@.str.2 = [15 x i8] c"read 1: %s %s\0A\00"
@.str.3 = [3 x i8] c"OK\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [30 x i8] c"read 1: ret = %d, errno = %d\0A\00"
@.str.6 = [33 x i8] c"read 2: ret = %d, errno = %d %s\0A\00"
define i32 @test_read() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
store i32 0, i32* @errno
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %35
9: ; preds = %0
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 0, i32 16)
store i32 0, i32* @errno
%12 = load i32, i32* %2
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 16)
store i32 %14, i32* %3
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 15
store i8 0, i8* %15
%16 = load i32, i32* %3
%17 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %28
19: ; preds = %9
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%22 = call i32 @strcmp(i8* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp ne i32 %22, 0
%24 = xor i1 %23, true
%25 = zext i1 %24 to i64
%26 = select i1 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* %26)
br label %32
28: ; preds = %9
%29 = load i32, i32* %3
%30 = load i32, i32* @errno
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %29, i32 %30)
br label %32
32: ; preds = %28, %19
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
br label %39
35: ; preds = %0
%36 = load i32, i32* %3
%37 = load i32, i32* @errno
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %37)
br label %39
39: ; preds = %35, %32
store i32 0, i32* @errno
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%41 = call i32 @read(i32 999, i8* %40, i32 16)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = load i32, i32* @errno
%44 = load i32, i32* @errno
%45 = call i8* @strerrno(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %42, i32 %43, i8* %45)
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"Hello World\00"
@.str.2 = [15 x i8] c"read 1: %s %s\0A\00"
@.str.3 = [3 x i8] c"OK\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [30 x i8] c"read 1: ret = %d, errno = %d\0A\00"
@.str.6 = [33 x i8] c"read 2: ret = %d, errno = %d %s\0A\00"
define i32 @test_read() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
store i32 0, i32* @errno
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %22
6: ; preds = %0
%7 = call i32 @memset(i8* %2, i32 0, i32 16)
store i32 0, i32* @errno
%8 = call i32 @read(i32 %4, i8* %2, i32 16)
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 15
store i8 0, i8* %9
%10 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32 %8, %10
br i1 %11, label %12, label %17
12: ; preds = %6
%13 = call i32 @bcmp(i8* dereferenceable(12) %2, i8* dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 12)
%14 = icmp eq i32 %13, 0
%15 = select i1 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %15)
br label %20
17: ; preds = %6
%18 = load i32, i32* @errno
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %8, i32 %18)
br label %20
20: ; preds = %17, %12
%21 = call i32 @close(i32 %4)
br label %25
22: ; preds = %0
%23 = load i32, i32* @errno
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 undef, i32 %23)
br label %25
25: ; preds = %22, %20
store i32 0, i32* @errno
%26 = call i32 @read(i32 999, i8* %2, i32 16)
%27 = load i32, i32* @errno
%28 = call i8* @strerrno(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %26, i32 %27, i8* %28)
ret i32 undef
}