#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_coff_characteristics(const char *pathname,
                             uint16_t coff_characteristics) {
  uint32_t pe_header_offset;
  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;
  if (pe_set16(fd, pe_header_offset + 4 + 18, coff_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_coff_characteristics>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11a3 <set_coff_characteristics+0x43>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1224 <set_coff_characteristics+0xc4>
mov    -0x28(%rbp),%edi
mov    $0x3c,%esi
lea    -0x20(%rbp),%rdx
call   1030 <pe_get32@plt>
cmp    $0x0,%rax
je     11c3 <set_coff_characteristics+0x63>
jmp    1216 <set_coff_characteristics+0xb6>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
add    $0x12,%rsi
mov    -0x14(%rbp),%edx
call   1080 <pe_set16@plt>
cmp    $0x0,%rax
je     120f <set_coff_characteristics+0xaf>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe08(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_coff_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_coff_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_coff_characteristics+0x54>
mov    (%rsp),%rsi
add    $0x16,%rsi
mov    %ebp,%edi
mov    %r14d,%edx
call   1080 <pe_set16@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11cf <set_coff_characteristics+0x6f>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11c2 <set_coff_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_coff_characteristics(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %7
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDWR
%12 = or i32 %11, 0
%13 = call i32 @open(i8* %10, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %43
18: ; preds = %2
%19 = load i32, i32* %8
%20 = call i64 @pe_get32(i32 %19, i32 60, i64* %6)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
br label %39
23: ; preds = %18
%24 = load i32, i32* %8
%25 = load i64, i64* %6
%26 = add nsw i64 %25, 4
%27 = add nsw i64 %26, 18
%28 = load i32, i32* %5
%29 = call i64 @pe_set16(i32 %24, i64 %27, i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %4
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i8* %33)
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
%37 = call i32 @exit(i32 2)
unreachable
38: ; preds = %23
store i32 0, i32* %7
br label %39
39: ; preds = %38, %22
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %7
store i32 %42, i32* %3
br label %43
43: ; preds = %39, %16
%44 = load i32, i32* %3
ret i32 %44
}

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_coff_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, 22
%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
}