#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 tcsetattr(int fd, int optional_actions, const struct termios *termios_p) {
  static int (*tcsetattr_orig)(int, int, const struct termios *) = ((void *)0);
  if (!tcsetattr_orig) {
    dlopen_libc();
    *(void **)(&tcsetattr_orig) = dlsym_("tcsetattr");
  }
  if (fd == 0)
    fd = 2;
  return tcsetattr_orig(fd, optional_actions, termios_p);
}


x86 assembly (clang, -O0):
<tcsetattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
cmpq   $0x0,0x2efe(%rip)
jne    115a <tcsetattr+0x3a>
mov    $0x0,%al
call   1030 <dlopen_libc@plt>
lea    0xeb2(%rip),%rdi
call   1040 <dlsym_@plt>
mov    %rax,0x2ede(%rip)
cmpl   $0x0,-0x4(%rbp)
jne    116b <tcsetattr+0x4b>
movl   $0x2,-0x4(%rbp)
mov    0x2ec6(%rip),%rax
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   *%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcsetattr>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2f06(%rip),%rax
test   %rax,%rax
jne    1151 <tcsetattr+0x31>
xor    %eax,%eax
call   1030 <dlopen_libc@plt>
lea    0xebb(%rip),%rdi
call   1040 <dlsym_@plt>
mov    %rax,0x2ee7(%rip)
test   %ebx,%ebx
mov    $0x2,%edi
cmovne %ebx,%edi
mov    %ebp,%esi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %rbp
jmp    *%rax

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

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