#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 O_TRUNC;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int open(char const *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int const);
int strlen(char const *);
int write(int, char const *, int);
int main() {
  int fd;
  int n;
  const char filename[] = "blah";
  const char bufw[] = "abcde";
  const int rd_bytes = 3;
  char bufr[rd_bytes];
  assert(rd_bytes <= strlen(bufw));
  fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0644);
  if (fd == -1) {
    printf("failed to open %s\n", filename);
    return 1;
  }
  n = write(fd, bufw, strlen(bufw));
  if (n != strlen(bufw)) {
    printf("failed to write to %s\n", filename);
    close(fd);
    return 2;
  }
  close(fd);
  fd = open(filename, O_RDWR);
  if (fd == -1) {
    printf("failed to open %s\n", filename);
    return 3;
  }
  n = read(fd, bufr, rd_bytes);
  if (n != rd_bytes) {
    printf("failed to read from %s\n", filename);
    close(fd);
    return 4;
  }
  bufr[n] = 0;
  printf("%d bytes read: %s\n", n, bufr);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7b(%rip),%eax
mov    %eax,-0x11(%rbp)
mov    0xe76(%rip),%al
mov    %al,-0xd(%rbp)
mov    0xe6e(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xe68(%rip),%ax
mov    %ax,-0x13(%rbp)
movl   $0x3,-0x1c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
lea    -0x17(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    %ecx,%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
lea    -0x11(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    122f <main+0xbf>
lea    -0x11(%rbp),%rsi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    136d <main+0x1fd>
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x17(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x17(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x17(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
je     12a0 <main+0x130>
lea    -0x11(%rbp),%rsi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    136d <main+0x1fd>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    -0x11(%rbp),%rdi
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12ee <main+0x17e>
lea    -0x11(%rbp),%rsi
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x3,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    136d <main+0x1fd>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x3,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
je     1339 <main+0x1c9>
lea    -0x11(%rbp),%rsi
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x4,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    136d <main+0x1fd>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
mov    -0xc(%rbp),%esi
lea    0xd01(%rip),%rdi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsp,%r15
movb   $0x0,0xc(%rsp)
movl   $0x68616c62,0x8(%rsp)
68
movw   $0x65,0x1c(%rsp)
movl   $0x64636261,0x18(%rsp)
64
lea    0x18(%rsp),%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
cmp    $0x3,%eax
setge  %dil
call   1080 <assert@plt>
mov    0x2e3e(%rip),%rax
mov    0x2e17(%rip),%r12
mov    (%r12),%esi
or     (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     126f <main+0xff>
mov    %eax,%ebx
lea    0x18(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jne    1289 <main+0x119>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%r12),%esi
lea    0x8(%rsp),%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12aa <main+0x13a>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1070 <read@plt>
cmp    $0x3,%eax
jne    12c4 <main+0x154>
movb   $0x0,0x13(%rsp)
lea    0xdfa(%rip),%rdi
xor    %ebp,%ebp
lea    0x10(%rsp),%rdx
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    12e3 <main+0x173>
lea    0xd95(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    12e3 <main+0x173>
lea    0xd8e(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x2,%ebp
jmp    12e3 <main+0x173>
lea    0xd5a(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%ebp
jmp    12e3 <main+0x173>
lea    0xd6a(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x4,%ebp
mov    %r15,%rsp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.filename = [5 x i8] c"blah\00"
@__const.main.bufw = [6 x i8] c"abcde\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str = [19 x i8] c"failed to open %s\0A\00"
@.str.1 = [23 x i8] c"failed to write to %s\0A\00"
@.str.2 = [24 x i8] c"failed to read from %s\0A\00"
@.str.3 = [19 x i8] c"%d bytes read: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [5 x i8]
%5 = alloca [6 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %1
%9 = bitcast [5 x i8]* %4 to i8*
%10 = bitcast [6 x i8]* %5 to i8*
store i32 3, i32* %6
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %7
%12 = alloca i8, i64 3
%13 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = icmp sle i32 3, %14
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %19, %20
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %23, i32 420)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %0
%28 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %28)
store i32 1, i32* %1
store i32 1, i32* %8
br label %74
30: ; preds = %0
%31 = load i32, i32* %2
%32 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = icmp ne i32 %36, %38
br i1 %39, label %40, label %45
40: ; preds = %30
%41 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
store i32 2, i32* %1
store i32 1, i32* %8
br label %74
45: ; preds = %30
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%49 = load i32, i32* @O_RDWR
%50 = call i32 (i8*, i32, ...) @open(i8* %48, i32 %49)
store i32 %50, i32* %2
%51 = load i32, i32* %2
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %45
%54 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %54)
store i32 3, i32* %1
store i32 1, i32* %8
br label %74
56: ; preds = %45
%57 = load i32, i32* %2
%58 = call i32 @read(i32 %57, i8* %12, i32 3)
store i32 %58, i32* %3
%59 = load i32, i32* %3
%60 = icmp ne i32 %59, 3
br i1 %60, label %61, label %66
61: ; preds = %56
%62 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %62)
%64 = load i32, i32* %2
%65 = call i32 @close(i32 %64)
store i32 4, i32* %1
store i32 1, i32* %8
br label %74
66: ; preds = %56
%67 = load i32, i32* %3
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %12, i64 %68
store i8 0, i8* %69
%70 = load i32, i32* %3
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %70, i8* %12)
%72 = load i32, i32* %2
%73 = call i32 @close(i32 %72)
store i32 0, i32* %1
store i32 1, i32* %8
br label %74
74: ; preds = %66, %61, %53, %40, %27
%75 = load i8*, i8** %7
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
@__const.main.filename = [5 x i8] c"blah\00"
@__const.main.bufw = [6 x i8] c"abcde\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str = [19 x i8] c"failed to open %s\0A\00"
@.str.1 = [23 x i8] c"failed to write to %s\0A\00"
@.str.2 = [24 x i8] c"failed to read from %s\0A\00"
@.str.3 = [19 x i8] c"%d bytes read: %s\0A\00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = alloca [6 x i8]
%3 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%5 = tail call i8* @llvm.stacksave()
%6 = alloca [3 x i8]
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%8 = call i32 @strlen(i8* %4)
%9 = icmp sgt i32 %8, 2
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_RDWR
%14 = or i32 %13, %12
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = call i32 (i8*, i32, ...) @open(i8* %3, i32 %16, i32 420)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %46
21: ; preds = %0
%22 = call i32 @strlen(i8* %4)
%23 = call i32 @write(i32 %17, i8* %4, i32 %22)
%24 = call i32 @strlen(i8* %4)
%25 = icmp eq i32 %23, %24
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%28 = call i32 @close(i32 %17)
br label %46
29: ; preds = %21
%30 = call i32 @close(i32 %17)
%31 = load i32, i32* @O_RDWR
%32 = call i32 (i8*, i32, ...) @open(i8* %3, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %46
36: ; preds = %29
%37 = call i32 @read(i32 %32, i8* %7, i32 3)
%38 = icmp eq i32 %37, 3
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%41 = call i32 @close(i32 %32)
br label %46
42: ; preds = %36
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 3
store i8 0, i8* %43
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 3, i8* %7)
%45 = call i32 @close(i32 %32)
br label %46
46: ; preds = %42, %39, %34, %26, %19
%47 = phi i32 [ 1, %19 ], [ 2, %26 ], [ 3, %34 ], [ 4, %39 ], [ 0, %42 ]
ret i32 %47
}