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

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd = open("test.data", O_RDWR | O_CREAT | O_EXCL, 0666);
  char buffer;
  char s[20] = "1234\n2234\0";
  if (fd < 0) {
    fd = open("test.data", O_RDONLY);
    while (buffer != '\n') {
      read(fd, &buffer, 1);
      write(1, &buffer, 1);
    }
  } else {
    write(fd, s, sizeof(s));
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
or     (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
lea    0xe8f(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0xe89(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe86(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe84(%rip),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11fb <main+0xbb>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
je     11f6 <main+0xb6>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    $0x1,%edx
lea    -0x9(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
jmp    11c3 <main+0x83>
jmp    120c <main+0xcc>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2e93(%rip),%rax
mov    0x2ea4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
movaps 0xe95(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
test   %eax,%eax
js     11a1 <main+0x61>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1030 <write@plt>
jmp    11e8 <main+0xa8>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
lea    0xf(%rsp),%rbx
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmpb   $0xa,0xf(%rsp)
jne    11c0 <main+0x80>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"test.data\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@__const.main.s = [20 x i8] c"1234\0A2234\00\00\00\00\00\00\00\00\00\00\00"
@O_RDONLY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%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_EXCL
%9 = or i32 %7, %8
%10 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9, i32 438)
store i32 %10, i32* %2
%11 = bitcast [20 x i8]* %4 to i8*
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %0
%15 = load i32, i32* @O_RDONLY
%16 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %2
br label %17
17: ; preds = %21, %14
%18 = load i8, i8* %3
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 10
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* %2
%23 = call i32 @read(i32 %22, i8* %3, i32 1)
%24 = call i32 @write(i32 1, i8* %3, i32 1)
br label %17
25: ; preds = %17
br label %30
26: ; preds = %0
%27 = load i32, i32* %2
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%29 = call i32 @write(i32 %27, i8* %28, i32 20)
br label %30
30: ; preds = %26, %25
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"test.data\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@__const.main.s = [20 x i8] c"1234\0A2234\00\00\00\00\00\00\00\00\00\00\00"
@O_RDONLY = global i32 0
define i32 @main() {
%1 = alloca i8
%2 = alloca [20 x i8]
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_EXCL
%7 = or i32 %5, %6
%8 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%10 = icmp slt i32 %8, 0
br i1 %10, label %11, label %19
11: ; preds = %0
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %11, %14
%15 = call i32 @read(i32 %13, i8* %1, i32 1)
%16 = call i32 @write(i32 1, i8* %1, i32 1)
%17 = load i8, i8* %1
%18 = icmp eq i8 %17, 10
br i1 %18, label %21, label %14
19: ; preds = %0
%20 = call i32 @write(i32 %8, i8* %9, i32 20)
br label %21
21: ; preds = %14, %19
%22 = phi i32 [ %8, %19 ], [ %13, %14 ]
%23 = tail call i32 @close(i32 %22)
ret i32 0
}