#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 timespec {
  int dummy;
};
typedef int key_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pthread_exit(void *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, size_t, int);
void *pthread_destroy_fn(void *arg) {
  size_t sz = sizeof(struct timespec);
  int shmid;
  key_t key;
  char *shm;
  key = 5678;
  shm = NULL;
  if ((shmid = shmget(key, sz, 0666)) < 0) {
    perror("shmget");
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
  }
  clock_gettime(CLOCK_MONOTONIC, (struct timespec *)shm);
  pthread_exit((void *)0);
}


x86 assembly (clang, -O0):
<pthread_destroy_fn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x4,-0x18(%rbp)
movl   $0x162e,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1b6,%edx
call   1070 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    119c <pthread_destroy_fn+0x4c>
lea    0xe69(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11ca <pthread_destroy_fn+0x7a>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_destroy_fn>:
push   %rbx
mov    $0x4,%esi
mov    $0x162e,%edi
mov    $0x1b6,%edx
call   1070 <shmget@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1198 <pthread_destroy_fn+0x48>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     11a6 <pthread_destroy_fn+0x56>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
pop    %rbx
jmp    1040 <pthread_exit@plt>
lea    0xe61(%rip),%rdi
call   1050 <perror@plt>
jmp    116b <pthread_destroy_fn+0x1b>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
jmp    117f <pthread_destroy_fn+0x2f>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@CLOCK_MONOTONIC = global i32 0
define i8* @pthread_destroy_fn(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i64 4, i64* %4
store i32 5678, i32* %6
store i8* null, i8** %7
%8 = load i32, i32* %6
%9 = load i64, i64* %4
%10 = call i32 @shmget(i32 %8, i64 %9, i32 438)
store i32 %10, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = load i32, i32* %5
%16 = call i8* @shmat(i32 %15, i32* null, i32 0)
store i8* %16, i8** %7
%17 = icmp eq i8* %16, inttoptr (i64 -1 to i8*)
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = load i8*, i8** %7
%23 = bitcast i8* %22 to %struct.timespec*
%24 = call i32 @clock_gettime(i32 %21, %struct.timespec* %23)
%25 = call i32 @pthread_exit(i8* null)
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@CLOCK_MONOTONIC = global i32 0
define noalias i8* @pthread_destroy_fn(i8* nocapture readnone %0) {
%2 = tail call i32 @shmget(i32 5678, i64 4, i32 438)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i8* @shmat(i32 %2, i32* null, i32 0)
%8 = icmp eq i8* %7, inttoptr (i64 -1 to i8*)
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = load i32, i32* @CLOCK_MONOTONIC
%13 = bitcast i8* %7 to %struct.timespec*
%14 = tail call i32 @clock_gettime(i32 %12, %struct.timespec* %13)
%15 = tail call i32 @pthread_exit(i8* null)
ret i8* undef
}