#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 */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, long, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
int main(void) {
  int fd, len;
  char buf[20];
  if ((fd = open("tempfile", O_RDWR | O_CREAT | O_TRUNC, 0666)) < 0) {
    perror("open error");
    exit(-1);
  }
  unlink("tempfile");
  if (write(fd, "How are you?", 12) != 12) {
    perror("write error");
    exit(-1);
  }
  lseek(fd, 0L, SEEK_SET);
  if ((len = read(fd, buf, sizeof(buf))) <= 0) {
    perror("read error\n");
    exit(-1);
  } else
    buf[len] = '\0';
  printf("%s\n", buf);
  close(fd);
  if ((fd = open("tempfile", O_RDWR)) < 0) {
    perror("Second open error\n");
    exit(-1);
  } else
    close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
lea    0xe3f(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ef <main+0x5f>
lea    0xe29(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xe0a(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%edi
lea    0xe0f(%rip),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
cmp    $0xc,%eax
je     122e <main+0x9e>
lea    0xe02(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     1276 <main+0xe6>
lea    0xdc6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rsi
lea    0xdaf(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    12d0 <main+0x140>
lea    0xd7c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e3a(%rip),%r14
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
lea    0xe46(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     125b <main+0xcb>
mov    %eax,%ebx
lea    0xe29(%rip),%rdi
call   1030 <unlink@plt>
lea    0xe31(%rip),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1040 <write@plt>
cmp    $0xc,%eax
jne    1264 <main+0xd4>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x14,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    126d <main+0xdd>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xe13(%rip),%rdi
mov    %rsp,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    (%r14),%esi
lea    0xdc1(%rip),%rdi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1276 <main+0xe6>
mov    %eax,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xda7(%rip),%rdi
jmp    127d <main+0xed>
lea    0xdb6(%rip),%rdi
jmp    127d <main+0xed>
lea    0xdb9(%rip),%rdi
jmp    127d <main+0xed>
lea    0xdc0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"tempfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [13 x i8] c"How are you?\00"
@.str.3 = [12 x i8] c"write error\00"
@SEEK_SET = global i32 0
@.str.4 = [12 x i8] c"read error\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [19 x i8] c"Second open error\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [20 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9, i32 438)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %0
%16 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %2
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 12)
%19 = icmp ne i32 %18, 12
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %15
%24 = load i32, i32* %2
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @lseek(i32 %24, i64 0, i32 %25)
%27 = load i32, i32* %2
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 20)
store i32 %29, i32* %3
%30 = icmp sle i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %23
%35 = load i32, i32* %3
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %34
%39 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%40 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* @O_RDWR
%44 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %43)
store i32 %44, i32* %2
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %38
%50 = load i32, i32* %2
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"tempfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [13 x i8] c"How are you?\00"
@.str.3 = [12 x i8] c"write error\00"
@SEEK_SET = global i32 0
@.str.4 = [12 x i8] c"read error\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [19 x i8] c"Second open error\0A\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = tail call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 12)
%16 = icmp eq i32 %15, 12
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %8, i64 0, i32 %21)
%23 = call i32 @read(i32 %8, i8* %2, i32 20)
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i32 @exit(i32 -1)
unreachable
28: ; preds = %20
%29 = zext i32 %23 to i64
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%32 = call i32 @close(i32 %8)
%33 = load i32, i32* @O_RDWR
%34 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %28
%40 = call i32 @close(i32 %34)
ret i32 0
}