#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 */
struct termios {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ dlopen_libc();
void *dlsym_(char *);
int tcgetattr(int fd, struct termios *termios_p) {
  static int (*tcgetattr_orig)(int, struct termios *) = ((void *)0);
  if (!tcgetattr_orig) {
    dlopen_libc();
    *(void **)(&tcgetattr_orig) = dlsym_("tcgetattr");
  }
  if (fd == 0)
    fd = 2;
  return tcgetattr_orig(fd, termios_p);
}


x86 assembly (clang, -O0):
<tcgetattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,0x2f01(%rip)
jne    1157 <tcgetattr+0x37>
mov    $0x0,%al
call   1030 <dlopen_libc@plt>
lea    0xeb5(%rip),%rdi
call   1040 <dlsym_@plt>
mov    %rax,0x2ee1(%rip)
cmpl   $0x0,-0x4(%rbp)
jne    1168 <tcgetattr+0x48>
movl   $0x2,-0x4(%rbp)
mov    0x2ec9(%rip),%rax
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   *%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcgetattr>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2f09(%rip),%rax
test   %rax,%rax
jne    114e <tcgetattr+0x2e>
xor    %eax,%eax
call   1030 <dlopen_libc@plt>
lea    0xebe(%rip),%rdi
call   1040 <dlsym_@plt>
mov    %rax,0x2eea(%rip)
test   %ebp,%ebp
mov    $0x2,%edi
cmovne %ebp,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    *%rax

LLVM IR (-O0):
%struct.termios = type { i32 }
@tcgetattr.tcgetattr_orig = internal global i32 (i32, %struct.termios*)* null
@.str = [10 x i8] c"tcgetattr\00"
define i32 @tcgetattr(i32 %0, %struct.termios* %1) {
%3 = alloca i32
%4 = alloca %struct.termios*
store i32 %0, i32* %3
store %struct.termios* %1, %struct.termios** %4
%5 = load i32 (i32, %struct.termios*)*, i32 (i32, %struct.termios*)** @tcgetattr.tcgetattr_orig
%6 = icmp ne i32 (i32, %struct.termios*)* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32 (...) @dlopen_libc()
%9 = call i8* @dlsym_(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** bitcast (i32 (i32, %struct.termios*)** @tcgetattr.tcgetattr_orig to i8**)
br label %10
10: ; preds = %7, %2
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 2, i32* %3
br label %14
14: ; preds = %13, %10
%15 = load i32 (i32, %struct.termios*)*, i32 (i32, %struct.termios*)** @tcgetattr.tcgetattr_orig
%16 = load i32, i32* %3
%17 = load %struct.termios*, %struct.termios** %4
%18 = call i32 %15(i32 %16, %struct.termios* %17)
ret i32 %18
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@tcgetattr.tcgetattr_orig = internal unnamed_addr global i32 (i32, %struct.termios*)* null
@.str = [10 x i8] c"tcgetattr\00"
define i32 @tcgetattr(i32 %0, %struct.termios* %1) {
%3 = load i32 (i32, %struct.termios*)*, i32 (i32, %struct.termios*)** @tcgetattr.tcgetattr_orig
%4 = icmp eq i32 (i32, %struct.termios*)* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 (...) @dlopen_libc()
%7 = tail call i8* @dlsym_(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** bitcast (i32 (i32, %struct.termios*)** @tcgetattr.tcgetattr_orig to i8**)
%8 = bitcast i8* %7 to i32 (i32, %struct.termios*)*
br label %9
9: ; preds = %5, %2
%10 = phi i32 (i32, %struct.termios*)* [ %8, %5 ], [ %3, %2 ]
%11 = icmp eq i32 %0, 0
%12 = select i1 %11, i32 2, i32 %0
%13 = tail call i32 %10(i32 %12, %struct.termios* %1)
ret i32 %13
}