#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 scalar_t__ uint32_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int);
scalar_t__ pe_get32(int, int, scalar_t__ *);
scalar_t__ pe_set16(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int set_pe_characteristics(const char *pathname, uint16_t pe_characteristics) {
  uint32_t pe_header_offset, opthdr_ofs;
  int status = 1;
  int fd, size;
  fd = open(pathname, O_RDWR | 0);
  if (fd == -1)
    return status;
  if (pe_get32(fd, 0x3c, &pe_header_offset) != 0)
    goto done;
  opthdr_ofs = pe_header_offset + 4 + 20;
  if (pe_set16(fd, opthdr_ofs + 70, pe_characteristics) != 0) {
    fprintf(stderr,
            "CATASTROPIC ERROR: attempt to write to file failed! %s could be "
            "corrupted; HALTING.\n",
            pathname);
    close(fd);
    exit(2);
  }
  status = 0;
done:
  close(fd);
  return status;
}


x86 assembly (clang, -O0):
<set_pe_characteristics>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   1060 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11a3 <set_pe_characteristics+0x43>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1230 <set_pe_characteristics+0xd0>
mov    -0x30(%rbp),%edi
mov    $0x3c,%esi
lea    -0x20(%rbp),%rdx
call   1030 <pe_get32@plt>
cmp    $0x0,%rax
je     11c3 <set_pe_characteristics+0x63>
jmp    1222 <set_pe_characteristics+0xc2>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
add    $0x14,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x46,%rsi
mov    -0x14(%rbp),%edx
call   1080 <pe_set16@plt>
cmp    $0x0,%rax
je     121b <set_pe_characteristics+0xbb>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfc(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_pe_characteristics>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11bd <set_pe_characteristics+0x5d>
mov    %eax,%ebp
mov    %rsp,%rdx
mov    %eax,%edi
mov    $0x3c,%esi
call   1030 <pe_get32@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    11b4 <set_pe_characteristics+0x54>
mov    (%rsp),%rsi
add    $0x5e,%rsi
mov    %ebp,%edi
mov    %r14d,%edx
call   1080 <pe_set16@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11cf <set_pe_characteristics+0x6f>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11c2 <set_pe_characteristics+0x62>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [85 x i8] c"CATASTROPIC ERROR: attempt to write to file failed! %s could be corrupted; HALTING.\0A\00"
define i32 @set_pe_characteristics(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %8
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDWR
%13 = or i32 %12, 0
%14 = call i32 @open(i8* %11, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = load i32, i32* %8
store i32 %18, i32* %3
br label %46
19: ; preds = %2
%20 = load i32, i32* %9
%21 = call i64 @pe_get32(i32 %20, i32 60, i64* %6)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
br label %42
24: ; preds = %19
%25 = load i64, i64* %6
%26 = add nsw i64 %25, 4
%27 = add nsw i64 %26, 20
store i64 %27, i64* %7
%28 = load i32, i32* %9
%29 = load i64, i64* %7
%30 = add nsw i64 %29, 70
%31 = load i32, i32* %5
%32 = call i64 @pe_set16(i32 %28, i64 %30, i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %24
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %4
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %9
%39 = call i32 @close(i32 %38)
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %24
store i32 0, i32* %8
br label %42
42: ; preds = %41, %23
%43 = load i32, i32* %9
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %8
store i32 %45, i32* %3
br label %46
46: ; preds = %42, %17
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [85 x i8] c"CATASTROPIC ERROR: attempt to write to file failed! %s could be corrupted; HALTING.\0A\00"
define i32 @set_pe_characteristics(i8* %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %24, label %8
8: ; preds = %2
%9 = call i64 @pe_get32(i32 %6, i32 60, i64* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %8
%12 = load i64, i64* %3
%13 = add nsw i64 %12, 94
%14 = call i64 @pe_set16(i32 %6, i64 %13, i32 %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i8* %0)
%19 = call i32 @close(i32 %6)
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %11, %8
%22 = phi i32 [ 1, %8 ], [ 0, %11 ]
%23 = call i32 @close(i32 %6)
br label %24
24: ; preds = %2, %21
%25 = phi i32 [ %22, %21 ], [ 1, %2 ]
ret i32 %25
}