#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int STDOUT_FILENO;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int strlen(char const *);
size_t write(int, char const *, int);
int main() {
  char BUF[1 << 10];
  const char *filename = "file";
  const char *letters = "abcdefghijklmnopqrstuvwxyz";
  size_t letters_len = strlen(letters) + 1;
  int fd;
  if ((fd = open(filename, O_RDWR | O_CREAT, S_IRWXU)) < 0) {
    perror("open");
    exit(1);
  }
  if (write(fd, letters, letters_len) != letters_len) {
    perror("write");
    exit(1);
  }
  lseek(fd, 0, SEEK_SET);
  int n;
  if ((n = read(fd, BUF, 1 << 10)) < 0) {
    perror("read");
    exit(1);
  }
  write(STDOUT_FILENO, BUF, n);
  puts("\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rax
mov    %rax,-0x418(%rbp)
lea    0xe4e(%rip),%rax
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x428(%rbp)
mov    -0x418(%rbp),%rdi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2e0b(%rip),%rax
or     (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x42c(%rbp)
cmp    $0x0,%eax
jge    1222 <main+0x92>
lea    0xe0d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x42c(%rbp),%edi
mov    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%rax
mov    %eax,%edx
call   1040 <write@plt>
cmp    -0x428(%rbp),%rax
je     1260 <main+0xd0>
lea    0xdd4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x42c(%rbp),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    -0x42c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x430(%rbp)
cmp    $0x0,%eax
jge    12b2 <main+0x122>
lea    0xd88(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    -0x430(%rbp),%edx
call   1040 <write@plt>
lea    0xd5c(%rip),%rdi
call   1030 <puts@plt>
mov    -0x42c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
lea    0xe65(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    0x2e22(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
lea    0xe37(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1246 <main+0xb6>
mov    %eax,%ebx
add    $0x1,%ebp
movslq %ebp,%rbp
lea    0xe24(%rip),%rsi
mov    %eax,%edi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %rbp,%rax
jne    124f <main+0xbf>
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
js     1258 <main+0xc8>
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%edi
mov    %rsp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdd3(%rip),%rdi
jmp    125f <main+0xcf>
lea    0xdcf(%rip),%rdi
jmp    125f <main+0xcf>
lea    0xdcc(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"file\00"
@.str.1 = [27 x i8] c"abcdefghijklmnopqrstuvwxyz\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"write\00"
@SEEK_SET = global i32 0
@.str.4 = [5 x i8] c"read\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%4 = add nsw i32 %3, 1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRWXU
%9 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = sext i32 %4 to i64
%16 = tail call i64 @write(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%17 = icmp eq i64 %16, %15
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = load i32, i32* @SEEK_SET
%23 = tail call i32 @lseek(i32 %9, i32 0, i32 %22)
%24 = call i32 @read(i32 %9, i8* %2, i32 1024)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* @STDOUT_FILENO
%31 = call i64 @write(i32 %30, i8* %2, i32 %24)
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%33 = tail call i32 @close(i32 %9)
ret i32 0
}