#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_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int errno;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
int strlen(char *);
int write(int, char *, size_t);
int main() {
  int fd, err;
  char buf[100];
  char *str = "sample string";
  size_t len = strlen(str) * sizeof(char);
  fd = open("opentest1.txt", O_RDWR);
  err = write(fd, str, len);
  assert(err == len);
  err = lseek(fd, SEEK_SET, 0);
  err = read(fd, buf, len);
  assert(err == -1);
  err = lseek(fd, SEEK_SET, 0);
  err = read(fd, buf, len);
  assert(err == -1);
  close(fd);
  return errno;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %rax,-0x80(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    -0x80(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    -0x80(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lseek@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x80(%rbp),%rdx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x8(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lseek@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x80(%rbp),%rdx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
lea    0xe7e(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
xor    %edi,%edi
cmp    %r14d,%eax
sete   %dil
call   1080 <assert@plt>
mov    0x2e0a(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <lseek@plt>
mov    %rsp,%r15
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <read@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   1080 <assert@plt>
mov    (%r12),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <read@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   1080 <assert@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"sample string\00"
@.str.1 = [14 x i8] c"opentest1.txt\00"
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i8*
%6 = alloca i64
store i32 0, i32* %1
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = load i8*, i8** %5
%8 = call i32 @strlen(i8* %7)
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 1
store i64 %10, i64* %6
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = call i32 @write(i32 %13, i8* %14, i64 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = load i64, i64* %6
%20 = icmp eq i64 %18, %19
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32, i32* %2
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %3
%26 = load i32, i32* %2
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%28 = load i64, i64* %6
%29 = call i32 @read(i32 %26, i8* %27, i64 %28)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, -1
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %2
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %34, i32 %35, i32 0)
store i32 %36, i32* %3
%37 = load i32, i32* %2
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%39 = load i64, i64* %6
%40 = call i32 @read(i32 %37, i8* %38, i64 %39)
store i32 %40, i32* %3
%41 = load i32, i32* %3
%42 = icmp eq i32 %41, -1
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* @errno
ret i32 %47
}

LLVM IR (-O2):
@.str = [14 x i8] c"sample string\00"
@.str.1 = [14 x i8] c"opentest1.txt\00"
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = sext i32 %3 to i64
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %4)
%8 = icmp eq i32 %7, %3
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %6, i32 %11, i32 0)
%13 = call i32 @read(i32 %6, i8* %2, i64 %4)
%14 = icmp eq i32 %13, -1
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @lseek(i32 %6, i32 %17, i32 0)
%19 = call i32 @read(i32 %6, i8* %2, i64 %4)
%20 = icmp eq i32 %19, -1
%21 = zext i1 %20 to i32
%22 = tail call i32 @assert(i32 %21)
%23 = tail call i32 @close(i32 %6)
%24 = load i32, i32* @errno
ret i32 %24
}