#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 */
scalar_t__ openpty(int *, int *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ symlink(char *, char *);
int /*<<< orphan*/ system(char *);
char *ttyname(int);
int /*<<< orphan*/ unlink(scalar_t__ (*)(char *, char *));
int my_openpty(int *amaster, int *aslave, char *name) {
  char *tmpname;
  char *symLink = "/dev/ttyGPS";
  if (openpty(amaster, aslave, NULL, NULL, NULL) < 0)
    return -1;
  if (!name)
    return 0;
  if ((tmpname = ttyname(*aslave)) == NULL)
    return -1;
  if (strlen(tmpname) > 32)
    return -1;
  strcpy(name, tmpname);
  unlink(symlink);
  system("rm /dev/ttyGPS");
  if (symlink(tmpname, symLink) != 0) {
    printf("symlink error!\n");
  }
  return (0);
}


x86 assembly (clang, -O0):
<my_openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe78(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <openpty@plt>
cmp    $0x0,%rax
jge    11bd <my_openpty+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <my_openpty+0xf6>
cmpq   $0x0,-0x20(%rbp)
jne    11d4 <my_openpty+0x64>
movl   $0x0,-0x4(%rbp)
jmp    1266 <my_openpty+0xf6>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1090 <ttyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11f9 <my_openpty+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <my_openpty+0xf6>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x20,%eax
jle    1217 <my_openpty+0xa7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <my_openpty+0xf6>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    0x2db5(%rip),%rdi
call   1030 <unlink@plt>
lea    0xdc9(%rip),%rdi
call   1060 <system@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10a0 <symlink@plt>
cmp    $0x0,%rax
je     125f <my_openpty+0xef>
lea    0xdb5(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_openpty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <openpty@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     11f9 <my_openpty+0x89>
test   %r15,%r15
je     11f7 <my_openpty+0x87>
mov    (%rbx),%edi
call   1090 <ttyname@plt>
test   %rax,%rax
je     11f9 <my_openpty+0x89>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <strlen@plt>
cmp    $0x20,%eax
jg     11f9 <my_openpty+0x89>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    0x2e1a(%rip),%rdi
call   1030 <unlink@plt>
lea    0xe2e(%rip),%rdi
call   1070 <system@plt>
lea    0xe25(%rip),%rsi
mov    %r14,%rdi
call   10a0 <symlink@plt>
test   %rax,%rax
je     11f7 <my_openpty+0x87>
lea    0xe1d(%rip),%rdi
call   1050 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/ttyGPS\00"
@.str.1 = [15 x i8] c"rm /dev/ttyGPS\00"
@.str.2 = [16 x i8] c"symlink error!\0A\00"
define i32 @my_openpty(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i32*, i32** %5
%11 = load i32*, i32** %6
%12 = call i64 @openpty(i32* %10, i32* %11, i32* null, i32* null, i32* null)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %43
15: ; preds = %3
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15
store i32 0, i32* %4
br label %43
19: ; preds = %15
%20 = load i32*, i32** %6
%21 = load i32, i32* %20
%22 = call i8* @ttyname(i32 %21)
store i8* %22, i8** %8
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %4
br label %43
25: ; preds = %19
%26 = load i8*, i8** %8
%27 = call i32 @strlen(i8* %26)
%28 = icmp sgt i32 %27, 32
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %4
br label %43
30: ; preds = %25
%31 = load i8*, i8** %7
%32 = load i8*, i8** %8
%33 = call i32 @strcpy(i8* %31, i8* %32)
%34 = call i32 @unlink(i64 (i8*, i8*)* @symlink)
%35 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%36 = load i8*, i8** %8
%37 = load i8*, i8** %9
%38 = call i64 @symlink(i8* %36, i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %30
%41 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %30
store i32 0, i32* %4
br label %43
43: ; preds = %42, %29, %24, %18, %14
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/ttyGPS\00"
@.str.1 = [15 x i8] c"rm /dev/ttyGPS\00"
@str = [15 x i8] c"symlink error!\00"
define i32 @my_openpty(i32* %0, i32* %1, i8* %2) {
%4 = tail call i64 @openpty(i32* %0, i32* %1, i32* null, i32* null, i32* null)
%5 = icmp slt i64 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = icmp eq i8* %2, null
br i1 %7, label %23, label %8
8: ; preds = %6
%9 = load i32, i32* %1
%10 = tail call i8* @ttyname(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %23, label %12
12: ; preds = %8
%13 = tail call i32 @strlen(i8* %10)
%14 = icmp sgt i32 %13, 32
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = tail call i32 @strcpy(i8* %2, i8* %10)
%17 = tail call i32 @unlink(i64 (i8*, i8*)* @symlink)
%18 = tail call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i64 @symlink(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %15, %21, %12, %8, %6, %3
%24 = phi i32 [ -1, %3 ], [ 0, %6 ], [ -1, %8 ], [ -1, %12 ], [ 0, %21 ], [ 0, %15 ]
ret i32 %24
}