#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, const char *argv[], const char *envp[]) {
  char data[8000];
  strcat(data, "CLI arguments\n");
  for (int i = 0; i < argc; i++) {
    strcat(data, argv[i]);
    strcat(data, "\n");
  }
  strcat(data, "Enviroment variables\n");
  for (int i = 0; envp[i]; i++) {
    strcat(data, envp[i]);
    strcat(data, "\n");
  }
  int filedesc =
      open("labka5.txt", O_CREAT | O_TRUNC | O_WRONLY,
           S_IRUSR | S_IXUSR | S_IWUSR | S_IRGRP | S_IXGRP | S_IXOTH);
  write(filedesc, data, strlen(data));
  close(filedesc);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1f60(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1070 <strcat@plt>
movl   $0x0,-0x1f64(%rbp)
mov    -0x1f64(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11db <main+0x8b>
lea    -0x1f60(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1f64(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1070 <strcat@plt>
lea    -0x1f60(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x1f64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f64(%rbp)
jmp    118a <main+0x3a>
lea    -0x1f60(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1070 <strcat@plt>
movl   $0x0,-0x1f68(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1f68(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1250 <main+0x100>
lea    -0x1f60(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x1f68(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1070 <strcat@plt>
lea    -0x1f60(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x1f68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f68(%rbp)
jmp    11f8 <main+0xa8>
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2d90(%rip),%rax
or     (%rax),%esi
mov    0x2d2f(%rip),%rax
or     (%rax),%esi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
mov    0x2d55(%rip),%rax
or     (%rax),%edx
mov    0x2d44(%rip),%rax
or     (%rax),%edx
mov    0x2d23(%rip),%rax
or     (%rax),%edx
mov    0x2d2a(%rip),%rax
or     (%rax),%edx
mov    0x2d39(%rip),%rax
or     (%rax),%edx
lea    0xd7d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1f6c(%rbp)
mov    -0x1f6c(%rbp),%eax
mov    %eax,-0x1f7c(%rbp)
lea    -0x1f60(%rbp),%rax
mov    %rax,-0x1f78(%rbp)
lea    -0x1f60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1f7c(%rbp),%edi
mov    -0x1f78(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x1f6c(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x1f80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f48,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe90(%rip),%rsi
mov    %rsp,%rdi
call   1070 <strcat@plt>
test   %ebp,%ebp
jle    11b0 <main+0x60>
mov    %ebp,%r13d
xor    %ebp,%ebp
mov    %rsp,%r15
lea    0xe98(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbp,8),%rsi
mov    %r15,%rdi
call   1070 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1190 <main+0x40>
lea    0xe58(%rip),%rsi
mov    %rsp,%rdi
call   1070 <strcat@plt>
mov    (%rbx),%rsi
test   %rsi,%rsi
je     11ff <main+0xaf>
add    $0x8,%rbx
mov    %rsp,%r14
lea    0xe4e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1070 <strcat@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
mov    (%rbx),%rsi
add    $0x8,%rbx
test   %rsi,%rsi
jne    11e0 <main+0x90>
mov    0x2df2(%rip),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d80(%rip),%rax
or     (%rax),%esi
mov    0x2d87(%rip),%rax
mov    0x2da8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d95(%rip),%rax
or     (%rax),%edx
mov    0x2d74(%rip),%rax
or     (%rax),%edx
mov    0x2d7b(%rip),%rax
or     (%rax),%edx
mov    0x2d8a(%rip),%rax
or     (%rax),%edx
lea    0xdce(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x1f48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"CLI arguments\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [22 x i8] c"Enviroment variables\0A\00"
@.str.3 = [11 x i8] c"labka5.txt\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IXUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [8000 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%12 = getelementptr inbounds [8000 x i8], [8000 x i8]* %8, i64 0, i64 0
%13 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %9
br label %14
14: ; preds = %28, %3
%15 = load i32, i32* %9
%16 = load i32, i32* %5
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = getelementptr inbounds [8000 x i8], [8000 x i8]* %8, i64 0, i64 0
%20 = load i8**, i8*** %6
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = call i32 @strcat(i8* %19, i8* %24)
%26 = getelementptr inbounds [8000 x i8], [8000 x i8]* %8, i64 0, i64 0
%27 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %18
%29 = load i32, i32* %9
%30 = add nsw i32 %29, 1
store i32 %30, i32* %9
br label %14
31: ; preds = %14
%32 = getelementptr inbounds [8000 x i8], [8000 x i8]* %8, i64 0, i64 0
%33 = call i32 @strcat(i8* %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %10
br label %34
34: ; preds = %51, %31
%35 = load i8**, i8*** %7
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %35, i64 %37
%39 = load i8*, i8** %38
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %54
41: ; preds = %34
%42 = getelementptr inbounds [8000 x i8], [8000 x i8]* %8, i64 0, i64 0
%43 = load i8**, i8*** %7
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8*, i8** %43, i64 %45
%47 = load i8*, i8** %46
%48 = call i32 @strcat(i8* %42, i8* %47)
%49 = getelementptr inbounds [8000 x i8], [8000 x i8]* %8, i64 0, i64 0
%50 = call i32 @strcat(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %41
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
br label %34
54: ; preds = %34
%55 = load i32, i32* @O_CREAT
%56 = load i32, i32* @O_TRUNC
%57 = or i32 %55, %56
%58 = load i32, i32* @O_WRONLY
%59 = or i32 %57, %58
%60 = load i32, i32* @S_IRUSR
%61 = load i32, i32* @S_IXUSR
%62 = or i32 %60, %61
%63 = load i32, i32* @S_IWUSR
%64 = or i32 %62, %63
%65 = load i32, i32* @S_IRGRP
%66 = or i32 %64, %65
%67 = load i32, i32* @S_IXGRP
%68 = or i32 %66, %67
%69 = load i32, i32* @S_IXOTH
%70 = or i32 %68, %69
%71 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %59, i32 %70)
store i32 %71, i32* %11
%72 = load i32, i32* %11
%73 = getelementptr inbounds [8000 x i8], [8000 x i8]* %8, i64 0, i64 0
%74 = getelementptr inbounds [8000 x i8], [8000 x i8]* %8, i64 0, i64 0
%75 = call i32 @strlen(i8* %74)
%76 = call i32 @write(i32 %72, i8* %73, i32 %75)
%77 = load i32, i32* %11
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
@.str = [15 x i8] c"CLI arguments\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [22 x i8] c"Enviroment variables\0A\00"
@.str.3 = [11 x i8] c"labka5.txt\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IXUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture %2) {
%4 = alloca [8000 x i8]
%5 = getelementptr inbounds [8000 x i8], [8000 x i8]* %4, i64 0, i64 0
%6 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = zext i32 %0 to i64
br label %14
10: ; preds = %14, %3
%11 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%12 = load i8*, i8** %2
%13 = icmp eq i8* %12, null
br i1 %13, label %22, label %43
14: ; preds = %8, %14
%15 = phi i64 [ 0, %8 ], [ %20, %14 ]
%16 = getelementptr inbounds i8*, i8** %1, i64 %15
%17 = load i8*, i8** %16
%18 = call i32 @strcat(i8* %5, i8* %17)
%19 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = add nuw nsw i64 %15, 1
%21 = icmp eq i64 %20, %9
br i1 %21, label %10, label %14
22: ; preds = %43, %10
%23 = load i32, i32* @O_CREAT
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %24, %23
%26 = load i32, i32* @O_WRONLY
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IXUSR
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRGRP
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IXGRP
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IXOTH
%38 = or i32 %36, %37
%39 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %38)
%40 = call i32 @strlen(i8* %5)
%41 = call i32 @write(i32 %39, i8* %5, i32 %40)
%42 = call i32 @close(i32 %39)
ret i32 0
43: ; preds = %10, %43
%44 = phi i64 [ %48, %43 ], [ 0, %10 ]
%45 = phi i8* [ %50, %43 ], [ %12, %10 ]
%46 = call i32 @strcat(i8* %5, i8* %45)
%47 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%48 = add nuw i64 %44, 1
%49 = getelementptr inbounds i8*, i8** %2, i64 %48
%50 = load i8*, i8** %49
%51 = icmp eq i8* %50, null
br i1 %51, label %22, label %43
}