#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ close(int);
int connect_to_unix_socket(char *);
int /*<<< orphan*/ display_loop(int);
int /*<<< orphan*/ free(char *);
int open(char *, int, int);
char *realloc(char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char const *);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
int view_instance(const char *path) {
  int ret = EXIT_FAILURE;
  int lockfd = -1;
  int sockfd = -1;
  char *lockpath = NULL;
  char *sockpath = NULL;
  lockpath = strdup(path);
  if (lockpath == NULL)
    goto failure;
  lockpath = realloc(lockpath, strlen(lockpath) + 15);
  if (lockpath == NULL)
    goto failure;
  strcat(lockpath, "/display.lock");
  lockfd = open(lockpath, O_CREAT | O_EXCL, 0600);
  if (lockfd == -1)
    goto failure;
  sockpath = strdup(lockpath);
  if (sockpath == NULL)
    goto failure;
  sockpath[strlen(sockpath) - 4] = 's';
  sockfd = connect_to_unix_socket(sockpath);
  if (sockfd == -1)
    goto failure;
  display_loop(sockfd);
  ret = EXIT_SUCCESS;
failure:
  if (sockfd != -1) {
    shutdown(sockfd, SHUT_RDWR);
    close(sockfd);
  }
  if (sockpath)
    free(sockpath);
  if (lockfd != -1) {
    close(lockfd);
    unlink(lockpath);
  }
  if (lockpath != NULL)
    free(lockpath);
  return ret;
}


