#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int open(char *, int /*<<< orphan*/);
int main(void) {
  char *path = "/etc/passwd";
  int fd;
  FILE *fp;
  fd = open(path, O_RDONLY);
  if (fd >= 0) {
    fp = fdopen(fd, "r");
    if (fp == NULL)
      close(fd);
    else
      fclose(fp);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11b2 <main+0x72>
mov    -0x14(%rbp),%edi
lea    0xe89(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a4 <main+0x64>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    11ad <main+0x6d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11b2 <main+0x72>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1180 <main+0x40>
mov    %eax,%ebx
lea    0xea9(%rip),%rsi
mov    %eax,%edi
call   1050 <fdopen@plt>
test   %rax,%rax
je     1179 <main+0x39>
mov    %rax,%rdi
call   1030 <fclose@plt>
jmp    1180 <main+0x40>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %22
10: ; preds = %0
%11 = load i32, i32* %3
%12 = call i32* @fdopen(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* %3
%17 = call i32 @close(i32 %16)
br label %21
18: ; preds = %10
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
br label %21
21: ; preds = %18, %15
br label %22
22: ; preds = %21, %0
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"r\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %11
4: ; preds = %0
%5 = tail call i32* @fdopen(i32 %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @close(i32 %2)
br label %11
9: ; preds = %4
%10 = tail call i32 @fclose(i32* %5)
br label %11
11: ; preds = %7, %9, %0
ret i32 0
}