#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 */
struct file_data {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
scalar_t__ __generate_file(struct file_data *, int, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int create_file(struct file_data *, char const *, int, size_t);
int generate_file(struct file_data *data, const char *name, size_t filesize) {
  int fd;
  fd = create_file(data, name, O_RDWR | O_CREAT | O_TRUNC, filesize);
  if (fd < 0)
    return -1;
  if (__generate_file(data, fd, 0) < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<generate_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
mov    0x2e9c(%rip),%rax
or     (%rax),%edx
mov    0x2e8b(%rip),%rax
or     (%rax),%edx
mov    -0x20(%rbp),%rcx
call   1050 <create_file@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1189 <generate_file+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <generate_file+0x8b>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
xor    %edx,%edx
call   1040 <__generate_file@plt>
cmp    $0x0,%rax
jge    11b5 <generate_file+0x85>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <generate_file+0x8b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_file>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e9d(%rip),%rcx
mov    0x2eb6(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rdx,%rcx
mov    %rdi,%rbx
mov    0x2e9d(%rip),%rdx
or     (%rdx),%eax
mov    %eax,%edx
call   1050 <create_file@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1185 <generate_file+0x55>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1040 <__generate_file@plt>
test   %rax,%rax
js     117e <generate_file+0x4e>
mov    %ebp,%r14d
jmp    1185 <generate_file+0x55>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.file_data = type { i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @generate_file(%struct.file_data* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.file_data*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store %struct.file_data* %0, %struct.file_data** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load %struct.file_data*, %struct.file_data** %5
%10 = load i8*, i8** %6
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = load i64, i64* %7
%17 = call i32 @create_file(%struct.file_data* %9, i8* %10, i32 %15, i64 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %31
21: ; preds = %3
%22 = load %struct.file_data*, %struct.file_data** %5
%23 = load i32, i32* %8
%24 = call i64 @__generate_file(%struct.file_data* %22, i32 %23, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %4
br label %31
29: ; preds = %21
%30 = load i32, i32* %8
store i32 %30, i32* %4
br label %31
31: ; preds = %29, %26, %20
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
%struct.file_data = type { i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @generate_file(%struct.file_data* %0, i8* %1, i64 %2) {
%4 = load i32, i32* @O_RDWR
%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 @create_file(%struct.file_data* %0, i8* %1, i32 %8, i64 %2)
%10 = icmp slt i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %3
%12 = tail call i64 @__generate_file(%struct.file_data* %0, i32 %9, i32 0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @close(i32 %9)
br label %16
16: ; preds = %11, %3, %14
%17 = phi i32 [ -1, %14 ], [ -1, %3 ], [ %9, %11 ]
ret i32 %17
}