#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 ssize_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int get_resource_size(char *);
int open(char *, int, int);
int /*<<< orphan*/ print_header(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int write(int, char *, int);
int lfd_write(char *data) {
  int fd;
  ssize_t wsize;
  char buf[512];
  char buf2[512];
  ssize_t size;
  print_header(data, buf);
  sprintf(buf2, "/tmp/%s", buf);
  size = get_resource_size(data);
  fd = open(buf2, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
  if (fd == -1) {
    return -1;
  }
  wsize = write(fd, data + 0x10, size - 0x10);
  close(fd);
  return wsize + 0x10;
}


x86 assembly (clang, -O0):
<lfd_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x220(%rbp),%rsi
call   1070 <print_header@plt>
lea    -0x420(%rbp),%rdi
lea    -0x220(%rbp),%rdx
lea    0xe6c(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <get_resource_size@plt>
mov    %eax,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1203 <lfd_write+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <lfd_write+0xd0>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
mov    -0x424(%rbp),%edx
sub    $0x10,%edx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lfd_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
lea    0x200(%rsp),%rbp
mov    %rbp,%rsi
call   1070 <print_header@plt>
lea    0xe79(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbp,%rdx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <get_resource_size@plt>
mov    0x2e24(%rip),%rcx
mov    0x2e4d(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e3a(%rip),%rcx
or     (%rcx),%esi
mov    0x2e11(%rip),%rcx
mov    0x2dfa(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,%r14d
mov    0x2e0c(%rip),%rax
or     (%rax),%edx
mov    %r15,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1208 <lfd_write+0xa8>
mov    %eax,%ebp
add    $0x10,%rbx
add    $0xfffffff0,%r14d
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x10,%ebx
jmp    120d <lfd_write+0xad>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/tmp/%s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
define i32 @lfd_write(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca [512 x i8]
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%11 = call i32 @print_header(i8* %9, i8* %10)
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %3
%16 = call i32 @get_resource_size(i8* %15)
store i32 %16, i32* %8
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRWXU
%24 = load i32, i32* @S_IRWXG
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRWXO
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %17, i32 %22, i32 %27)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %1
store i32 -1, i32* %2
br label %43
32: ; preds = %1
%33 = load i32, i32* %4
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 16
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 16
%38 = call i32 @write(i32 %33, i8* %35, i32 %37)
store i32 %38, i32* %5
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 16
store i32 %42, i32* %2
br label %43
43: ; preds = %32, %31
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [8 x i8] c"/tmp/%s\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
define i32 @lfd_write(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = call i32 @print_header(i8* %0, i8* %4)
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %4)
%8 = call i32 @get_resource_size(i8* %0)
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRWXU
%15 = load i32, i32* @S_IRWXG
%16 = or i32 %15, %14
%17 = load i32, i32* @S_IRWXO
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %5, i32 %13, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %27, label %21
21: ; preds = %1
%22 = getelementptr inbounds i8, i8* %0, i64 16
%23 = add nsw i32 %8, -16
%24 = call i32 @write(i32 %19, i8* %22, i32 %23)
%25 = call i32 @close(i32 %19)
%26 = add nsw i32 %24, 16
br label %27
27: ; preds = %1, %21
%28 = phi i32 [ %26, %21 ], [ -1, %1 ]
ret i32 %28
}