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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xea4(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%r15
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1060 <assert@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   1060 <assert@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   1060 <assert@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
pop    %rbx
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
@errno = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i32 0, i32* %1
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %4
%6 = load i8*, i8** %4
%7 = call i32 @strlen(i8* %6)
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 1
store i64 %9, i64* %5
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, -1
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* %2
%17 = load i8*, i8** %4
%18 = load i64, i64* %5
%19 = call i32 @write(i32 %16, i8* %17, i64 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* %2
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = call i32 @write(i32 %24, i8* %25, i64 %26)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp eq i32 %28, -1
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* @errno
ret i32 %34
}

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
@errno = global i32 0
define i32 @main() {
%1 = tail call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%2 = sext i32 %1 to i64
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = icmp ne i32 %4, -1
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %2)
%9 = icmp eq i32 %8, -1
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %2)
%13 = icmp eq i32 %12, -1
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @close(i32 %4)
%17 = load i32, i32* @errno
ret i32 %17
}