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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopendir(int);
int /*<<< orphan*/ *opendir(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void noleak(int x, int y, int z) {
  DIR *p1 = fdopendir(x);
  closedir(p1);
  DIR *p2 = opendir("abc");
  closedir(p2);
  int s = socket(AF_INET, SOCK_STREAM, 0);
  close(s);
}


x86 assembly (clang, -O0):
<noleak>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <fdopendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
lea    0xe83(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<noleak>:
push   %rax
call   1060 <fdopendir@plt>
mov    %rax,%rdi
call   1050 <closedir@plt>
lea    0xe9b(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rdi
call   1050 <closedir@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%edi
pop    %rax
jmp    1040 <close@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"abc\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define void @noleak(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %4
%11 = call i32* @fdopendir(i32 %10)
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = call i32 @closedir(i32* %12)
%14 = call i32* @opendir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = call i32 @closedir(i32* %15)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = call i32 @close(i32 %20)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"abc\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define void @noleak(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32* @fdopendir(i32 %0)
%5 = tail call i32 @closedir(i32* %4)
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @closedir(i32* %6)
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = tail call i32 @close(i32 %10)
ret void
}