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

/* Variables and functions */
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int S_IXGRP;
int S_IXUSR;
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int write_to_file(char *filename, char *buf, size_t num) {
  if (!buf)
    return 1;
  FILE *fp;
  fp = fopen(filename, "wb");
  if (!fp) {
    printf("Failed to open file %s\n", filename);
    return 1;
  }
  fwrite(buf, num, sizeof(char), fp);
  int fd = fileno(fp);
  int flags = 0;
  flags |= S_IXUSR;
  flags |= S_IRUSR;
  flags |= S_IWUSR;
  flags |= S_IXGRP;
  flags |= S_IRGRP;
  flags |= S_IWGRP;
  fchmod(fd, flags);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<write_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118b <write_to_file+0x2b>
movl   $0x1,-0x4(%rbp)
jmp    1264 <write_to_file+0x104>
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <write_to_file+0x66>
mov    -0x10(%rbp),%rsi
lea    0xe4e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1264 <write_to_file+0x104>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1060 <fchmod@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-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   %r12
push   %rbx
mov    $0x1,%ebp
test   %rsi,%rsi
je     1206 <write_to_file+0xa6>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f7 <write_to_file+0x97>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %r15,%rdi
call   1050 <fileno@plt>
mov    0x2e33(%rip),%rcx
mov    0x2dfc(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e11(%rip),%rcx
or     (%rcx),%esi
mov    0x2e00(%rip),%rcx
or     (%rcx),%esi
mov    0x2de7(%rip),%rcx
or     (%rcx),%esi
mov    0x2dfe(%rip),%rcx
or     (%rcx),%esi
mov    %eax,%edi
call   1060 <fchmod@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %ebp,%ebp
jmp    1206 <write_to_file+0xa6>
lea    0xe05(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [24 x i8] c"Failed to open file %s\0A\00"
@S_IXUSR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXGRP = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
define i32 @write_to_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 1, i32* %4
br label %52
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 1, i32* %4
br label %52
22: ; preds = %14
%23 = load i8*, i8** %6
%24 = load i64, i64* %7
%25 = load i32*, i32** %8
%26 = call i32 @fwrite(i8* %23, i64 %24, i32 1, i32* %25)
%27 = load i32*, i32** %8
%28 = call i32 @fileno(i32* %27)
store i32 %28, i32* %9
store i32 0, i32* %10
%29 = load i32, i32* @S_IXUSR
%30 = load i32, i32* %10
%31 = or i32 %30, %29
store i32 %31, i32* %10
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* %10
%34 = or i32 %33, %32
store i32 %34, i32* %10
%35 = load i32, i32* @S_IWUSR
%36 = load i32, i32* %10
%37 = or i32 %36, %35
store i32 %37, i32* %10
%38 = load i32, i32* @S_IXGRP
%39 = load i32, i32* %10
%40 = or i32 %39, %38
store i32 %40, i32* %10
%41 = load i32, i32* @S_IRGRP
%42 = load i32, i32* %10
%43 = or i32 %42, %41
store i32 %43, i32* %10
%44 = load i32, i32* @S_IWGRP
%45 = load i32, i32* %10
%46 = or i32 %45, %44
store i32 %46, i32* %10
%47 = load i32, i32* %9
%48 = load i32, i32* %10
%49 = call i32 @fchmod(i32 %47, i32 %48)
%50 = load i32*, i32** %8
%51 = call i32 @fclose(i32* %50)
store i32 0, i32* %4
br label %52
52: ; preds = %22, %19, %13
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [24 x i8] c"Failed to open file %s\0A\00"
@S_IXUSR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXGRP = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
define i32 @write_to_file(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %26, label %5
5: ; preds = %3
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %26
10: ; preds = %5
%11 = tail call i32 @fwrite(i8* %1, i64 %2, i32 1, i32* %6)
%12 = tail call i32 @fileno(i32* %6)
%13 = load i32, i32* @S_IXUSR
%14 = load i32, i32* @S_IRUSR
%15 = or i32 %14, %13
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IXGRP
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IWGRP
%23 = or i32 %21, %22
%24 = tail call i32 @fchmod(i32 %12, i32 %23)
%25 = tail call i32 @fclose(i32* %6)
br label %26
26: ; preds = %8, %10, %3
%27 = phi i32 [ 1, %3 ], [ 0, %10 ], [ 1, %8 ]
ret i32 %27
}