x86 assembly (clang, -O0):
<view_instance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <view_instance+0x53>
jmp    12e5 <view_instance+0x135>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
add    $0xf,%esi
call   1090 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1236 <view_instance+0x86>
jmp    12e5 <view_instance+0x135>
mov    -0x20(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2d76(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1278 <view_instance+0xc8>
jmp    12e5 <view_instance+0x135>
mov    -0x20(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1295 <view_instance+0xe5>
jmp    12e5 <view_instance+0x135>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x4,%ecx
movslq %ecx,%rcx
movb   $0x73,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1060 <connect_to_unix_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12d1 <view_instance+0x121>
jmp    12e5 <view_instance+0x135>
mov    -0x14(%rbp),%edi
call   10a0 <display_loop@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1308 <view_instance+0x158>
mov    -0x14(%rbp),%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
cmpq   $0x0,-0x28(%rbp)
je     131c <view_instance+0x16c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x10(%rbp)
je     1337 <view_instance+0x187>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
cmpq   $0x0,-0x20(%rbp)
je     134b <view_instance+0x19b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<view_instance>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e09(%rip),%rax
mov    (%rax),%r15d
call   10d0 <strdup@plt>
test   %rax,%rax
je     12a1 <view_instance+0xf1>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <strlen@plt>
lea    0xf(%rax),%esi
mov    %rbx,%rdi
call   1090 <realloc@plt>
test   %rax,%rax
je     12a1 <view_instance+0xf1>
mov    %rax,%rbx
lea    0xe07(%rip),%rsi
mov    %rax,%rdi
call   10c0 <strcat@plt>
mov    0x2df0(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1299 <view_instance+0xe9>
mov    %eax,%r14d
mov    %rbx,%rdi
call   10d0 <strdup@plt>
test   %rax,%rax
je     1289 <view_instance+0xd9>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <strlen@plt>
cltq
movb   $0x73,-0x4(%rax,%rbp,1)
mov    %rbp,%rdi
call   1060 <connect_to_unix_socket@plt>
cmp    $0xffffffff,%eax
je     1281 <view_instance+0xd1>
mov    %eax,%r12d
mov    %eax,%edi
call   10a0 <display_loop@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%r15d
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1050 <shutdown@plt>
mov    %r12d,%edi
call   1080 <close@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %rbx,%rdi
call   1040 <unlink@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"/display.lock\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@EXIT_SUCCESS = global i32 0
@SHUT_RDWR = global i32 0
define i32 @view_instance(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i32, i32* @EXIT_FAILURE
store i32 %8, i32* %3
store i32 -1, i32* %4
store i32 -1, i32* %5
store i8* null, i8** %6
store i8* null, i8** %7
%9 = load i8*, i8** %2
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
br label %56
14: ; preds = %1
%15 = load i8*, i8** %6
%16 = load i8*, i8** %6
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 15
%19 = call i8* @realloc(i8* %15, i32 %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %14
br label %56
23: ; preds = %14
%24 = load i8*, i8** %6
%25 = call i32 @strcat(i8* %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%26 = load i8*, i8** %6
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_EXCL
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %26, i32 %29, i32 384)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %23
br label %56
34: ; preds = %23
%35 = load i8*, i8** %6
%36 = call i8* @strdup(i8* %35)
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %40
39: ; preds = %34
br label %56
40: ; preds = %34
%41 = load i8*, i8** %7
%42 = load i8*, i8** %7
%43 = call i32 @strlen(i8* %42)
%44 = sub nsw i32 %43, 4
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
store i8 115, i8* %46
%47 = load i8*, i8** %7
%48 = call i32 @connect_to_unix_socket(i8* %47)
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %52
51: ; preds = %40
br label %56
52: ; preds = %40
%53 = load i32, i32* %5
%54 = call i32 @display_loop(i32 %53)
%55 = load i32, i32* @EXIT_SUCCESS
store i32 %55, i32* %3
br label %56
56: ; preds = %52, %51, %39, %33, %22, %13
%57 = load i32, i32* %5
%58 = icmp ne i32 %57, -1
br i1 %58, label %59, label %65
59: ; preds = %56
%60 = load i32, i32* %5
%61 = load i32, i32* @SHUT_RDWR
%62 = call i32 @shutdown(i32 %60, i32 %61)
%63 = load i32, i32* %5
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %59, %56
%66 = load i8*, i8** %7
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i8*, i8** %7
%70 = call i32 @free(i8* %69)
br label %71
71: ; preds = %68, %65
%72 = load i32, i32* %4
%73 = icmp ne i32 %72, -1
br i1 %73, label %74, label %79
74: ; preds = %71
%75 = load i32, i32* %4
%76 = call i32 @close(i32 %75)
%77 = load i8*, i8** %6
%78 = call i32 @unlink(i8* %77)
br label %79
79: ; preds = %74, %71
%80 = load i8*, i8** %6
%81 = icmp ne i8* %80, null
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i8*, i8** %6
%84 = call i32 @free(i8* %83)
br label %85
85: ; preds = %82, %79
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"/display.lock\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@EXIT_SUCCESS = global i32 0
@SHUT_RDWR = global i32 0
define i32 @view_instance(i8* nocapture %0) {
%2 = load i32, i32* @EXIT_FAILURE
%3 = tail call i8* @strdup(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %43, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %3)
%7 = add nsw i32 %6, 15
%8 = tail call i8* @realloc(i8* %3, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %43, label %10
10: ; preds = %5
%11 = tail call i32 @strcat(i8* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_EXCL
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %8, i32 %14, i32 384)
%16 = icmp eq i32 %15, -1
br i1 %16, label %40, label %17
17: ; preds = %10
%18 = tail call i8* @strdup(i8* %8)
%19 = icmp eq i8* %18, null
br i1 %19, label %36, label %20
20: ; preds = %17
%21 = tail call i32 @strlen(i8* %18)
%22 = add nsw i32 %21, -4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %18, i64 %23
store i8 115, i8* %24
%25 = tail call i32 @connect_to_unix_socket(i8* %18)
%26 = icmp eq i32 %25, -1
br i1 %26, label %33, label %27
27: ; preds = %20
%28 = tail call i32 @display_loop(i32 %25)
%29 = load i32, i32* @EXIT_SUCCESS
%30 = load i32, i32* @SHUT_RDWR
%31 = tail call i32 @shutdown(i32 %25, i32 %30)
%32 = tail call i32 @close(i32 %25)
br label %33
33: ; preds = %27, %20
%34 = phi i32 [ %29, %27 ], [ %2, %20 ]
%35 = tail call i32 @free(i8* %18)
br label %36
36: ; preds = %33, %17
%37 = phi i32 [ %34, %33 ], [ %2, %17 ]
%38 = tail call i32 @close(i32 %15)
%39 = tail call i32 @unlink(i8* %8)
br label %40
40: ; preds = %10, %36
%41 = phi i32 [ %37, %36 ], [ %2, %10 ]
%42 = tail call i32 @free(i8* %8)
br label %43
43: ; preds = %1, %5, %40
%44 = phi i32 [ %41, %40 ], [ %2, %5 ], [ %2, %1 ]
ret i32 %44
}