#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(void) {
  int fd = open("fileio-open.temp", O_CREAT | O_RDWR, 0600);
  if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  };
  write(fd, "Hello      \n", strlen("Hello      \n"));
  lseek(fd, -6, SEEK_CUR);
  write(fd, "world", strlen("world"));
  close(fd);
  fd = open("fileio-open.temp", O_RDONLY);
  if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  };
  int done = 0;
  while (!done) {
    char c;
    int ret = read(fd, &c, 1);
    if (ret == -1)
      perror("read");
    else if (ret == 0)
      done = 1;
    else
      printf("%c", c);
  }
  close(fd);
  unlink("fileio-open.temp");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe38(%rip),%rdi
mov    $0x180,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11fb <main+0x5b>
lea    0xe29(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe0e(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xdfd(%rip),%rsi
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffa,%esi
call   1070 <lseek@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    0xde2(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xdd1(%rip),%rsi
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    129d <main+0xfd>
lea    0xd87(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12ba <main+0x11a>
jmp    1319 <main+0x179>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12e9 <main+0x149>
lea    0xd4a(%rip),%rdi
call   10b0 <perror@plt>
jmp    1314 <main+0x174>
cmpl   $0x0,-0x14(%rbp)
jne    12ff <main+0x15f>
movl   $0x1,-0xc(%rbp)
jmp    130f <main+0x16f>
lea    0xd28(%rip),%rdi
movsbl -0xd(%rbp),%esi
call   1060 <printf@plt>
jmp    1314 <main+0x174>
jmp    12a4 <main+0x104>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
lea    0xcd8(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2c84(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e49(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3f(%rip),%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12bf <main+0x11f>
mov    %eax,%ebx
lea    0xe37(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0xfffffffa,%esi
call   1070 <lseek@plt>
lea    0xe14(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    0xdc6(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12bf <main+0x11f>
mov    %eax,%ebx
lea    0x7(%rsp),%r14
lea    0xdd5(%rip),%r15
lea    0xdd3(%rip),%r12
jmp    1270 <main+0xd0>
mov    %r15,%rdi
call   10b0 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     125d <main+0xbd>
test   %eax,%eax
je     1297 <main+0xf7>
movsbl 0x7(%rsp),%esi
mov    %r12,%rdi
call   1060 <printf@plt>
jmp    1270 <main+0xd0>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xd5b(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd4b(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"fileio-open.temp\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"Hello \0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [6 x i8] c"world\00"
@O_RDONLY = global i32 0
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [3 x i8] c"%c\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %6, %7
%9 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8, i32 384)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = load i32, i32* %2
%18 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @write(i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %2
%21 = load i32, i32* @SEEK_CUR
%22 = call i32 @lseek(i32 %20, i32 -6, i32 %21)
%23 = load i32, i32* %2
%24 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @write(i32 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @O_RDONLY
%29 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %28)
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %16
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %16
store i32 0, i32* %3
br label %37
37: ; preds = %56, %36
%38 = load i32, i32* %3
%39 = icmp ne i32 %38, 0
%40 = xor i1 %39, true
br i1 %40, label %41, label %57
41: ; preds = %37
%42 = load i32, i32* %2
%43 = call i32 @read(i32 %42, i8* %4, i32 1)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %56
48: ; preds = %41
%49 = load i32, i32* %5
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
store i32 1, i32* %3
br label %55
52: ; preds = %48
%53 = load i8, i8* %4
%54 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8 signext %53)
br label %55
55: ; preds = %52, %51
br label %56
56: ; preds = %55, %46
br label %37
57: ; preds = %37
%58 = load i32, i32* %2
%59 = call i32 @close(i32 %58)
%60 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%61 = load i32, i32* @EXIT_SUCCESS
ret i32 %61
}

LLVM IR (-O2):
@.str = [17 x i8] c"fileio-open.temp\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"Hello \0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [6 x i8] c"world\00"
@O_RDONLY = global i32 0
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [3 x i8] c"%c\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_RDWR
%4 = or i32 %3, %2
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4, i32 384)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @SEEK_CUR
%15 = tail call i32 @lseek(i32 %5, i32 -6, i32 %14)
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = tail call i32 @close(i32 %5)
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %11
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %11, %33
%27 = call i32 @read(i32 %20, i8* %1, i32 1)
switch i32 %27, label %30 [
i32 -1, label %28
i32 0, label %34
]
28: ; preds = %26
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %33
30: ; preds = %26
%31 = load i8, i8* %1
%32 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8 signext %31)
br label %33
33: ; preds = %30, %28
br label %26
34: ; preds = %26
%35 = tail call i32 @close(i32 %20)
%36 = tail call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @EXIT_SUCCESS
ret i32 %37
}