#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcmp(char *, char *);
int main(int argn, char *argv[]) {
  char flag = 2;
  FILE *fi = fopen(argv[1], "rb+");
  if (argn < 2) {
    printf("Usage:\n");
    printf("\t%s program.exe [console]\n", argv[0]);
    return 0;
  }
  if (argn == 3) {
    if (!strcmp(argv[2], "console")) {
      flag = 3;
    }
  }
  if (!fi) {
    perror(argv[1]);
    exit(1);
  }
  fseek(fi, 220, SEEK_SET);
  if (flag == 2)
    printf("Setting Win32 GUI flag in %s\n", argv[1]);
  else if (flag == 3)
    printf("Setting Win32 Console flag in %s\n", argv[1]);
  fwrite(&flag, 1, 1, fi);
  fclose(fi);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x2,-0x11(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe57(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11eb <main+0x6b>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c8 <main+0x148>
cmpl   $0x3,-0x8(%rbp)
jne    121b <main+0x9b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe23(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1216 <main+0x96>
movb   $0x3,-0x11(%rbp)
jmp    121b <main+0x9b>
cmpq   $0x0,-0x20(%rbp)
jne    123d <main+0xbd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
mov    $0xdc,%esi
call   1060 <fseek@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x2,%eax
jne    127c <main+0xfc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a4 <main+0x124>
movsbl -0x11(%rbp),%eax
cmp    $0x3,%eax
jne    129f <main+0x11f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a4 <main+0x124>
mov    -0x20(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10a0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
lea    0xe59(%rip),%rsi
call   1090 <fopen@plt>
cmp    $0x1,%ebp
jg     11d0 <main+0x40>
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%rsi
lea    0xe3d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1245 <main+0xb5>
mov    %rax,%r14
mov    $0x2,%r15b
cmp    $0x3,%ebp
jne    11f5 <main+0x65>
mov    0x10(%rbx),%rdi
lea    0xe39(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
sete   %r15b
or     $0x2,%r15b
test   %r14,%r14
je     1252 <main+0xc2>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0xdc,%esi
call   1080 <fseek@plt>
cmp    $0x3,%r15b
mov    0x8(%rbx),%rsi
lea    0xe26(%rip),%rax
lea    0xe01(%rip),%rdi
cmove  %rax,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl %r15b,%edi
mov    %r14,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"rb+\00"
@.str.1 = [8 x i8] c"Usage:\0A\00"
@.str.2 = [27 x i8] c"\09%s program.exe [console]\0A\00"
@.str.3 = [8 x i8] c"console\00"
@SEEK_SET = global i32 0
@.str.4 = [30 x i8] c"Setting Win32 GUI flag in %s\0A\00"
@.str.5 = [34 x i8] c"Setting Win32 Console flag in %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 2, i8* %6
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 0, i32* %3
br label %67
20: ; preds = %2
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 3
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @strcmp(i8* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %23
store i8 3, i8* %6
br label %30
30: ; preds = %29, %23
br label %31
31: ; preds = %30, %20
%32 = load i32*, i32** %7
%33 = icmp ne i32* %32, null
br i1 %33, label %40, label %34
34: ; preds = %31
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @perror(i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %31
%41 = load i32*, i32** %7
%42 = load i32, i32* @SEEK_SET
%43 = call i32 @fseek(i32* %41, i32 220, i32 %42)
%44 = load i8, i8* %6
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 2
br i1 %46, label %47, label %52
47: ; preds = %40
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %62
52: ; preds = %40
%53 = load i8, i8* %6
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 3
br i1 %55, label %56, label %61
56: ; preds = %52
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %56, %52
br label %62
62: ; preds = %61, %47
%63 = load i32*, i32** %7
%64 = call i32 @fwrite(i8* %6, i32 1, i32 1, i32* %63)
%65 = load i32*, i32** %7
%66 = call i32 @fclose(i32* %65)
store i32 0, i32* %3
br label %67
67: ; preds = %62, %14
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [4 x i8] c"rb+\00"
@.str.2 = [27 x i8] c"\09%s program.exe [console]\0A\00"
@.str.3 = [8 x i8] c"console\00"
@SEEK_SET = global i32 0
@.str.4 = [30 x i8] c"Setting Win32 GUI flag in %s\0A\00"
@.str.5 = [34 x i8] c"Setting Win32 Console flag in %s\0A\00"
@str = [7 x i8] c"Usage:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %36
11: ; preds = %2
%12 = icmp eq i32 %0, 3
br i1 %12, label %13, label %19
13: ; preds = %11
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
%18 = select i1 %17, i8 3, i8 2
br label %19
19: ; preds = %13, %11
%20 = phi i8 [ 2, %11 ], [ %18, %13 ]
%21 = icmp eq i32* %5, null
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i8*, i8** %3
%24 = tail call i32 @perror(i8* %23)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = load i32, i32* @SEEK_SET
%28 = tail call i32 @fseek(i32* %5, i32 220, i32 %27)
%29 = icmp eq i8 %20, 3
%30 = load i8*, i8** %3
%31 = select i1 %29, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %31, i8* %30)
%33 = zext i8 %20 to i32
%34 = tail call i32 @fputc(i32 %33, i32* %5)
%35 = tail call i32 @fclose(i32* %5)
br label %36
36: ; preds = %26, %7
ret i32 0
}