#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*/ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void main() {
  int outfd = open("password.bak", O_RDWR | O_CREAT, S_IRWXU);
  int infd = open("/etc/passwd", O_RDONLY);
  int fp = lseek(infd, 0, SEEK_END);
  char buf[fp];
  lseek(infd, 0, SEEK_SET);
  ssize_t fileread = read(infd, buf, sizeof(buf));
  write(outfd, buf, fileread);
  write(1, buf, fileread);
  printf("\n\n\nThe value of FD for  input file is :: %d\n", infd);
  printf("The value of FD for output file is :: %d\n", outfd);
  close(infd);
  close(outfd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
or     %eax,%esi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
lea    0xe74(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
mov    %al,-0x3d(%rbp)
call   1080 <open@plt>
mov    %eax,%ecx
mov    -0x3d(%rbp),%al
mov    %ecx,-0x4(%rbp)
mov    0x2e20(%rip),%rcx
mov    (%rcx),%esi
lea    0xe5c(%rip),%rdi
call   1080 <open@plt>
mov    -0x3c(%rbp),%esi
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%edi
mov    %eax,%edx
call   1070 <read@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x8(%rbp),%esi
lea    0xdcd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%esi
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e66(%rip),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
lea    0xe73(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r12d
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,%r15d
mov    %eax,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1070 <read@plt>
mov    %eax,%r15d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
lea    0xdf7(%rip),%rdi
mov    %r12d,%esi
call   1040 <printf@plt>
lea    0xe15(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r14d,%edi
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"password.bak\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [45 x i8] c"\0A\0A\0AThe value of FD for input file is :: %d\0A\00"
@.str.3 = [42 x i8] c"The value of FD for output file is :: %d\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
store i32 %11, i32* %1
%12 = load i32, i32* @O_RDONLY
%13 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = load i32, i32* @SEEK_END
%16 = call i32 @lseek(i32 %14, i32 0, i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %4
%20 = alloca i8, i64 %18
store i64 %18, i64* %5
%21 = load i32, i32* %2
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @lseek(i32 %21, i32 0, i32 %22)
%24 = load i32, i32* %2
%25 = trunc i64 %18 to i32
%26 = call i32 @read(i32 %24, i8* %20, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %1
%28 = load i32, i32* %6
%29 = call i32 @write(i32 %27, i8* %20, i32 %28)
%30 = load i32, i32* %6
%31 = call i32 @write(i32 1, i8* %20, i32 %30)
%32 = load i32, i32* %2
%33 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %1
%35 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %1
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"password.bak\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [45 x i8] c"\0A\0A\0AThe value of FD for input file is :: %d\0A\00"
@.str.3 = [42 x i8] c"The value of FD for output file is :: %d\0A\00"
define void @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRWXU
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @lseek(i32 %7, i32 0, i32 %8)
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @lseek(i32 %7, i32 0, i32 %12)
%14 = call i32 @read(i32 %7, i8* %11, i32 %9)
%15 = call i32 @write(i32 %5, i8* %11, i32 %14)
%16 = call i32 @write(i32 1, i8* %11, i32 %14)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%19 = tail call i32 @close(i32 %7)
%20 = tail call i32 @close(i32 %5)
ret void
}