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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, ...);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
scalar_t__ read(int, char *, int);
int main(int argc, char *arvg[]) {
  int devfd = open("/dev/host-zero", O_RDONLY);
  if (devfd < 0)
    err(1, "/dev/host-zero open");
  off_t offset;
  offset = lseek(devfd, 0, SEEK_SET);
  if (offset < 0)
    err(1, "/dev/host-zero lseek(0, SEEK_SET)");
  if (offset > 0)
    errx(1, "/dev/host-zero lseek(0, SEEK_SET) didn't return 0 (returned: %ld)",
         offset);
  char buf = 'A';
  ssize_t bytes = read(devfd, &buf, sizeof(buf));
  if (bytes < 0)
    err(1, "/dev/host-zero read");
  if (buf != '\0')
    errx(1, "read from /dev/host-zero didn't return NUL byte");
  int ret = close(devfd);
  if (ret < 0)
    err(1, "/dev/host-zero close");
  puts("TEST OK");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b9 <main+0x49>
mov    $0x1,%edi
lea    0xe5b(%rip),%rsi
call   1080 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11ec <main+0x7c>
mov    $0x1,%edi
lea    0xe3c(%rip),%rsi
call   1080 <err@plt>
cmpq   $0x0,-0x20(%rbp)
jle    120e <main+0x9e>
mov    -0x20(%rbp),%rdx
mov    $0x1,%edi
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
movb   $0x41,-0x21(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1243 <main+0xd3>
mov    $0x1,%edi
lea    0xe49(%rip),%rsi
call   1080 <err@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x0,%eax
je     1263 <main+0xf3>
mov    $0x1,%edi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1289 <main+0x119>
mov    $0x1,%edi
lea    0xe47(%rip),%rsi
call   1080 <err@plt>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a1 <main+0x31>
lea    0xe78(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   %rax,%rax
js     11d2 <main+0x62>
je     11e3 <main+0x73>
lea    0xe84(%rip),%rsi
mov    $0x1,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <errx@plt>
jmp    11e3 <main+0x73>
lea    0xe4a(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
movb   $0x41,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
js     1248 <main+0xd8>
cmpb   $0x0,0xf(%rsp)
je     1218 <main+0xa8>
lea    0xe8f(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <errx@plt>
mov    %ebx,%edi
call   1060 <close@plt>
test   %eax,%eax
jns    1234 <main+0xc4>
lea    0xea1(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe38(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
cmpb   $0x0,0xf(%rsp)
jne    1205 <main+0x95>
jmp    1218 <main+0xa8>

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/host-zero\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"/dev/host-zero open\00"
@SEEK_SET = global i32 0
@.str.2 = [34 x i8] c"/dev/host-zero lseek(0, SEEK_SET)\00"
@.str.3 = [66 x i8] c"/dev/host-zero lseek(0, SEEK_SET) didn't return 0 (returned: %ld)\00"
@.str.4 = [20 x i8] c"/dev/host-zero read\00"
@.str.5 = [48 x i8] c"read from /dev/host-zero didn't return NUL byte\00"
@.str.6 = [21 x i8] c"/dev/host-zero close\00"
@.str.7 = [8 x i8] c"TEST OK\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* %6
%19 = load i32, i32* @SEEK_SET
%20 = call i64 @lseek(i32 %18, i32 0, i32 %19)
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = load i64, i64* %7
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i64, i64* %7
%30 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i64 %29)
br label %31
31: ; preds = %28, %25
store i8 65, i8* %8
%32 = load i32, i32* %6
%33 = call i64 @read(i32 %32, i8* %8, i32 1)
store i64 %33, i64* %9
%34 = load i64, i64* %9
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %31
%39 = load i8, i8* %8
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %49, %44
%52 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/host-zero\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"/dev/host-zero open\00"
@SEEK_SET = global i32 0
@.str.2 = [34 x i8] c"/dev/host-zero lseek(0, SEEK_SET)\00"
@.str.3 = [66 x i8] c"/dev/host-zero lseek(0, SEEK_SET) didn't return 0 (returned: %ld)\00"
@.str.4 = [20 x i8] c"/dev/host-zero read\00"
@.str.5 = [48 x i8] c"read from /dev/host-zero didn't return NUL byte\00"
@.str.6 = [21 x i8] c"/dev/host-zero close\00"
@.str.7 = [8 x i8] c"TEST OK\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* @SEEK_SET
%11 = tail call i64 @lseek(i32 %5, i32 0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %19
15: ; preds = %9
%16 = icmp eq i64 %11, 0
br i1 %16, label %19, label %17
17: ; preds = %15
%18 = tail call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i64 %11)
br label %19
19: ; preds = %13, %17, %15
store i8 65, i8* %3
%20 = call i64 @read(i32 %5, i8* %3, i32 1)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = load i8, i8* %3
%26 = icmp eq i8 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = tail call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = tail call i32 @close(i32 %5)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}