#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*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ pthread_exit(int *);
void *func(void *ptr) {
  int *x = (int *)ptr;
  int *ret = (int *)malloc(sizeof(int));
  int *ret1 = (int *)malloc(sizeof(int));
  *ret1 = 100;
  *x += 1;
  if (*x == 1) {
    *ret = 42;
    pthread_exit(ret);
  } else if (*x == 2) {
    pthread_exit(ret1);
  }
  free(ret);
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x64,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x1,(%rax)
jne    119a <func+0x6a>
mov    -0x20(%rbp),%rax
movl   $0x2a,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <pthread_exit@plt>
jmp    11b5 <func+0x85>
mov    -0x18(%rbp),%rax
cmpl   $0x2,(%rax)
jne    11b0 <func+0x80>
mov    -0x28(%rbp),%rdi
call   1040 <pthread_exit@plt>
jmp    11b5 <func+0x85>
mov    -0x20(%rbp),%rdi
call   1030 <free@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):
<func>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    $0x4,%edi
call   1050 <malloc@plt>
movl   $0x64,(%rax)
mov    (%rbx),%edx
lea    0x1(%rdx),%ecx
mov    %ecx,(%rbx)
test   %edx,%edx
je     1166 <func+0x36>
cmp    $0x2,%ecx
je     1170 <func+0x40>
jmp    1178 <func+0x48>
movl   $0x2a,(%r14)
mov    %r14,%rax
mov    %rax,%rdi
call   1040 <pthread_exit@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <pthread_exit@plt>

LLVM IR (-O0):
define i8* @func(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = bitcast i8* %7 to i32*
store i32* %8, i32** %4
%9 = call i64 @malloc(i32 4)
%10 = inttoptr i64 %9 to i32*
store i32* %10, i32** %5
%11 = call i64 @malloc(i32 4)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %6
%13 = load i32*, i32** %6
store i32 100, i32* %13
%14 = load i32*, i32** %4
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
store i32 %16, i32* %14
%17 = load i32*, i32** %4
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %1
%21 = load i32*, i32** %5
store i32 42, i32* %21
%22 = load i32*, i32** %5
%23 = call i32 @pthread_exit(i32* %22)
br label %32
24: ; preds = %1
%25 = load i32*, i32** %4
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32*, i32** %6
%30 = call i32 @pthread_exit(i32* %29)
br label %31
31: ; preds = %28, %24
br label %32
32: ; preds = %31, %20
%33 = load i32*, i32** %5
%34 = call i32 @free(i32* %33)
%35 = call i32 @pthread_exit(i32* null)
%36 = load i8*, i8** %2
ret i8* %36
}

LLVM IR (-O2):
define noalias i8* @func(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = tail call i64 @malloc(i32 4)
%4 = inttoptr i64 %3 to i32*
%5 = tail call i64 @malloc(i32 4)
%6 = inttoptr i64 %5 to i32*
store i32 100, i32* %6
%7 = load i32, i32* %2
%8 = add nsw i32 %7, 1
store i32 %8, i32* %2
%9 = icmp eq i32 %7, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 42, i32* %4
br label %13
11: ; preds = %1
%12 = icmp eq i32 %8, 2
br i1 %12, label %13, label %16
13: ; preds = %11, %10
%14 = phi i32* [ %4, %10 ], [ %6, %11 ]
%15 = tail call i32 @pthread_exit(i32* %14)
br label %16
16: ; preds = %13, %11
%17 = tail call i32 @free(i32* %4)
%18 = tail call i32 @pthread_exit(i32* null)
ret i8* undef
}