#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*/ uint8_t;
typedef scalar_t__ uint32_t;
typedef int int32_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int write(int, int /*<<< orphan*/ *, scalar_t__);
void write_buffer_to_file(char *filename, void *in, uint32_t buf_size) {
  int32_t fd;
  uint32_t count = 0;
  uint8_t *buffer = (uint8_t *)in;
  fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
  if (fd == -1) {
    printf("Error opening the file for writing\n");
    return;
  }
  while (count < buf_size) {
    int ret = write(fd, (buffer + count), (buf_size - count));
    if (ret < 0) {
      printf("Error writing PPM file contents\n");
      break;
    }
    count += ret;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<write_buffer_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11af <write_buffer_to_file+0x6f>
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
jmp    1218 <write_buffer_to_file+0xd8>
jmp    11b4 <write_buffer_to_file+0x74>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1210 <write_buffer_to_file+0xd0>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11ff <write_buffer_to_file+0xbf>
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
jmp    1210 <write_buffer_to_file+0xd0>
movslq -0x34(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11b4 <write_buffer_to_file+0x74>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buffer_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    0x2e6c(%rip),%rax
mov    0x2e9d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e8a(%rip),%rax
or     (%rax),%esi
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11b2 <write_buffer_to_file+0x72>
mov    %eax,%r15d
test   %rbx,%rbx
jle    11d4 <write_buffer_to_file+0x94>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r14,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1040 <write@plt>
test   %eax,%eax
js     11c8 <write_buffer_to_file+0x88>
mov    %eax,%eax
add    %rax,%rbp
cmp    %rbx,%rbp
jl     1190 <write_buffer_to_file+0x50>
jmp    11d4 <write_buffer_to_file+0x94>
lea    0xe67(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str = [36 x i8] c"Error opening the file for writing\0A\00"
@.str.1 = [33 x i8] c"Error writing PPM file contents\0A\00"
define void @write_buffer_to_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %8
%11 = load i8*, i8** %5
%12 = bitcast i8* %11 to i32*
store i32* %12, i32** %9
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRWXU
%20 = call i32 @open(i8* %13, i32 %18, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %51
25: ; preds = %3
br label %26
26: ; preds = %43, %25
%27 = load i64, i64* %8
%28 = load i64, i64* %6
%29 = icmp slt i64 %27, %28
br i1 %29, label %30, label %48
30: ; preds = %26
%31 = load i32, i32* %7
%32 = load i32*, i32** %9
%33 = load i64, i64* %8
%34 = getelementptr inbounds i32, i32* %32, i64 %33
%35 = load i64, i64* %6
%36 = load i64, i64* %8
%37 = sub nsw i64 %35, %36
%38 = call i32 @write(i32 %31, i32* %34, i64 %37)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %30
%42 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %48
43: ; preds = %30
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = load i64, i64* %8
%47 = add nsw i64 %46, %45
store i64 %47, i64* %8
br label %26
48: ; preds = %41, %26
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %23
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@str = [32 x i8] c"Error writing PPM file contents\00"
@str.2 = [35 x i8] c"Error opening the file for writing\00"
define void @write_buffer_to_file(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = bitcast i8* %1 to i32*
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = tail call i32 @open(i8* %0, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %15, label %13
13: ; preds = %3
%14 = icmp sgt i64 %2, 0
br i1 %14, label %19, label %29
15: ; preds = %3
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.2, i64 0, i64 0))
br label %31
17: ; preds = %19
%18 = icmp slt i64 %26, %2
br i1 %18, label %19, label %29
19: ; preds = %13, %17
%20 = phi i64 [ %26, %17 ], [ 0, %13 ]
%21 = getelementptr inbounds i32, i32* %4, i64 %20
%22 = sub nsw i64 %2, %20
%23 = tail call i32 @write(i32 %11, i32* %21, i64 %22)
%24 = icmp slt i32 %23, 0
%25 = zext i32 %23 to i64
%26 = add nuw nsw i64 %20, %25
br i1 %24, label %27, label %17
27: ; preds = %19
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %17, %13, %27
%30 = tail call i32 @close(i32 %11)
br label %31
31: ; preds = %29, %15
ret void
}