#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 /*<<< orphan*/ DEFFILEMODE;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ chmod(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int write_to_file(char *filename, char *buffer, int bytes) {
  int fd;
  int ret;
  if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
    printf("Can't open %s\n", filename);
    return -1;
  }
  printf("Write content to: %s\n", filename);
  ret = write(fd, buffer, bytes);
  if (ret != bytes)
    printf("write() returned %d instead of %d\n", ret, bytes);
  chmod(filename, DEFFILEMODE);
  close(fd);
  return ret != bytes;
}


x86 assembly (clang, -O0):
<write_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11b6 <write_to_file+0x66>
mov    -0x10(%rbp),%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <write_to_file+0xd5>
mov    -0x10(%rbp),%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11fa <write_to_file+0xaa>
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1060 <chmod@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e66(%rip),%rcx
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e73(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11ef <write_to_file+0x9f>
mov    %eax,%r13d
lea    0xe76(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
cmp    %r12d,%eax
je     11cb <write_to_file+0x7b>
lea    0xe66(%rip),%rdi
mov    %ebx,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %ebp,%ebp
cmp    %r12d,%ebx
setne  %bpl
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <chmod@plt>
mov    %r13d,%edi
call   1050 <close@plt>
jmp    1205 <write_to_file+0xb5>
lea    0xe0a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@.str.1 = [22 x i8] c"Write content to: %s\0A\00"
@.str.2 = [35 x i8] c"write() returned %d instead of %d\0A\00"
@DEFFILEMODE = global i32 0
define i32 @write_to_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 420)
store i32 %16, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 -1, i32* %4
br label %45
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* %8
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = load i32, i32* %7
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = load i32, i32* %9
%33 = load i32, i32* %7
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
br label %35
35: ; preds = %31, %21
%36 = load i8*, i8** %5
%37 = load i32, i32* @DEFFILEMODE
%38 = call i32 @chmod(i8* %36, i32 %37)
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %9
%42 = load i32, i32* %7
%43 = icmp ne i32 %41, %42
%44 = zext i1 %43 to i32
store i32 %44, i32* %4
br label %45
45: ; preds = %35, %18
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@.str.1 = [22 x i8] c"Write content to: %s\0A\00"
@.str.2 = [35 x i8] c"write() returned %d instead of %d\0A\00"
@DEFFILEMODE = global i32 0
define i32 @write_to_file(i8* %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8, i32 420)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %24
13: ; preds = %3
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%15 = tail call i32 @write(i32 %9, i8* %1, i32 %2)
%16 = icmp ne i32 %15, %2
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %2)
br label %19
19: ; preds = %17, %13
%20 = load i32, i32* @DEFFILEMODE
%21 = tail call i32 @chmod(i8* %0, i32 %20)
%22 = tail call i32 @close(i32 %9)
%23 = zext i1 %16 to i32
br label %24
24: ; preds = %19, %11
%25 = phi i32 [ -1, %11 ], [ %23, %19 ]
ret i32 %25
}