#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*/ mode_t;

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ EEXIST;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int memcmp(char *, char *, int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int read(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(scalar_t__);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int /*<<< orphan*/);
int generic_open_read_write(char *filename, int flag, mode_t mode) {
  int fd = 0;
  int ret = -1;
  char wstring[50] = {
      0,
  };
  char rstring[50] = {
      0,
  };
  fd = open(filename, flag, mode);
  if (fd < 0) {
    if (flag == (O_CREAT | O_EXCL) && errno == EEXIST) {
      unlink(filename);
      return 0;
    } else {
      fprintf(stderr, "open failed: %s\n", strerror(errno));
      return -1;
    }
  }
  strcpy(wstring, "My string to write\n");
  ret = write(fd, wstring, strlen(wstring));
  if (ret <= 0) {
    if (errno != EBADF) {
      fprintf(stderr, "write failed: %s\n", strerror(errno));
      close(fd);
      unlink(filename);
      return ret;
    }
  }
  ret = lseek(fd, 0, SEEK_SET);
  if (ret < 0) {
    close(fd);
    unlink(filename);
    return ret;
  }
  ret = read(fd, rstring, strlen(wstring));
  if (ret < 0 && flag != (O_CREAT | O_WRONLY) && flag != O_WRONLY &&
      flag != (O_TRUNC | O_WRONLY)) {
    close(fd);
    unlink(filename);
    return ret;
  }
  ret = memcmp(wstring, rstring, strlen(wstring));
  if (0 != ret && flag != (O_TRUNC | O_WRONLY) && flag != O_WRONLY &&
      flag != (O_CREAT | O_WRONLY) &&
      !(flag == (O_CREAT | O_RDONLY) || flag == O_RDONLY ||
        flag == (O_TRUNC | O_RDONLY))) {
    fprintf(stderr, "read is returning junk\n");
    close(fd);
    unlink(filename);
    return ret;
  }
  close(fd);
  unlink(filename);
  return 0;
}


x86 assembly (clang, -O0):
<generic_open_read_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1080 <memset@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1080 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   10d0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12af <generic_open_read_write+0xef>
mov    -0x14(%rbp),%eax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d83(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
jne    126e <generic_open_read_write+0xae>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rax
mov    0x2d78(%rip),%rcx
cmp    (%rcx),%rax
jne    126e <generic_open_read_write+0xae>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    157a <generic_open_read_write+0x3ba>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa4(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0xa4(%rbp),%edi
mov    %rax,%rdx
lea    0xd64(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    157a <generic_open_read_write+0x3ba>
lea    -0x60(%rbp),%rdi
lea    0xd57(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xb4(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0x60(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     136d <generic_open_read_write+0x1ad>
mov    0x2cac(%rip),%rax
mov    (%rax),%rax
mov    0x2c92(%rip),%rcx
cmp    (%rcx),%rax
je     1368 <generic_open_read_write+0x1a8>
mov    0x2ca2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xb8(%rbp)
mov    0x2c83(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0xb8(%rbp),%edi
mov    %rax,%rdx
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    157a <generic_open_read_write+0x3ba>
jmp    136d <generic_open_read_write+0x1ad>
mov    -0x1c(%rbp),%edi
mov    0x2c31(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    13a9 <generic_open_read_write+0x1e9>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    157a <generic_open_read_write+0x3ba>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xc4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0x60(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xc4(%rbp),%edi
mov    -0xc0(%rbp),%rsi
mov    %eax,%edx
call   10a0 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1452 <generic_open_read_write+0x292>
mov    -0x14(%rbp),%eax
mov    0x2c04(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2b93(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
je     1452 <generic_open_read_write+0x292>
mov    -0x14(%rbp),%eax
mov    0x2b7f(%rip),%rcx
cmp    (%rcx),%eax
je     1452 <generic_open_read_write+0x292>
mov    -0x14(%rbp),%eax
mov    0x2bcd(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2b64(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
je     1452 <generic_open_read_write+0x292>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    157a <generic_open_read_write+0x3ba>
lea    -0x60(%rbp),%rax
mov    %rax,-0xd8(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0x60(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xd8(%rbp),%rdi
mov    -0xd0(%rbp),%rsi
mov    %eax,%edx
call   10b0 <memcmp@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%eax
je     1562 <generic_open_read_write+0x3a2>
mov    -0x14(%rbp),%eax
mov    0x2b4f(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ae6(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
je     1562 <generic_open_read_write+0x3a2>
mov    -0x14(%rbp),%eax
mov    0x2ad2(%rip),%rcx
cmp    (%rcx),%eax
je     1562 <generic_open_read_write+0x3a2>
mov    -0x14(%rbp),%eax
mov    0x2b28(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ab7(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
je     1562 <generic_open_read_write+0x3a2>
mov    -0x14(%rbp),%eax
mov    0x2b0b(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ae2(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
je     1562 <generic_open_read_write+0x3a2>
mov    -0x14(%rbp),%eax
mov    0x2ace(%rip),%rcx
cmp    (%rcx),%eax
je     1562 <generic_open_read_write+0x3a2>
mov    -0x14(%rbp),%eax
mov    0x2ad4(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ab3(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
je     1562 <generic_open_read_write+0x3a2>
mov    0x2a8a(%rip),%rax
mov    (%rax),%edi
lea    0xaf8(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    157a <generic_open_read_write+0x3ba>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generic_open_read_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movw   $0x0,0x30(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movw   $0x0,0x70(%rsp)
call   10c0 <open@plt>
test   %eax,%eax
js     1317 <generic_open_read_write+0x167>
mov    %eax,%ebp
lea    0xe0d(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %eax,%eax
jg     1243 <generic_open_read_write+0x93>
mov    %eax,%ebx
mov    0x2d80(%rip),%rax
mov    (%rax),%rdi
mov    0x2d66(%rip),%rax
cmp    (%rax),%rdi
jne    13c2 <generic_open_read_write+0x212>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
test   %eax,%eax
js     1382 <generic_open_read_write+0x1d2>
mov    %rsp,%rdi
call   1060 <strlen@plt>
lea    0x40(%rsp),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1090 <read@plt>
test   %eax,%eax
js     1386 <generic_open_read_write+0x1d6>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x40(%rsp),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <memcmp@plt>
test   %eax,%eax
je     136f <generic_open_read_write+0x1bf>
mov    %eax,%ebx
mov    0x2cea(%rip),%rax
mov    (%rax),%ecx
cmp    %r15d,%ecx
je     136f <generic_open_read_write+0x1bf>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    %ecx,%edx
or     %eax,%edx
cmp    %r15d,%edx
je     136f <generic_open_read_write+0x1bf>
mov    0x2d2a(%rip),%rdx
mov    (%rdx),%edx
or     %edx,%ecx
cmp    %r15d,%ecx
je     136f <generic_open_read_write+0x1bf>
mov    0x2cf6(%rip),%rcx
mov    (%rcx),%ecx
cmp    %r15d,%ecx
je     136f <generic_open_read_write+0x1bf>
or     %ecx,%edx
cmp    %r15d,%edx
je     136f <generic_open_read_write+0x1bf>
or     %eax,%ecx
cmp    %r15d,%ecx
je     136f <generic_open_read_write+0x1bf>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
lea    0xd2c(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    13e5 <generic_open_read_write+0x235>
mov    0x2cda(%rip),%rax
mov    0x2c93(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2c80(%rip),%rax
mov    (%rax),%rdi
cmp    %r15d,%ecx
jne    1344 <generic_open_read_write+0x194>
mov    0x2c89(%rip),%rax
cmp    (%rax),%rdi
je     1376 <generic_open_read_write+0x1c6>
mov    0x2c75(%rip),%rax
mov    (%rax),%ebx
call   10d0 <strerror@plt>
lea    0xca7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    13f4 <generic_open_read_write+0x244>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %ebx,%ebx
jmp    13f4 <generic_open_read_write+0x244>
mov    %eax,%ebx
jmp    13e5 <generic_open_read_write+0x235>
mov    %eax,%ebx
mov    0x2c01(%rip),%rax
mov    (%rax),%eax
cmp    %r15d,%eax
je     127b <generic_open_read_write+0xcb>
mov    0x2c57(%rip),%rcx
mov    (%rcx),%ecx
or     %eax,%ecx
cmp    %r15d,%ecx
je     127b <generic_open_read_write+0xcb>
mov    0x2c3b(%rip),%rcx
or     (%rcx),%eax
cmp    %r15d,%eax
je     127b <generic_open_read_write+0xcb>
jmp    13e5 <generic_open_read_write+0x235>
mov    0x2bf7(%rip),%rax
mov    (%rax),%r15d
call   10d0 <strerror@plt>
lea    0xc4d(%rip),%rsi
mov    %r15d,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str = [17 x i8] c"open failed: %s\0A\00"
@.str.1 = [20 x i8] c"My string to write\0A\00"
@EBADF = global i64 0
@.str.2 = [18 x i8] c"write failed: %s\0A\00"
@SEEK_SET = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [24 x i8] c"read is returning junk\0A\00"
define i32 @generic_open_read_write(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [50 x i8]
%11 = alloca [50 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 -1, i32* %9
%12 = bitcast [50 x i8]* %10 to i8*
%13 = bitcast [50 x i8]* %11 to i8*
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = call i32 @open(i8* %14, i32 %15, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %3
%21 = load i32, i32* %6
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_EXCL
%24 = or i32 %22, %23
%25 = icmp eq i32 %21, %24
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i64, i64* @errno
%28 = load i64, i64* @EEXIST
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i8*, i8** %5
%32 = call i32 @unlink(i8* %31)
store i32 0, i32* %4
br label %158
33: ; preds = %26, %20
%34 = load i32, i32* @stderr
%35 = load i64, i64* @errno
%36 = call i8* @strerror(i64 %35)
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %36)
store i32 -1, i32* %4
br label %158
38: ; preds = %3
%39 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%40 = call i32 @strcpy(i8* %39, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%41 = load i32, i32* %8
%42 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%43 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @write(i32 %41, i8* %42, i32 %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp sle i32 %46, 0
br i1 %47, label %48, label %63
48: ; preds = %38
%49 = load i64, i64* @errno
%50 = load i64, i64* @EBADF
%51 = icmp ne i64 %49, %50
br i1 %51, label %52, label %62
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = load i64, i64* @errno
%55 = call i8* @strerror(i64 %54)
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
%59 = load i8*, i8** %5
%60 = call i32 @unlink(i8* %59)
%61 = load i32, i32* %9
store i32 %61, i32* %4
br label %158
62: ; preds = %48
br label %63
63: ; preds = %62, %38
%64 = load i32, i32* %8
%65 = load i32, i32* @SEEK_SET
%66 = call i32 @lseek(i32 %64, i32 0, i32 %65)
store i32 %66, i32* %9
%67 = load i32, i32* %9
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %63
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
%72 = load i8*, i8** %5
%73 = call i32 @unlink(i8* %72)
%74 = load i32, i32* %9
store i32 %74, i32* %4
br label %158
75: ; preds = %63
%76 = load i32, i32* %8
%77 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%78 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%79 = call i32 @strlen(i8* %78)
%80 = call i32 @read(i32 %76, i8* %77, i32 %79)
store i32 %80, i32* %9
%81 = load i32, i32* %9
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %105
83: ; preds = %75
%84 = load i32, i32* %6
%85 = load i32, i32* @O_CREAT
%86 = load i32, i32* @O_WRONLY
%87 = or i32 %85, %86
%88 = icmp ne i32 %84, %87
br i1 %88, label %89, label %105
89: ; preds = %83
%90 = load i32, i32* %6
%91 = load i32, i32* @O_WRONLY
%92 = icmp ne i32 %90, %91
br i1 %92, label %93, label %105
93: ; preds = %89
%94 = load i32, i32* %6
%95 = load i32, i32* @O_TRUNC
%96 = load i32, i32* @O_WRONLY
%97 = or i32 %95, %96
%98 = icmp ne i32 %94, %97
br i1 %98, label %99, label %105
99: ; preds = %93
%100 = load i32, i32* %8
%101 = call i32 @close(i32 %100)
%102 = load i8*, i8** %5
%103 = call i32 @unlink(i8* %102)
%104 = load i32, i32* %9
store i32 %104, i32* %4
br label %158
105: ; preds = %93, %89, %83, %75
%106 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%107 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%108 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%109 = call i32 @strlen(i8* %108)
%110 = call i32 @memcmp(i8* %106, i8* %107, i32 %109)
store i32 %110, i32* %9
%111 = load i32, i32* %9
%112 = icmp ne i32 0, %111
br i1 %112, label %113, label %153
113: ; preds = %105
%114 = load i32, i32* %6
%115 = load i32, i32* @O_TRUNC
%116 = load i32, i32* @O_WRONLY
%117 = or i32 %115, %116
%118 = icmp ne i32 %114, %117
br i1 %118, label %119, label %153
119: ; preds = %113
%120 = load i32, i32* %6
%121 = load i32, i32* @O_WRONLY
%122 = icmp ne i32 %120, %121
br i1 %122, label %123, label %153
123: ; preds = %119
%124 = load i32, i32* %6
%125 = load i32, i32* @O_CREAT
%126 = load i32, i32* @O_WRONLY
%127 = or i32 %125, %126
%128 = icmp ne i32 %124, %127
br i1 %128, label %129, label %153
129: ; preds = %123
%130 = load i32, i32* %6
%131 = load i32, i32* @O_CREAT
%132 = load i32, i32* @O_RDONLY
%133 = or i32 %131, %132
%134 = icmp eq i32 %130, %133
br i1 %134, label %153, label %135
135: ; preds = %129
%136 = load i32, i32* %6
%137 = load i32, i32* @O_RDONLY
%138 = icmp eq i32 %136, %137
br i1 %138, label %153, label %139
139: ; preds = %135
%140 = load i32, i32* %6
%141 = load i32, i32* @O_TRUNC
%142 = load i32, i32* @O_RDONLY
%143 = or i32 %141, %142
%144 = icmp eq i32 %140, %143
br i1 %144, label %153, label %145
145: ; preds = %139
%146 = load i32, i32* @stderr
%147 = call i32 (i32, i8*, ...) @fprintf(i32 %146, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%148 = load i32, i32* %8
%149 = call i32 @close(i32 %148)
%150 = load i8*, i8** %5
%151 = call i32 @unlink(i8* %150)
%152 = load i32, i32* %9
store i32 %152, i32* %4
br label %158
153: ; preds = %139, %135, %129, %123, %119, %113, %105
%154 = load i32, i32* %8
%155 = call i32 @close(i32 %154)
%156 = load i8*, i8** %5
%157 = call i32 @unlink(i8* %156)
store i32 0, i32* %4
br label %158
158: ; preds = %153, %145, %99, %69, %52, %33, %30
%159 = load i32, i32* %4
ret i32 %159
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str = [17 x i8] c"open failed: %s\0A\00"
@.str.1 = [20 x i8] c"My string to write\0A\00"
@EBADF = global i64 0
@.str.2 = [18 x i8] c"write failed: %s\0A\00"
@SEEK_SET = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [24 x i8] c"read is returning junk\0A\00"
define i32 @generic_open_read_write(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca [50 x i8]
%5 = alloca [50 x i8]
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%8 = tail call i32 @open(i8* %0, i32 %1, i32 %2)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %3
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_EXCL
%13 = or i32 %12, %11
%14 = icmp eq i32 %13, %1
%15 = load i64, i64* @errno
%16 = load i64, i64* @EEXIST
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = tail call i32 @unlink(i8* %0)
br label %97
21: ; preds = %10
%22 = load i32, i32* @stderr
%23 = tail call i8* @strerror(i64 %15)
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %97
25: ; preds = %3
%26 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @strlen(i8* %6)
%28 = call i32 @write(i32 %8, i8* %6, i32 %27)
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %40
30: ; preds = %25
%31 = load i64, i64* @errno
%32 = load i64, i64* @EBADF
%33 = icmp eq i64 %31, %32
br i1 %33, label %40, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i8* @strerror(i64 %31)
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = call i32 @close(i32 %8)
%39 = call i32 @unlink(i8* %0)
br label %97
40: ; preds = %30, %25
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @lseek(i32 %8, i32 0, i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 @close(i32 %8)
%46 = call i32 @unlink(i8* %0)
br label %97
47: ; preds = %40
%48 = call i32 @strlen(i8* %6)
%49 = call i32 @read(i32 %8, i8* %7, i32 %48)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %65
51: ; preds = %47
%52 = load i32, i32* @O_CREAT
%53 = load i32, i32* @O_WRONLY
%54 = or i32 %53, %52
%55 = icmp eq i32 %54, %1
%56 = icmp eq i32 %53, %1
%57 = or i1 %56, %55
br i1 %57, label %65, label %58
58: ; preds = %51
%59 = load i32, i32* @O_TRUNC
%60 = or i32 %59, %53
%61 = icmp eq i32 %60, %1
br i1 %61, label %65, label %62
62: ; preds = %58
%63 = call i32 @close(i32 %8)
%64 = call i32 @unlink(i8* %0)
br label %97
65: ; preds = %58, %51, %47
%66 = call i32 @strlen(i8* %6)
%67 = call i32 @memcmp(i8* %6, i8* %7, i32 %66)
%68 = icmp eq i32 %67, 0
br i1 %68, label %94, label %69
69: ; preds = %65
%70 = load i32, i32* @O_TRUNC
%71 = load i32, i32* @O_WRONLY
%72 = or i32 %71, %70
%73 = icmp eq i32 %72, %1
%74 = icmp eq i32 %71, %1
%75 = or i1 %74, %73
br i1 %75, label %94, label %76
76: ; preds = %69
%77 = load i32, i32* @O_CREAT
%78 = or i32 %77, %71
%79 = icmp eq i32 %78, %1
br i1 %79, label %94, label %80
80: ; preds = %76
%81 = load i32, i32* @O_RDONLY
%82 = or i32 %81, %77
%83 = icmp eq i32 %82, %1
%84 = icmp eq i32 %81, %1
%85 = or i1 %84, %83
%86 = or i32 %81, %70
%87 = icmp eq i32 %86, %1
%88 = select i1 %85, i1 true, i1 %87
br i1 %88, label %94, label %89
89: ; preds = %80
%90 = load i32, i32* @stderr
%91 = call i32 (i32, i8*, ...) @fprintf(i32 %90, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%92 = call i32 @close(i32 %8)
%93 = call i32 @unlink(i8* %0)
br label %97
94: ; preds = %80, %76, %69, %65
%95 = call i32 @close(i32 %8)
%96 = call i32 @unlink(i8* %0)
br label %97
97: ; preds = %94, %89, %62, %44, %34, %21, %19
%98 = phi i32 [ 0, %19 ], [ -1, %21 ], [ %28, %34 ], [ %42, %44 ], [ %49, %62 ], [ 0, %94 ], [ %67, %89 ]
ret i32 %98
}