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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  int fd;
  fd = open(argv[1], O_RDWR);
  printf("fd: %d\n", fd);
  if (fd < 0) {
    perror("open()");
  }
  close(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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe87(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jge    1194 <main+0x54>
lea    0xe79(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x8(%rsi),%rdi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
lea    0xea4(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
test   %ebx,%ebx
js     1172 <main+0x32>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe8f(%rip),%rdi
call   1060 <perror@plt>
jmp    1167 <main+0x27>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [8 x i8] c"fd: %d\0A\00"
@.str.1 = [7 x i8] c"open()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i8**, i8*** %5
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %6
%20 = call i32 @close(i32 %19)
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [8 x i8] c"fd: %d\0A\00"
@.str.1 = [7 x i8] c"open()\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %4, i32 %5)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = tail call i32 @close(i32 %6)
ret i32 0
}