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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd;
  int i;
  pid_t child;
  fd = open("/dev/cdata", O_RDWR);
  if (fd == -1) {
    printf("can't open file [%d]\n", errno);
    exit(1);
  }
  for (i = 0; i < 32; i++)
    write(fd, "hello", 5);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a0 <main+0x50>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x20,-0xc(%rbp)
jge    11d3 <main+0x83>
mov    -0x8(%rbp),%edi
lea    0xe66(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a7 <main+0x57>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11a2 <main+0x52>
mov    %eax,%ebx
mov    $0x20,%ebp
lea    0xea5(%rip),%r14
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebp
jne    1180 <main+0x30>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/cdata\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"can't open file [%d]\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"hello\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
store i32 0, i32* %3
br label %14
14: ; preds = %20, %13
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 32
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* %2
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
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 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/cdata\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"can't open file [%d]\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"hello\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = load i32, i32* @errno
%6 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0, %8
%9 = phi i32 [ %11, %8 ], [ 0, %0 ]
%10 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%11 = add nuw nsw i32 %9, 1
%12 = icmp eq i32 %11, 32
br i1 %12, label %13, label %8
13: ; preds = %8
%14 = tail call i32 @close(i32 %2)
ret i32 0
}