#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 BUFSIZ;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd;
  int i;
  printf("BUFSIZ = %d\n", BUFSIZ);
  fd = open("test_1.txt", O_WRONLY);
  if (fd == -1) {
    printf("open error!\n");
    return -1;
  }
  printf("write start\n");
  for (i = 0; i < 2048; i++) {
    write(fd, "ABCD", 4);
  }
  printf("write end\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a2 <main+0x62>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <main+0xc3>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x800,-0xc(%rbp)
jge    11e6 <main+0xa6>
mov    -0x8(%rbp),%edi
lea    0xe64(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b7 <main+0x77>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe92(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11cb <main+0x7b>
mov    %eax,%ebx
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x800,%ebp
lea    0xe79(%rip),%r14
nop
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebp
jne    11a0 <main+0x50>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11dc <main+0x8c>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"BUFSIZ = %d\0A\00"
@BUFSIZ = global i32 0
@.str.1 = [11 x i8] c"test_1.txt\00"
@O_WRONLY = global i32 0
@.str.2 = [13 x i8] c"open error!\0A\00"
@.str.3 = [13 x i8] c"write start\0A\00"
@.str.4 = [5 x i8] c"ABCD\00"
@.str.5 = [11 x i8] c"write end\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @BUFSIZ
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %14
14: ; preds = %20, %12
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 2048
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* %2
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
br label %20
20: ; preds = %17
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %14
23: ; preds = %14
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
store i32 0, i32* %1
br label %27
27: ; preds = %23, %10
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [13 x i8] c"BUFSIZ = %d\0A\00"
@BUFSIZ = global i32 0
@.str.1 = [11 x i8] c"test_1.txt\00"
@O_WRONLY = global i32 0
@.str.4 = [5 x i8] c"ABCD\00"
@str = [12 x i8] c"write start\00"
@str.6 = [10 x i8] c"write end\00"
@str.7 = [12 x i8] c"open error!\00"
define i32 @main() {
%1 = load i32, i32* @BUFSIZ
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %18
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %8, %10
%11 = phi i32 [ 0, %8 ], [ %13, %10 ]
%12 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%13 = add nuw nsw i32 %11, 1
%14 = icmp eq i32 %13, 2048
br i1 %14, label %15, label %10
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.6, i64 0, i64 0))
%17 = tail call i32 @close(i32 %4)
br label %18
18: ; preds = %15, %6
%19 = phi i32 [ -1, %6 ], [ 0, %15 ]
ret i32 %19
}