#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 grantpt(int);
int /*<<< orphan*/ perror(char *);
int posix_openpt(int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
int unlockpt(int);
int main(void) {
  int fdm;
  int rc;
  system("ls -l /dev/pts");
  fdm = posix_openpt(O_RDWR);
  if (fdm < 0) {
    perror("posix_openpt");
    return 1;
  }
  rc = grantpt(fdm);
  if (rc != 0) {
    perror("grantpt");
    return 1;
  }
  rc = unlockpt(fdm);
  if (rc != 0) {
    perror("unlockpt");
    return 1;
  }
  system("ls -l /dev/pts");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <system@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
call   1070 <posix_openpt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119e <main+0x4e>
lea    0xe82(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120b <main+0xbb>
mov    -0x8(%rbp),%edi
call   1060 <grantpt@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11cb <main+0x7b>
lea    0xe62(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120b <main+0xbb>
mov    -0x8(%rbp),%edi
call   1040 <unlockpt@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11f8 <main+0xa8>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120b <main+0xbb>
lea    0xe01(%rip),%rdi
call   1030 <system@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1030 <system@plt>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
call   1070 <posix_openpt@plt>
test   %eax,%eax
js     1197 <main+0x47>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <grantpt@plt>
test   %eax,%eax
jne    11a0 <main+0x50>
mov    %ebx,%edi
call   1040 <unlockpt@plt>
test   %eax,%eax
jne    11a9 <main+0x59>
lea    0xe72(%rip),%rdi
call   1030 <system@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe71(%rip),%rdi
jmp    11b0 <main+0x60>
lea    0xe75(%rip),%rdi
jmp    11b0 <main+0x60>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"ls -l /dev/pts\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"posix_openpt\00"
@.str.2 = [8 x i8] c"grantpt\00"
@.str.3 = [9 x i8] c"unlockpt\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @O_RDWR
%6 = call i32 @posix_openpt(i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %27
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 @grantpt(i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %27
18: ; preds = %11
%19 = load i32, i32* %2
%20 = call i32 @unlockpt(i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %27
25: ; preds = %18
%26 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %27
27: ; preds = %25, %23, %16, %9
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [15 x i8] c"ls -l /dev/pts\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"posix_openpt\00"
@.str.2 = [8 x i8] c"grantpt\00"
@.str.3 = [9 x i8] c"unlockpt\00"
define i32 @main() {
%1 = tail call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @posix_openpt(i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %19
7: ; preds = %0
%8 = tail call i32 @grantpt(i32 %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %19
12: ; preds = %7
%13 = tail call i32 @unlockpt(i32 %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %19
17: ; preds = %12
%18 = tail call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %15, %10, %5
%20 = phi i32 [ 1, %5 ], [ 1, %10 ], [ 1, %15 ], [ 0, %17 ]
ret i32 %20
}