#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*/ result;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ WriteDescriptor(int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, int *, int);
int main(int argc, char **argv) {
  int fd;
  if (argc != 2) {
    int result[1] = {500 + argc};
    write(STDOUT_FILENO, result, sizeof(result));
    return 1;
  }
  fd = open(argv[1], O_CREAT | O_RDWR);
  if (fd < 0) {
    int result[1] = {2};
    write(STDOUT_FILENO, result, sizeof(result));
    return 2;
  }
  int result[1] = {0};
  write(STDOUT_FILENO, result, sizeof(result));
  WriteDescriptor(STDOUT_FILENO, fd);
  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)
cmpl   $0x2,-0x8(%rbp)
je     118e <main+0x4e>
mov    -0x8(%rbp),%eax
add    $0x1f4,%eax
mov    %eax,-0x18(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
movl   $0x1,-0x4(%rbp)
jmp    1225 <main+0xe5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e6 <main+0xa6>
mov    0xe40(%rip),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
movl   $0x2,-0x4(%rbp)
jmp    1225 <main+0xe5>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
call   1040 <WriteDescriptor@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    118c <main+0x5c>
mov    0x8(%rsi),%rdi
mov    0x2eb1(%rip),%rax
mov    0x2e8a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11b5 <main+0x85>
mov    %eax,%ebx
movl   $0x0,0xc(%rsp)
mov    0x2e7c(%rip),%r14
mov    (%r14),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    (%r14),%edi
mov    %ebx,%esi
call   1040 <WriteDescriptor@plt>
xor    %eax,%eax
jmp    11da <main+0xaa>
add    $0x1f4,%edi
mov    %edi,0x14(%rsp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0x1,%eax
jmp    11da <main+0xaa>
movl   $0x2,0x10(%rsp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0x2,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@__const.main.result = [1 x i32] [i32 2]
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1 x i32]
%8 = alloca [1 x i32]
%9 = alloca [1 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = getelementptr inbounds [1 x i32], [1 x i32]* %7, i64 0, i64 0
%14 = load i32, i32* %4
%15 = add nsw i32 500, %14
store i32 %15, i32* %13
%16 = load i32, i32* @STDOUT_FILENO
%17 = getelementptr inbounds [1 x i32], [1 x i32]* %7, i64 0, i64 0
%18 = call i32 @write(i32 %16, i32* %17, i32 4)
store i32 1, i32* %3
br label %42
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_CREAT
%24 = load i32, i32* @O_RDWR
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %22, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = bitcast [1 x i32]* %8 to i8*
%31 = load i32, i32* @STDOUT_FILENO
%32 = getelementptr inbounds [1 x i32], [1 x i32]* %8, i64 0, i64 0
%33 = call i32 @write(i32 %31, i32* %32, i32 4)
store i32 2, i32* %3
br label %42
34: ; preds = %19
%35 = bitcast [1 x i32]* %9 to i8*
%36 = load i32, i32* @STDOUT_FILENO
%37 = getelementptr inbounds [1 x i32], [1 x i32]* %9, i64 0, i64 0
%38 = call i32 @write(i32 %36, i32* %37, i32 4)
%39 = load i32, i32* @STDOUT_FILENO
%40 = load i32, i32* %6
%41 = call i32 @WriteDescriptor(i32 %39, i32 %40)
store i32 0, i32* %3
br label %42
42: ; preds = %34, %29, %12
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x i32]
%4 = alloca [1 x i32]
%5 = alloca [1 x i32]
%6 = icmp eq i32 %0, 2
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = bitcast [1 x i32]* %3 to i8*
%9 = getelementptr inbounds [1 x i32], [1 x i32]* %3, i64 0, i64 0
%10 = add nsw i32 %0, 500
store i32 %10, i32* %9
%11 = load i32, i32* @STDOUT_FILENO
%12 = call i32 @write(i32 %11, i32* %9, i32 4)
br label %33
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_RDWR
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %15, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = bitcast [1 x i32]* %4 to i8*
%23 = getelementptr inbounds [1 x i32], [1 x i32]* %4, i64 0, i64 0
store i32 2, i32* %23
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @write(i32 %24, i32* %23, i32 4)
br label %33
26: ; preds = %13
%27 = bitcast [1 x i32]* %5 to i8*
%28 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
store i32 0, i32* %28
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @write(i32 %29, i32* %28, i32 4)
%31 = load i32, i32* @STDOUT_FILENO
%32 = tail call i32 @WriteDescriptor(i32 %31, i32 %19)
br label %33
33: ; preds = %26, %21, %7
%34 = phi i32 [ 1, %7 ], [ 2, %21 ], [ 0, %26 ]
ret i32 %34
}